From f17d7d51fbf7b9c817866594d133425bb90ed030 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Thu, 5 Oct 2023 22:26:10 +0200 Subject: [PATCH] Remove `ArrayPushTransformer` & `CallbackEntryTransformer` --- src/core/etl/README.md | 1 - src/core/etl/src/Flow/ETL/DSL/Transform.php | 33 ------- .../ETL/Transformer/ArrayPushTransformer.php | 63 ------------- .../Transformer/CallbackEntryTransformer.php | 90 ------------------- .../Transformer/ArrayPushTransformerTest.php | 54 ----------- .../CallbackEntryTransformerTest.php | 87 ------------------ 6 files changed, 328 deletions(-) delete mode 100644 src/core/etl/src/Flow/ETL/Transformer/ArrayPushTransformer.php delete mode 100755 src/core/etl/src/Flow/ETL/Transformer/CallbackEntryTransformer.php delete mode 100644 src/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/ArrayPushTransformerTest.php delete mode 100755 src/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/CallbackEntryTransformerTest.php diff --git a/src/core/etl/README.md b/src/core/etl/README.md index 6533fc427..94cea636f 100644 --- a/src/core/etl/README.md +++ b/src/core/etl/README.md @@ -221,7 +221,6 @@ Adapters might also define some custom transformers. * [array keys style converter](src/Flow/ETL/Transformer/ArrayKeysStyleConverterTransformer.php) - [tests](tests/Flow/ETL/Tests/Unit/Transformer/ArrayKeysStyleConverterTransformerTest.php) * [array sort](src/Flow/ETL/Transformer/ArraySortTransformer.php) - [tests](tests/Flow/ETL/Tests/Unit/Transformer/ArraySortTransformerTest.php) * **Callback** - *Might come with performance degradation* - * [callback entry](src/Flow/ETL/Transformer/CallbackEntryTransformer.php) - [tests](tests/Flow/ETL/Tests/Unit/Transformer/CallbackEntryTransformerTest.php) * [callback row](src/Flow/ETL/Transformer/CallbackRowTransformer.php) - [tests](tests/Flow/ETL/Tests/Unit/Transformer/CallbackRowTransformerTest.php) Some transformers come with complex configuration, please find more details [here](/docs/complex_transformers.md). diff --git a/src/core/etl/src/Flow/ETL/DSL/Transform.php b/src/core/etl/src/Flow/ETL/DSL/Transform.php index 5e72cf969..1d9c028e2 100644 --- a/src/core/etl/src/Flow/ETL/DSL/Transform.php +++ b/src/core/etl/src/Flow/ETL/DSL/Transform.php @@ -9,7 +9,6 @@ use Flow\ETL\Exception\RuntimeException; use Flow\ETL\Row; use Flow\ETL\Row\Entries; -use Flow\ETL\Row\Entry; use Flow\ETL\Row\Factory\NativeEntryFactory; use Flow\ETL\Row\Reference; use Flow\ETL\Row\Schema; @@ -47,8 +46,6 @@ final public static function add_json_object(string $name, array $data) : Transf } /** - * @param string $array_column - * @param string $style * @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 * when converting enum string value into specific Enum. @@ -68,20 +65,6 @@ final public static function array_convert_keys(string $array_column, string $st ); } - /** - * Pushes static values into existing array entry, if array entry does not exist, this transformer - * will create one. - * - * @param string $array_entry - * @param array $values - * - * @return Transformer - */ - final public static function array_push(string $array_entry, array $values = []) : Transformer - { - return new Transformer\ArrayPushTransformer($array_entry, $values); - } - final public static function array_reverse(string $array_name) : Transformer { return new Transformer\ArrayReverseTransformer($array_name); @@ -92,16 +75,6 @@ final public static function array_sort(string $array_name, int $sort_flag = \SO return new Transformer\ArraySortTransformer($array_name, $sort_flag); } - /** - * @psalm-param callable(Entry $entry) : Entry ...$callables - * - * @param callable(Entry $entry) : Entry ...$callables - */ - final public static function callback_entry(callable ...$callables) : Transformer - { - return new Transformer\CallbackEntryTransformer(...$callables); - } - /** * @param callable(Row) : Row $callable */ @@ -157,12 +130,6 @@ public static function rename_all_case(bool $upper = false, bool $lower = false, return new Transformer\RenameAllCaseTransformer($upper, $lower, $ucfirst, $ucwords); } - /** - * @param string $search - * @param string $replace - * - * @return Transformer - */ public static function rename_str_replace_all(string $search, string $replace) : Transformer { return new Transformer\RenameStrReplaceAllEntriesTransformer($search, $replace); diff --git a/src/core/etl/src/Flow/ETL/Transformer/ArrayPushTransformer.php b/src/core/etl/src/Flow/ETL/Transformer/ArrayPushTransformer.php deleted file mode 100644 index 5a057b09e..000000000 --- a/src/core/etl/src/Flow/ETL/Transformer/ArrayPushTransformer.php +++ /dev/null @@ -1,63 +0,0 @@ - - */ -final class ArrayPushTransformer implements Transformer -{ - /** - * @param string $arrayEntry - * @param array $values - */ - public function __construct( - private readonly string $arrayEntry, - private readonly array $values = [] - ) { - } - - public function __serialize() : array - { - return [ - 'array_entry' => $this->arrayEntry, - 'values' => $this->values, - ]; - } - - public function __unserialize(array $data) : void - { - $this->arrayEntry = $data['array_entry']; - $this->values = $data['values']; - } - - public function transform(Rows $rows, FlowContext $context) : Rows - { - $transformer = function (Row $row) : Row { - if (!$row->entries()->has($this->arrayEntry)) { - $arrayEntry = Entry::array($this->arrayEntry, []); - } else { - $arrayEntry = $row->entries()->get($this->arrayEntry); - } - - if (!$arrayEntry instanceof Row\Entry\ArrayEntry) { - throw new RuntimeException("\"{$this->arrayEntry}\" is not ArrayEntry"); - } - - return $row->set( - Entry::array($this->arrayEntry, \array_merge($arrayEntry->value(), $this->values)) - ); - }; - - return $rows->map($transformer); - } -} diff --git a/src/core/etl/src/Flow/ETL/Transformer/CallbackEntryTransformer.php b/src/core/etl/src/Flow/ETL/Transformer/CallbackEntryTransformer.php deleted file mode 100755 index f8af9814d..000000000 --- a/src/core/etl/src/Flow/ETL/Transformer/CallbackEntryTransformer.php +++ /dev/null @@ -1,90 +0,0 @@ -}> - */ -final class CallbackEntryTransformer implements Transformer -{ - /** - * @phpstan-var array - */ - private readonly array $callables; - - /** - * @param callable(Entry) : Entry ...$callables - */ - public function __construct(callable ...$callables) - { - $this->callables = $callables; - } - - public function __serialize() : array - { - if (!Closure::isSerializable()) { - throw new RuntimeException('CallbackEntryTransformer is not serializable without "opis/closure" library in your dependencies.'); - } - - $closures = []; - - foreach ($this->callables as $callable) { - $closures[] = new SerializableClosure(\Closure::fromCallable($callable)); - } - - return [ - 'callables' => $closures, - ]; - } - - public function __unserialize(array $data) : void - { - if (!Closure::isSerializable()) { - throw new RuntimeException('CallbackEntryTransformer is not serializable without "opis/closure" library in your dependencies.'); - } - - $callables = []; - - foreach ($data['callables'] as $closure) { - $callables[] = $closure->getClosure(); - } - - /** - * @psalm-suppress PropertyTypeCoercion - * @psalm-suppress MixedPropertyTypeCoercion - */ - $this->callables = $callables; - } - - public function transform(Rows $rows, FlowContext $context) : Rows - { - /** - * @var callable(Row) : Row $transform - */ - $transform = function (Row $row) : Row { - $callable = function (Row\Entry $entry) : Row\Entry { - foreach ($this->callables as $callable) { - $entry = $callable($entry); - } - - return $entry; - }; - $entries = $row->entries()->map($callable); - - return new Row(new Row\Entries(...$entries)); - }; - - return $rows->map($transform); - } -} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/ArrayPushTransformerTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/ArrayPushTransformerTest.php deleted file mode 100644 index c1ad71f77..000000000 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/ArrayPushTransformerTest.php +++ /dev/null @@ -1,54 +0,0 @@ -assertSame( - [ - [ - 'array' => ['A', 'Z', 'C', 'O', 1], - ], - ], - $transformer->transform(new Rows(Row::create($arrayEntry)), new FlowContext(Config::default()))->toArray() - ); - } - - public function test_array_push_transformer_when_there_is_no_array() : void - { - $transformer = Transform::array_push('array', [1]); - - $this->assertSame( - [ - [ - 'array' => [1], - ], - ], - $transformer->transform(new Rows(Row::create()), new FlowContext(Config::default()))->toArray() - ); - } -} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/CallbackEntryTransformerTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/CallbackEntryTransformerTest.php deleted file mode 100755 index 9e4394b40..000000000 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/CallbackEntryTransformerTest.php +++ /dev/null @@ -1,87 +0,0 @@ - new $entry(\trim($entry->name()), $entry->value()) - ); - - $rows = $callbackTransformer->transform( - new Rows( - Row::create( - new Entry\StringEntry('string entry ', 'String entry') - ) - ), - new FlowContext(Config::default()) - ); - - $this->assertEquals(new Rows( - Row::create( - new Entry\StringEntry('string entry', 'String entry') - ) - ), $rows); - } - - public function test_removing_whitespace_with_trim_callback_with_serialization() : void - { - $callbackTransformer = Transform::callback_entry( - fn (Entry $entry) : Entry => new $entry(\trim($entry->name()), $entry->value()) - ); - - $serialization = new NativePHPSerializer(); - - $rows = $serialization->unserialize($serialization->serialize($callbackTransformer))->transform( - new Rows( - Row::create( - new Entry\StringEntry('string entry ', 'String entry') - ) - ), - new FlowContext(Config::default()) - ); - - $this->assertEquals(new Rows( - Row::create( - new Entry\StringEntry('string entry', 'String entry') - ) - ), $rows); - } - - public function test_replacing_dashes_in_entry_name_with_str_replace_callback() : void - { - $callbackTransformer = Transform::callback_entry( - fn (Entry $entry) : Entry => new $entry(\str_replace('-', '_', $entry->name()), $entry->value()) - ); - - $rows = $callbackTransformer->transform( - new Rows( - Row::create( - new Row\Entry\IntegerEntry('old-int', 1000), - new Entry\StringEntry('string-entry ', 'String entry') - ) - ), - new FlowContext(Config::default()) - ); - - $this->assertEquals(new Rows( - Row::create( - new Row\Entry\IntegerEntry('old_int', 1000), - new Entry\StringEntry('string_entry ', 'String entry') - ) - ), $rows); - } -}