From bd4b2a7eb6166ea254aec8e137d1f06ab0a94b68 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Fri, 6 Oct 2023 10:48:45 +0200 Subject: [PATCH] Rework `ArraySort` expression to work on callable not a `\Closure` --- .../Flow/ETL/Row/Reference/EntryExpression.php | 4 ++-- .../ETL/Row/Reference/Expression/ArraySort.php | 17 ++++++++++++----- .../Row/Reference/Expression/ArraySortTest.php | 8 ++++---- 3 files changed, 18 insertions(+), 11 deletions(-) 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 23c46f574..311cb97ac 100644 --- a/src/core/etl/src/Flow/ETL/Row/Reference/EntryExpression.php +++ b/src/core/etl/src/Flow/ETL/Row/Reference/EntryExpression.php @@ -68,9 +68,9 @@ public function arrayReverse(bool $preserveKeys = false) : Expression|EntryRefer return new Expressions(new Expression\ArrayReverse($this, $preserveKeys)); } - public function arraySort(\Closure $function = null) : Expression|EntryReference + public function arraySort(callable $function = null) : Expression|EntryReference { - return new Expressions(new Expression\ArraySort($this, $function ?? \Closure::fromCallable('sort'))); + return new Expressions(new Expression\ArraySort($this, $function ?: 'sort')); } public function cast(string $type) : Expression|EntryReference diff --git a/src/core/etl/src/Flow/ETL/Row/Reference/Expression/ArraySort.php b/src/core/etl/src/Flow/ETL/Row/Reference/Expression/ArraySort.php index 9b7f0fdfc..c86f9d6d5 100644 --- a/src/core/etl/src/Flow/ETL/Row/Reference/Expression/ArraySort.php +++ b/src/core/etl/src/Flow/ETL/Row/Reference/Expression/ArraySort.php @@ -9,10 +9,17 @@ final class ArraySort implements Expression { + /** + * @var callable + */ + private $function; + public function __construct( private readonly Expression $ref, - private readonly \Closure $function + callable $function, + private readonly bool $recursive = true ) { + $this->function = $function; } public function eval(Row $row) : mixed @@ -24,17 +31,17 @@ public function eval(Row $row) : mixed return null; } - $this->recursiveSort($val, $this->function); + $this->recursiveSort($val, $this->function, $this->recursive); return $val; } - private function recursiveSort(array &$array, \Closure $function) : void + private function recursiveSort(array &$array, callable $function, bool $recursive) : void { /** @var mixed $value */ foreach ($array as &$value) { - if (\is_array($value)) { - $this->recursiveSort($value, $function); + if ($recursive && \is_array($value)) { + $this->recursiveSort($value, $function, true); } } diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Reference/Expression/ArraySortTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Reference/Expression/ArraySortTest.php index 5697608f5..028455101 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Reference/Expression/ArraySortTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Row/Reference/Expression/ArraySortTest.php @@ -14,8 +14,8 @@ final class ArraySortTest extends TestCase public function test_sorting_big_arrays() : void { $this->assertSame( - ref('array')->arraySort(\Closure::fromCallable('sort'))->eval(Row::create(Entry::array('array', \json_decode($this->jsonDifferentOrder(), true, 512, JSON_THROW_ON_ERROR)))), - ref('array')->arraySort(\Closure::fromCallable('sort'))->eval(Row::create(Entry::array('array', \json_decode($this->json(), true, 512, JSON_THROW_ON_ERROR)))) + ref('array')->arraySort('sort')->eval(Row::create(Entry::array('array', \json_decode($this->jsonDifferentOrder(), true, 512, JSON_THROW_ON_ERROR)))), + ref('array')->arraySort('sort')->eval(Row::create(Entry::array('array', \json_decode($this->json(), true, 512, JSON_THROW_ON_ERROR)))) ); } @@ -31,7 +31,7 @@ public function test_sorting_nested_array_using_asort_algo() : void ], ], ], - ref('array')->arraySort(\Closure::fromCallable('asort'))->eval(Row::create( + ref('array')->arraySort('asort')->eval(Row::create( Entry::array( 'array', [ @@ -60,7 +60,7 @@ public function test_sorting_nested_associative_array() : void 'g' => 'h', ], ], - ref('array')->arraySort(\Closure::fromCallable('ksort'))->eval(Row::create( + ref('array')->arraySort('ksort')->eval(Row::create( Entry::array( 'array', [