Skip to content

Commit

Permalink
Rework ArraySort expression to work on callable not a \Closure
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Oct 6, 2023
1 parent 612e3e1 commit bd4b2a7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/core/etl/src/Flow/ETL/Row/Reference/EntryExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions src/core/etl/src/Flow/ETL/Row/Reference/Expression/ArraySort.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))))
);
}

Expand All @@ -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',
[
Expand Down Expand Up @@ -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',
[
Expand Down

0 comments on commit bd4b2a7

Please sign in to comment.