Skip to content

Commit

Permalink
Add json encode transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
leomarquine committed Aug 23, 2017
1 parent bbcd98b commit 4c3cbd2
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 0 deletions.
26 changes: 26 additions & 0 deletions docs/transformers.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,29 @@ $options = [

$job->transform('trim', $options);
```

## Json Encode
### Syntax
```php
$job->transform('JsonEncode', $options);
```
### Options
| Name | Type | Default | Description |
| ---- | ------ |-------- | ----------- |
| columns | array | null | Columns that will be transformed. |
| options | int | 0 | Bitmask consisting of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_PRESERVE_ZERO_FRACTION, JSON_UNESCAPED_UNICODE, JSON_PARTIAL_OUTPUT_ON_ERROR. The behaviour of these constants is described on the [JSON constants](http://php.net/manual/en/json.constants.php) page. |
| depth | int | 512 | The maximum depth. Must be greater than zero. |

### Examples
Convert all columns to JSON:
```php
$job->transform('JsonEncode');
```
Convert specific columns to JSON:
```php
$options = [
'columns' => ['data'],
];

$job->transform('JsonEncode', $options);
```
50 changes: 50 additions & 0 deletions src/Transformers/JsonEncode.php
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);
}
}
51 changes: 51 additions & 0 deletions tests/Transformers/JsonEncodeTest.php
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);
}
}

0 comments on commit 4c3cbd2

Please sign in to comment.