Skip to content

Commit 39f3d51

Browse files
authored
Added and/or/andNot/orNot to ScalarFunctionsChain (#1378)
1 parent 6e51340 commit 39f3d51

File tree

7 files changed

+87
-34
lines changed

7 files changed

+87
-34
lines changed

src/core/etl/src/Flow/ETL/Function/All.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,32 @@
66

77
use Flow\ETL\Row;
88

9-
final class All extends ScalarFunctionChain implements CompositeScalarFunction
9+
final readonly class All implements ScalarFunction
1010
{
1111
/**
1212
* @var array<ScalarFunction>
1313
*/
14-
private readonly array $refs;
14+
private array $functions;
1515

1616
public function __construct(
17-
ScalarFunction ...$refs,
17+
ScalarFunction ...$functions,
1818
) {
19-
$this->refs = $refs;
19+
$this->functions = $functions;
20+
}
21+
22+
public function and(ScalarFunction $scalarFunction) : self
23+
{
24+
return new self(...$this->functions, ...[$scalarFunction]);
25+
}
26+
27+
public function andNot(ScalarFunction $scalarFunction) : self
28+
{
29+
return new self(...$this->functions, ...[new Not($scalarFunction)]);
2030
}
2131

2232
public function eval(Row $row) : mixed
2333
{
24-
foreach ($this->refs as $ref) {
34+
foreach ($this->functions as $ref) {
2535
if (!$ref->eval($row)) {
2636
return false;
2737
}
@@ -30,8 +40,13 @@ public function eval(Row $row) : mixed
3040
return true;
3141
}
3242

33-
public function functions() : array
43+
public function or(ScalarFunction $scalarFunction) : Any
44+
{
45+
return new Any(...$this->functions, ...[$scalarFunction]);
46+
}
47+
48+
public function orNot(ScalarFunction $scalarFunction) : Any
3449
{
35-
return $this->refs;
50+
return new Any(...$this->functions, ...[new Not($scalarFunction)]);
3651
}
3752
}

src/core/etl/src/Flow/ETL/Function/Any.php

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,32 @@
66

77
use Flow\ETL\Row;
88

9-
final class Any extends ScalarFunctionChain implements CompositeScalarFunction
9+
final readonly class Any implements ScalarFunction
1010
{
1111
/**
1212
* @var array<ScalarFunction>
1313
*/
14-
private readonly array $refs;
14+
private array $functions;
1515

1616
public function __construct(
17-
ScalarFunction ...$values,
17+
ScalarFunction ...$functions,
1818
) {
19-
$this->refs = $values;
19+
$this->functions = $functions;
20+
}
21+
22+
public function and(ScalarFunction $scalarFunction) : All
23+
{
24+
return new All(...$this->functions, ...[$scalarFunction]);
25+
}
26+
27+
public function andNot(ScalarFunction $scalarFunction) : All
28+
{
29+
return new All(...$this->functions, ...[new Not($scalarFunction)]);
2030
}
2131

2232
public function eval(Row $row) : mixed
2333
{
24-
foreach ($this->refs as $ref) {
34+
foreach ($this->functions as $ref) {
2535
if ($ref->eval($row)) {
2636
return true;
2737
}
@@ -30,11 +40,13 @@ public function eval(Row $row) : mixed
3040
return false;
3141
}
3242

33-
/**
34-
* @return array<ScalarFunction>
35-
*/
36-
public function functions() : array
43+
public function or(ScalarFunction $scalarFunction) : self
44+
{
45+
return new self(...$this->functions, ...[$scalarFunction]);
46+
}
47+
48+
public function orNot(ScalarFunction $scalarFunction) : self
3749
{
38-
return $this->refs;
50+
return new self(...$this->functions, ...[new Not($scalarFunction)]);
3951
}
4052
}

src/core/etl/src/Flow/ETL/Function/CompositeScalarFunction.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515

1616
abstract class ScalarFunctionChain implements ScalarFunction
1717
{
18+
public function and(ScalarFunction $function) : All
19+
{
20+
return new All($this, $function);
21+
}
22+
23+
public function andNot(ScalarFunction $function) : All
24+
{
25+
return new All($this, new Not($function));
26+
}
27+
1828
public function arrayGet(ScalarFunction|string $path) : self
1929
{
2030
return new ArrayGet($this, $path);
@@ -319,6 +329,16 @@ public function onEach(ScalarFunction $function, ScalarFunction|bool $preserveKe
319329
return new OnEach($this, $function, $preserveKeys);
320330
}
321331

332+
public function or(ScalarFunction $function) : Any
333+
{
334+
return new Any($this, $function);
335+
}
336+
337+
public function orNot(ScalarFunction $function) : Any
338+
{
339+
return new Any($this, new Not($function));
340+
}
341+
322342
public function plus(ScalarFunction|int|float $ref) : self
323343
{
324344
return new Plus($this, $ref);

src/core/etl/tests/Flow/ETL/Tests/Integration/Function/AllTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Flow\ETL\Tests\Integration\Function;
66

7-
use function Flow\ETL\DSL\{all, from_array, lit, ref, to_memory, when};
7+
use function Flow\ETL\DSL\{from_array, lit, ref, to_memory, when};
88
use Flow\ETL\Flow;
99
use Flow\ETL\Memory\ArrayMemory;
1010
use Flow\ETL\Tests\FlowTestCase;
@@ -27,7 +27,7 @@ public function test_all_cases_found() : void
2727
->withEntry(
2828
'result',
2929
when(
30-
all(ref('id')->isEven(), ref('array')->exists()),
30+
ref('id')->isEven()->and(ref('array')->exists()),
3131
lit('found'),
3232
lit('not found')
3333
)

src/core/etl/tests/Flow/ETL/Tests/Integration/Function/AnyTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Flow\ETL\Tests\Integration\Function;
66

7-
use function Flow\ETL\DSL\{any, from_array, lit, ref, to_memory, when};
7+
use function Flow\ETL\DSL\{from_array, lit, ref, to_memory, when};
88
use Flow\ETL\Flow;
99
use Flow\ETL\Memory\ArrayMemory;
1010
use Flow\ETL\Tests\FlowTestCase;
@@ -27,7 +27,7 @@ public function test_any_case_found() : void
2727
->withEntry(
2828
'result',
2929
when(
30-
any(ref('id')->isEven(), ref('array')->exists()),
30+
ref('id')->isEven()->or(ref('array')->exists()),
3131
lit('found'),
3232
lit('not found')
3333
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flow\ETL\Tests\Unit\Function;
6+
7+
use function Flow\ETL\DSL\{int_entry, lit, ref, row};
8+
use Flow\ETL\Tests\FlowTestCase;
9+
10+
final class LogicalFunctionsTest extends FlowTestCase
11+
{
12+
public function test_logical_operations() : void
13+
{
14+
self::assertFalse(ref('id')->isEven()->andNot(ref('id')->equals(lit(1)))->eval(row(int_entry('id', 1))));
15+
self::assertTrue(ref('id')->isOdd()->and(ref('id')->equals(lit(1)))->eval(row(int_entry('id', 1))));
16+
self::assertTrue(ref('id')->isEven()->or(ref('id')->equals(lit(1)))->eval(row(int_entry('id', 1))));
17+
self::assertFalse(ref('id')->isOdd()->andNot(ref('id')->equals(lit(1)))->eval(row(int_entry('id', 1))));
18+
}
19+
}

0 commit comments

Comments
 (0)