Skip to content

Commit

Permalink
Add new ref()->arrayReverse() entry expression (#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd authored Oct 5, 2023
1 parent df0fb89 commit f36414d
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 105 deletions.
5 changes: 0 additions & 5 deletions src/core/etl/src/Flow/ETL/DSL/Transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions src/core/etl/src/Flow/ETL/Row/Reference/EntryExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Row\Reference\Expression;

use Flow\ETL\Row;
use Flow\ETL\Row\Reference\Expression;

final class ArrayReverse implements Expression
{
public function __construct(private readonly Expression $left, private readonly bool $preserveKeys)
{
}

public function eval(Row $row) : mixed
{
$left = $this->left->eval($row);

if (!\is_array($left)) {
return null;
}

return \array_reverse($left, $this->preserveKeys);
}
}
60 changes: 0 additions & 60 deletions src/core/etl/src/Flow/ETL/Transformer/ArrayReverseTransformer.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Tests\Unit\Row\Reference\Expression;

use function Flow\ETL\DSL\ref;
use Flow\ETL\DSL\Entry;
use Flow\ETL\Row;
use PHPUnit\Framework\TestCase;

final class ArrayReverseTest extends TestCase
{
public function test_array_reverse_array_entry() : void
{
$this->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),
),
)
);
}
}

This file was deleted.

0 comments on commit f36414d

Please sign in to comment.