diff --git a/src/core/etl/src/Flow/ETL/DSL/Transform.php b/src/core/etl/src/Flow/ETL/DSL/Transform.php index 1a81522cb..be026e867 100644 --- a/src/core/etl/src/Flow/ETL/DSL/Transform.php +++ b/src/core/etl/src/Flow/ETL/DSL/Transform.php @@ -81,11 +81,6 @@ final public static function array_push(string $array_entry, array $values = []) return new Transformer\ArrayPushTransformer($array_entry, $values); } - final public static function array_reverse(string $array_name) : Transformer - { - return new Transformer\ArrayReverseTransformer($array_name); - } - final public static function array_sort(string $array_name, int $sort_flag = \SORT_REGULAR) : Transformer { return new Transformer\ArraySortTransformer($array_name, $sort_flag); diff --git a/src/core/etl/src/Flow/ETL/Row/Reference/EntryExpression.php b/src/core/etl/src/Flow/ETL/Row/Reference/EntryExpression.php index 4ac14f437..23c46f574 100644 --- a/src/core/etl/src/Flow/ETL/Row/Reference/EntryExpression.php +++ b/src/core/etl/src/Flow/ETL/Row/Reference/EntryExpression.php @@ -63,6 +63,11 @@ public function arrayMergeCollection() : Expression|EntryReference return new Expressions(new Expression\ArrayMergeCollection($this)); } + public function arrayReverse(bool $preserveKeys = false) : Expression|EntryReference + { + return new Expressions(new Expression\ArrayReverse($this, $preserveKeys)); + } + public function arraySort(\Closure $function = null) : Expression|EntryReference { return new Expressions(new Expression\ArraySort($this, $function ?? \Closure::fromCallable('sort'))); diff --git a/src/core/etl/src/Flow/ETL/Row/Reference/Expression/ArrayReverse.php b/src/core/etl/src/Flow/ETL/Row/Reference/Expression/ArrayReverse.php new file mode 100644 index 000000000..a4d1a195d --- /dev/null +++ b/src/core/etl/src/Flow/ETL/Row/Reference/Expression/ArrayReverse.php @@ -0,0 +1,26 @@ +left->eval($row); + + if (!\is_array($left)) { + return null; + } + + return \array_reverse($left, $this->preserveKeys); + } +} diff --git a/src/core/etl/src/Flow/ETL/Transformer/ArrayReverseTransformer.php b/src/core/etl/src/Flow/ETL/Transformer/ArrayReverseTransformer.php deleted file mode 100644 index 1232f153e..000000000 --- a/src/core/etl/src/Flow/ETL/Transformer/ArrayReverseTransformer.php +++ /dev/null @@ -1,60 +0,0 @@ - - */ -final class ArrayReverseTransformer implements Transformer -{ - public function __construct(private readonly string $arrayEntryName) - { - } - - public function __serialize() : array - { - return [ - 'array_entry_name' => $this->arrayEntryName, - ]; - } - - public function __unserialize(array $data) : void - { - $this->arrayEntryName = $data['array_entry_name']; - } - - public function transform(Rows $rows, FlowContext $context) : Rows - { - $transformer = function (Row $row) : Row { - if (!$row->entries()->has($this->arrayEntryName)) { - throw new RuntimeException("\"{$this->arrayEntryName}\" not found"); - } - - if (!$row->entries()->get($this->arrayEntryName) instanceof Row\Entry\ArrayEntry) { - throw new RuntimeException("\"{$this->arrayEntryName}\" is not ArrayEntry"); - } - - $arrayEntry = $row->get($this->arrayEntryName); - - if (!$arrayEntry instanceof Row\Entry\ArrayEntry) { - throw new InvalidArgumentException("Entry {$this->arrayEntryName} is not a ArrayEntry"); - } - - return $row->set(new Row\Entry\ArrayEntry( - $arrayEntry->name(), - \array_reverse($arrayEntry->value()) - )); - }; - - return $rows->map($transformer); - } -} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Reference/Expression/ArrayReverseTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Reference/Expression/ArrayReverseTest.php new file mode 100644 index 000000000..c26ad56fd --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Reference/Expression/ArrayReverseTest.php @@ -0,0 +1,38 @@ +assertSame( + [5, 3, 10, 4], + ref('a')->arrayReverse() + ->eval( + Row::create( + Entry::array('a', [4, 10, 3, 5]), + ), + ) + ); + } + + public function test_array_reverse_non_array_entry() : void + { + $this->assertNull( + ref('a')->arrayReverse() + ->eval( + Row::create( + Entry::int('a', 123), + ), + ) + ); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/ArrayReverseTransformerTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/ArrayReverseTransformerTest.php deleted file mode 100644 index d82226f5b..000000000 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Transformer/ArrayReverseTransformerTest.php +++ /dev/null @@ -1,40 +0,0 @@ -assertSame( - [ - [ - 'array' => [4, 10, 3, 5], - ], - ], - $transformer->transform(new Rows(Row::create($arrayEntry)), new FlowContext(Config::default()))->toArray() - ); - } -}