-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bbcd98b
commit 4c3cbd2
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Marquine\Etl\Transformers; | ||
|
||
class JsonEncode implements TransformerInterface | ||
{ | ||
/** | ||
* Transformer columns. | ||
* | ||
* @var array | ||
*/ | ||
public $columns; | ||
|
||
/** | ||
* Options. | ||
* | ||
* @var int | ||
*/ | ||
public $options = 0; | ||
|
||
/** | ||
* Maximum depth. | ||
* | ||
* @var string | ||
*/ | ||
public $depth = 512; | ||
|
||
/** | ||
* Execute a transformation. | ||
* | ||
* @param array $items | ||
* @return array | ||
*/ | ||
public function transform($items) | ||
{ | ||
return array_map(function($row) { | ||
if ($this->columns) { | ||
foreach ($this->columns as $column) { | ||
$row[$column] = json_encode($row[$column], $this->options, $this->depth); | ||
} | ||
} else { | ||
foreach ($row as $column => $value) { | ||
$row[$column] = json_encode($value, $this->options, $this->depth); | ||
} | ||
} | ||
|
||
return $row; | ||
}, $items); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace Tests\Transformers; | ||
|
||
use Tests\TestCase; | ||
use Marquine\Etl\Transformers\JsonEncode; | ||
|
||
class JsonEncodeTest extends TestCase | ||
{ | ||
/** @test */ | ||
function convert_all_columns_to_json() | ||
{ | ||
$items = [ | ||
['id' => '1', 'data' => ['name' => 'John Doe', 'email' => 'johndoe@email.com']], | ||
['id' => '2', 'data' => ['name' => 'Jane Doe', 'email' => 'janedoe@email.com']], | ||
]; | ||
|
||
$transformer = new JsonEncode; | ||
|
||
$results = $transformer->transform($items); | ||
|
||
$expected = [ | ||
['id' => '"1"', 'data' => '{"name":"John Doe","email":"johndoe@email.com"}'], | ||
['id' => '"2"', 'data' => '{"name":"Jane Doe","email":"janedoe@email.com"}'], | ||
]; | ||
|
||
$this->assertEquals($expected, $results); | ||
} | ||
|
||
/** @test */ | ||
function convert_specific_columns_to_json() | ||
{ | ||
$items = [ | ||
['id' => '1', 'data' => ['name' => 'John Doe', 'email' => 'johndoe@email.com']], | ||
['id' => '2', 'data' => ['name' => 'Jane Doe', 'email' => 'janedoe@email.com']], | ||
]; | ||
|
||
$transformer = new JsonEncode; | ||
|
||
$transformer->columns = ['data']; | ||
|
||
$results = $transformer->transform($items); | ||
|
||
$expected = [ | ||
['id' => '1', 'data' => '{"name":"John Doe","email":"johndoe@email.com"}'], | ||
['id' => '2', 'data' => '{"name":"Jane Doe","email":"janedoe@email.com"}'], | ||
]; | ||
|
||
$this->assertEquals($expected, $results); | ||
} | ||
} |