Skip to content

Commit

Permalink
Fix json decode transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
leomarquine committed Nov 5, 2018
1 parent 798778f commit 776f8e7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
27 changes: 10 additions & 17 deletions src/Transformers/JsonDecode.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace Marquine\Etl\Transformers;

use Marquine\Etl\Row;

class JsonDecode extends Transformer
{
/**
* Transformer columns.
*
* @var array
*/
protected $columns;
protected $columns = [];

/**
* Use associative arrays.
Expand Down Expand Up @@ -42,24 +44,15 @@ class JsonDecode extends Transformer
];

/**
* Get the transformer handler.
* Transform the given row.
*
* @return callable
* @param \Marquine\Etl\Row $row
* @return void
*/
public function transform()
public function transform(Row $row)
{
return function ($row) {
if ($this->columns) {
foreach ($this->columns as $column) {
$row[$column] = json_decode($row[$column], $this->assoc, $this->depth, $this->options);
}
} else {
foreach ($row as $column => $value) {
$row[$column] = json_decode($value, $this->assoc, $this->depth, $this->options);
}
}

return $row;
};
$row->transform($this->columns, function ($column) {
return json_decode($column, $this->assoc, $this->depth, $this->options);
});
}
}
38 changes: 25 additions & 13 deletions tests/Transformers/JsonDecodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,67 @@
namespace Tests\Transformers;

use Tests\TestCase;
use Marquine\Etl\Row;
use Marquine\Etl\Transformers\JsonDecode;

class JsonDecodeTest extends TestCase
{
protected $data = [
['id' => '"1"', 'data' => '{"name":"John Doe","email":"johndoe@email.com"}'],
['id' => '"2"', 'data' => '{"name":"Jane Doe","email":"janedoe@email.com"}'],
];
protected function setUp()
{
parent::setUp();

$this->data = [
new Row(['id' => '"1"', 'data' => '{"name":"John Doe","email":"johndoe@email.com"}']),
new Row(['id' => '"2"', 'data' => '{"name":"Jane Doe","email":"janedoe@email.com"}']),
];
}

/** @test */
public function default_options()
{
$expected = [
['id' => '1', 'data' => (object) ['name' => 'John Doe', 'email' => 'johndoe@email.com']],
['id' => '2', 'data' => (object) ['name' => 'Jane Doe', 'email' => 'janedoe@email.com']],
new Row(['id' => '1', 'data' => (object) ['name' => 'John Doe', 'email' => 'johndoe@email.com']]),
new Row(['id' => '2', 'data' => (object) ['name' => 'Jane Doe', 'email' => 'janedoe@email.com']]),
];

$transformer = new JsonDecode;

$this->assertEquals($expected, array_map($transformer->transform(), $this->data));
array_map([$transformer, 'transform'], $this->data);

$this->assertEquals($expected, $this->data);
}

/** @test */
public function converting_objects_to_associative_arrays()
{
$expected = [
['id' => '1', 'data' => ['name' => 'John Doe', 'email' => 'johndoe@email.com']],
['id' => '2', 'data' => ['name' => 'Jane Doe', 'email' => 'janedoe@email.com']],
new Row(['id' => '1', 'data' => ['name' => 'John Doe', 'email' => 'johndoe@email.com']]),
new Row(['id' => '2', 'data' => ['name' => 'Jane Doe', 'email' => 'janedoe@email.com']]),
];

$transformer = new JsonDecode;

$transformer->options(['assoc' => true]);

$this->assertEquals($expected, array_map($transformer->transform(), $this->data));
array_map([$transformer, 'transform'], $this->data);

$this->assertEquals($expected, $this->data);
}

/** @test */
public function custom_columns()
{
$expected = [
['id' => '"1"', 'data' => (object) ['name' => 'John Doe', 'email' => 'johndoe@email.com']],
['id' => '"2"', 'data' => (object) ['name' => 'Jane Doe', 'email' => 'janedoe@email.com']],
new Row(['id' => '"1"', 'data' => (object) ['name' => 'John Doe', 'email' => 'johndoe@email.com']]),
new Row(['id' => '"2"', 'data' => (object) ['name' => 'Jane Doe', 'email' => 'janedoe@email.com']]),
];

$transformer = new JsonDecode;

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

$this->assertEquals($expected, array_map($transformer->transform(), $this->data));
array_map([$transformer, 'transform'], $this->data);

$this->assertEquals($expected, $this->data);
}
}

0 comments on commit 776f8e7

Please sign in to comment.