Skip to content

Commit

Permalink
Add new Any & All expressions (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd authored Oct 5, 2023
1 parent 60fe06e commit b520a09
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/core/etl/src/Flow/ETL/DSL/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,16 @@ function array_sort(Expression $expression, \Closure $function = null) : Express
return new Expression\ArraySort($expression, $function ?? \Closure::fromCallable('sort'));
}

function all(Expression ...$expressions) : Expression
{
return new Expression\All(...$expressions);
}

function any(Expression ...$expressions) : Expression
{
return new Expression\Any(...$expressions);
}

function not(Expression $expression) : Expression
{
return new Expression\Not($expression);
Expand Down
33 changes: 33 additions & 0 deletions src/core/etl/src/Flow/ETL/Row/Reference/Expression/All.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Row\Reference\Expression;

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

final class All implements Expression
{
/**
* @var array<Expression>
*/
private readonly array $refs;

public function __construct(
Expression ...$refs,
) {
$this->refs = $refs;
}

public function eval(Row $row) : mixed
{
foreach ($this->refs as $ref) {
if (!$ref->eval($row)) {
return false;
}
}

return true;
}
}
33 changes: 33 additions & 0 deletions src/core/etl/src/Flow/ETL/Row/Reference/Expression/Any.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Row\Reference\Expression;

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

final class Any implements Expression
{
/**
* @var array<Expression>
*/
private readonly array $refs;

public function __construct(
Expression ...$refs,
) {
$this->refs = $refs;
}

public function eval(Row $row) : mixed
{
foreach ($this->refs as $ref) {
if ($ref->eval($row)) {
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

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

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

final class AllTest extends TestCase
{
public function test_all_expression_on_boolean_false_value() : void
{
$this->assertFalse(
all(lit(false))->eval(Row::create())
);
}

public function test_all_expression_on_boolean_true_value() : void
{
$this->assertTrue(
all(lit(true))->eval(Row::create())
);
}

public function test_all_expression_on_is_null_expression() : void
{
$this->assertTrue(
all(ref('value')->isNull())->eval(Row::create(Entry::null('value')))
);
}

public function test_all_expression_on_multiple_boolean_values() : void
{
$this->assertTrue(
all(lit(true), lit(true), lit(true))->eval(Row::create())
);
}

public function test_all_expression_on_multiple_random_boolean_values() : void
{
$this->assertFalse(
all(lit(true), lit(false), lit(true))->eval(Row::create())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

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

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

final class AnyTest extends TestCase
{
public function test_any_expression_on_boolean_false_value() : void
{
$this->assertFalse(
any(lit(false))->eval(Row::create())
);
}

public function test_any_expression_on_boolean_true_value() : void
{
$this->assertTrue(
any(lit(true))->eval(Row::create())
);
}

public function test_any_expression_on_is_null_expression() : void
{
$this->assertTrue(
any(ref('value')->isNull())->eval(Row::create(Entry::null('value')))
);
}

public function test_any_expression_on_multiple_boolean_values() : void
{
$this->assertTrue(
any(lit(false), lit(true), lit(false))->eval(Row::create())
);
}
}

0 comments on commit b520a09

Please sign in to comment.