Skip to content

Commit 5ec7c9c

Browse files
committed
Fix json encode transformer
1 parent 776f8e7 commit 5ec7c9c

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

src/Transformers/JsonEncode.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace Marquine\Etl\Transformers;
44

5+
use Marquine\Etl\Row;
6+
57
class JsonEncode extends Transformer
68
{
79
/**
810
* Transformer columns.
911
*
1012
* @var array
1113
*/
12-
protected $columns;
14+
protected $columns = [];
1315

1416
/**
1517
* Options.
@@ -35,24 +37,15 @@ class JsonEncode extends Transformer
3537
];
3638

3739
/**
38-
* Get the transformer handler.
40+
* Transform the given row.
3941
*
40-
* @return callable
42+
* @param \Marquine\Etl\Row $row
43+
* @return void
4144
*/
42-
public function transform()
45+
public function transform(Row $row)
4346
{
44-
return function ($row) {
45-
if ($this->columns) {
46-
foreach ($this->columns as $column) {
47-
$row[$column] = json_encode($row[$column], $this->options, $this->depth);
48-
}
49-
} else {
50-
foreach ($row as $column => $value) {
51-
$row[$column] = json_encode($value, $this->options, $this->depth);
52-
}
53-
}
54-
55-
return $row;
56-
};
47+
$row->transform($this->columns, function ($column) {
48+
return json_encode($column, $this->options, $this->depth);
49+
});
5750
}
5851
}

tests/Transformers/JsonEncodeTest.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,50 @@
33
namespace Tests\Transformers;
44

55
use Tests\TestCase;
6+
use Marquine\Etl\Row;
67
use Marquine\Etl\Transformers\JsonEncode;
78

89
class JsonEncodeTest extends TestCase
910
{
10-
protected $data = [
11-
['id' => '1', 'data' => ['name' => 'John Doe', 'email' => 'johndoe@email.com']],
12-
['id' => '2', 'data' => ['name' => 'Jane Doe', 'email' => 'janedoe@email.com']],
13-
];
11+
protected function setUp()
12+
{
13+
parent::setUp();
14+
15+
$this->data = [
16+
new Row(['id' => '1', 'data' => ['name' => 'John Doe', 'email' => 'johndoe@email.com']]),
17+
new Row(['id' => '2', 'data' => ['name' => 'Jane Doe', 'email' => 'janedoe@email.com']]),
18+
];
19+
}
1420

1521
/** @test */
1622
public function default_options()
1723
{
1824
$expected = [
19-
['id' => '"1"', 'data' => '{"name":"John Doe","email":"johndoe@email.com"}'],
20-
['id' => '"2"', 'data' => '{"name":"Jane Doe","email":"janedoe@email.com"}'],
25+
new Row(['id' => '"1"', 'data' => '{"name":"John Doe","email":"johndoe@email.com"}']),
26+
new Row(['id' => '"2"', 'data' => '{"name":"Jane Doe","email":"janedoe@email.com"}']),
2127
];
2228

2329
$transformer = new JsonEncode;
2430

25-
$this->assertEquals($expected, array_map($transformer->transform(), $this->data));
31+
array_map([$transformer, 'transform'], $this->data);
32+
33+
$this->assertEquals($expected, $this->data);
2634
}
2735

2836
/** @test */
2937
public function custom_columns()
3038
{
3139
$expected = [
32-
['id' => '1', 'data' => '{"name":"John Doe","email":"johndoe@email.com"}'],
33-
['id' => '2', 'data' => '{"name":"Jane Doe","email":"janedoe@email.com"}'],
40+
new Row(['id' => '1', 'data' => '{"name":"John Doe","email":"johndoe@email.com"}']),
41+
new Row(['id' => '2', 'data' => '{"name":"Jane Doe","email":"janedoe@email.com"}']),
3442
];
3543

3644
$transformer = new JsonEncode;
3745

3846
$transformer->options(['columns' => ['data']]);
3947

40-
$this->assertEquals($expected, array_map($transformer->transform(), $this->data));
48+
array_map([$transformer, 'transform'], $this->data);
49+
50+
$this->assertEquals($expected, $this->data);
4151
}
4252
}

0 commit comments

Comments
 (0)