diff --git a/src/core/etl/README.md b/src/core/etl/README.md index 9c3782e7f..2bb6deff6 100644 --- a/src/core/etl/README.md +++ b/src/core/etl/README.md @@ -216,7 +216,6 @@ Adapters might also define some custom transformers. * [keep entries](src/Flow/ETL/Transformer/KeepEntriesTransformer.php) - [tests](tests/Flow/ETL/Tests/Unit/Transformer/KeepEntriesTransformerTest.php) * [remove entries](src/Flow/ETL/Transformer/RemoveEntriesTransformer.php) - [tests](tests/Flow/ETL/Tests/Unit/Transformer/RemoveEntriesTransformerTest.php) * [rename entries](src/Flow/ETL/Transformer/RenameEntryTransformer.php) - [tests](tests/Flow/ETL/Tests/Unit/Transformer/RenameEntryTransformerTest.php) - * [static entry](src/Flow/ETL/Transformer/StaticEntryTransformer.php) - [tests](tests/Flow/ETL/Tests/Unit/Transformer/StaticEntryTransformerTest.php) * **Array** * [array keys style converter](src/Flow/ETL/Transformer/ArrayKeysStyleConverterTransformer.php) - [tests](tests/Flow/ETL/Tests/Unit/Transformer/ArrayKeysStyleConverterTransformerTest.php) * **Callback** - *Might come with performance degradation* diff --git a/src/core/etl/src/Flow/ETL/DSL/Transform.php b/src/core/etl/src/Flow/ETL/DSL/Transform.php index 38242abd7..76d3f94d5 100644 --- a/src/core/etl/src/Flow/ETL/DSL/Transform.php +++ b/src/core/etl/src/Flow/ETL/DSL/Transform.php @@ -4,7 +4,6 @@ namespace Flow\ETL\DSL; -use Flow\ETL\DSL\Entry as DSLEntry; use Flow\ETL\Exception\InvalidArgumentException; use Flow\ETL\Exception\RuntimeException; use Flow\ETL\Row; @@ -23,27 +22,6 @@ */ class Transform { - /** - * @param array $data - */ - final public static function add_json(string $name, array $data) : Transformer - { - return new Transformer\StaticEntryTransformer(DSLEntry::json($name, $data)); - } - - final public static function add_json_from_string(string $name, string $json) : Transformer - { - return new Transformer\StaticEntryTransformer(DSLEntry::json($name, (array) \json_decode($json, true, 512, JSON_THROW_ON_ERROR))); - } - - /** - * @param array $data - */ - final public static function add_json_object(string $name, array $data) : Transformer - { - return new Transformer\StaticEntryTransformer(DSLEntry::json_object($name, $data)); - } - /** * @param ?Schema $schema Desired schema of unpacked elements. Elements not found in schema will be auto detected. * It is allowed to provide definitions only for selected elements, like for example diff --git a/src/core/etl/src/Flow/ETL/Transformer/StaticEntryTransformer.php b/src/core/etl/src/Flow/ETL/Transformer/StaticEntryTransformer.php deleted file mode 100644 index ae3307f0b..000000000 --- a/src/core/etl/src/Flow/ETL/Transformer/StaticEntryTransformer.php +++ /dev/null @@ -1,40 +0,0 @@ - - */ -final class StaticEntryTransformer implements Transformer -{ - public function __construct(private readonly Entry $entry) - { - } - - public function __serialize() : array - { - return [ - 'entry' => $this->entry, - ]; - } - - public function __unserialize(array $data) : void - { - $this->entry = $data['entry']; - } - - public function transform(Rows $rows, FlowContext $context) : Rows - { - $transformer = fn (Row $row) : Row => $row->set($this->entry); - - return $rows->map($transformer); - } -} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/AddJsonTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/AddJsonTest.php new file mode 100644 index 000000000..964c98713 --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/AddJsonTest.php @@ -0,0 +1,114 @@ +read( + From::array( + [['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]]], + ) + ) + ->withEntry('row', array_unpack(ref('row'))) + ->renameAll('row.', '') + ->drop('row') + ->withEntry('array', ref('array')->arrayMerge(lit(['d' => 4]))) + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + ['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]], + ], + $memory->data + ); + } + + public function test_add_json_string_into_existing_reference() : void + { + (new Flow()) + ->read( + From::array( + [['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]]], + ) + ) + ->withEntry('row', array_unpack(ref('row'))) + ->renameAll('row.', '') + ->drop('row') + ->withEntry('json', lit('{"d": 4}')) + ->withEntry('array', ref('array')->arrayMerge(ref('json')->jsonDecode())) + ->drop('json') + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + ['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]], + ], + $memory->data + ); + } + + public function test_adding_json_as_object_from_string_entry() : void + { + (new Flow()) + ->read( + From::array([['id' => 1]]) + ) + ->withEntry('row', array_unpack(ref('row'))) + ->renameAll('row.', '') + ->drop('row') + ->withEntry('json', lit(['id' => 1, 'name' => 'test'])) + ->withEntry('json', ref('json')->jsonEncode(\JSON_FORCE_OBJECT)) + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + [ + 'id' => 1, + 'json' => '{"id":1,"name":"test"}', + ], + ], + $memory->data + ); + } + + public function test_adding_json_from_string_entry() : void + { + (new Flow()) + ->read( + From::array([['id' => 1]]) + ) + ->withEntry('row', array_unpack(ref('row'))) + ->renameAll('row.', '') + ->drop('row') + ->withEntry('json', lit('[{"id":1},{"id":2}]')) + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + [ + 'id' => 1, + 'json' => '[{"id":1},{"id":2}]', + ], + ], + $memory->data + ); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/StaticEntryTransformerTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/StaticEntryTransformerTest.php deleted file mode 100644 index 651a8b357..000000000 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/StaticEntryTransformerTest.php +++ /dev/null @@ -1,78 +0,0 @@ - 1], ['id' => 2]]); - - $rows = $transformer->transform(new Rows( - Row::create( - new Row\Entry\StringEntry('id', '1'), - ) - ), new FlowContext(Config::default())); - - $this->assertEquals( - [ - [ - 'id' => '1', - 'json' => '[{"id":1},{"id":2}]', - ], - ], - $rows->toArray() - ); - } - - public function test_adding_json_from_string_entry() : void - { - $transformer = Transform::add_json_from_string('json', '[{"id":1},{"id":2}]'); - - $rows = $transformer->transform(new Rows( - Row::create( - new Row\Entry\StringEntry('id', '1'), - ) - ), new FlowContext(Config::default())); - - $this->assertEquals( - [ - [ - 'id' => '1', - 'json' => '[{"id":1},{"id":2}]', - ], - ], - $rows->toArray() - ); - } - - public function test_adding_json_object() : void - { - $transformer = Transform::add_json_object('json', ['id' => 1, 'name' => 'test']); - - $rows = $transformer->transform(new Rows( - Row::create( - new Row\Entry\StringEntry('id', '1'), - ) - ), new FlowContext(Config::default())); - - $this->assertEquals( - [ - [ - 'id' => '1', - 'json' => '{"id":1,"name":"test"}', - ], - ], - $rows->toArray() - ); - } -}