diff --git a/src/core/etl/src/Flow/ETL/Function/All.php b/src/core/etl/src/Flow/ETL/Function/All.php index e68ffa26d..f2ce678a3 100644 --- a/src/core/etl/src/Flow/ETL/Function/All.php +++ b/src/core/etl/src/Flow/ETL/Function/All.php @@ -6,22 +6,32 @@ use Flow\ETL\Row; -final class All extends ScalarFunctionChain implements CompositeScalarFunction +final readonly class All implements ScalarFunction { /** * @var array */ - private readonly array $refs; + private array $functions; public function __construct( - ScalarFunction ...$refs, + ScalarFunction ...$functions, ) { - $this->refs = $refs; + $this->functions = $functions; + } + + public function and(ScalarFunction $scalarFunction) : self + { + return new self(...$this->functions, ...[$scalarFunction]); + } + + public function andNot(ScalarFunction $scalarFunction) : self + { + return new self(...$this->functions, ...[new Not($scalarFunction)]); } public function eval(Row $row) : mixed { - foreach ($this->refs as $ref) { + foreach ($this->functions as $ref) { if (!$ref->eval($row)) { return false; } @@ -30,8 +40,13 @@ public function eval(Row $row) : mixed return true; } - public function functions() : array + public function or(ScalarFunction $scalarFunction) : Any + { + return new Any(...$this->functions, ...[$scalarFunction]); + } + + public function orNot(ScalarFunction $scalarFunction) : Any { - return $this->refs; + return new Any(...$this->functions, ...[new Not($scalarFunction)]); } } diff --git a/src/core/etl/src/Flow/ETL/Function/Any.php b/src/core/etl/src/Flow/ETL/Function/Any.php index 79825323c..380452c10 100644 --- a/src/core/etl/src/Flow/ETL/Function/Any.php +++ b/src/core/etl/src/Flow/ETL/Function/Any.php @@ -6,22 +6,32 @@ use Flow\ETL\Row; -final class Any extends ScalarFunctionChain implements CompositeScalarFunction +final readonly class Any implements ScalarFunction { /** * @var array */ - private readonly array $refs; + private array $functions; public function __construct( - ScalarFunction ...$values, + ScalarFunction ...$functions, ) { - $this->refs = $values; + $this->functions = $functions; + } + + public function and(ScalarFunction $scalarFunction) : All + { + return new All(...$this->functions, ...[$scalarFunction]); + } + + public function andNot(ScalarFunction $scalarFunction) : All + { + return new All(...$this->functions, ...[new Not($scalarFunction)]); } public function eval(Row $row) : mixed { - foreach ($this->refs as $ref) { + foreach ($this->functions as $ref) { if ($ref->eval($row)) { return true; } @@ -30,11 +40,13 @@ public function eval(Row $row) : mixed return false; } - /** - * @return array - */ - public function functions() : array + public function or(ScalarFunction $scalarFunction) : self + { + return new self(...$this->functions, ...[$scalarFunction]); + } + + public function orNot(ScalarFunction $scalarFunction) : self { - return $this->refs; + return new self(...$this->functions, ...[new Not($scalarFunction)]); } } diff --git a/src/core/etl/src/Flow/ETL/Function/CompositeScalarFunction.php b/src/core/etl/src/Flow/ETL/Function/CompositeScalarFunction.php deleted file mode 100644 index d8f7fa0b0..000000000 --- a/src/core/etl/src/Flow/ETL/Function/CompositeScalarFunction.php +++ /dev/null @@ -1,13 +0,0 @@ - - */ - public function functions() : array; -} diff --git a/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php b/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php index 41b7e6006..945c46f86 100644 --- a/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php +++ b/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php @@ -15,6 +15,16 @@ abstract class ScalarFunctionChain implements ScalarFunction { + public function and(ScalarFunction $function) : All + { + return new All($this, $function); + } + + public function andNot(ScalarFunction $function) : All + { + return new All($this, new Not($function)); + } + public function arrayGet(ScalarFunction|string $path) : self { return new ArrayGet($this, $path); @@ -319,6 +329,16 @@ public function onEach(ScalarFunction $function, ScalarFunction|bool $preserveKe return new OnEach($this, $function, $preserveKeys); } + public function or(ScalarFunction $function) : Any + { + return new Any($this, $function); + } + + public function orNot(ScalarFunction $function) : Any + { + return new Any($this, new Not($function)); + } + public function plus(ScalarFunction|int|float $ref) : self { return new Plus($this, $ref); diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Function/AllTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Function/AllTest.php index d174aa066..10caac927 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Integration/Function/AllTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Function/AllTest.php @@ -4,7 +4,7 @@ namespace Flow\ETL\Tests\Integration\Function; -use function Flow\ETL\DSL\{all, from_array, lit, ref, to_memory, when}; +use function Flow\ETL\DSL\{from_array, lit, ref, to_memory, when}; use Flow\ETL\Flow; use Flow\ETL\Memory\ArrayMemory; use Flow\ETL\Tests\FlowTestCase; @@ -27,7 +27,7 @@ public function test_all_cases_found() : void ->withEntry( 'result', when( - all(ref('id')->isEven(), ref('array')->exists()), + ref('id')->isEven()->and(ref('array')->exists()), lit('found'), lit('not found') ) diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Function/AnyTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Function/AnyTest.php index 5436f8e93..a0a265f1f 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Integration/Function/AnyTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Function/AnyTest.php @@ -4,7 +4,7 @@ namespace Flow\ETL\Tests\Integration\Function; -use function Flow\ETL\DSL\{any, from_array, lit, ref, to_memory, when}; +use function Flow\ETL\DSL\{from_array, lit, ref, to_memory, when}; use Flow\ETL\Flow; use Flow\ETL\Memory\ArrayMemory; use Flow\ETL\Tests\FlowTestCase; @@ -27,7 +27,7 @@ public function test_any_case_found() : void ->withEntry( 'result', when( - any(ref('id')->isEven(), ref('array')->exists()), + ref('id')->isEven()->or(ref('array')->exists()), lit('found'), lit('not found') ) diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/LogicalFunctionsTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/LogicalFunctionsTest.php new file mode 100644 index 000000000..585d5aee9 --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/LogicalFunctionsTest.php @@ -0,0 +1,19 @@ +isEven()->andNot(ref('id')->equals(lit(1)))->eval(row(int_entry('id', 1)))); + self::assertTrue(ref('id')->isOdd()->and(ref('id')->equals(lit(1)))->eval(row(int_entry('id', 1)))); + self::assertTrue(ref('id')->isEven()->or(ref('id')->equals(lit(1)))->eval(row(int_entry('id', 1)))); + self::assertFalse(ref('id')->isOdd()->andNot(ref('id')->equals(lit(1)))->eval(row(int_entry('id', 1)))); + } +}