-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added onEach scalar function (#1168)
- Loading branch information
1 parent
75fa0e1
commit 897a13b
Showing
4 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Function; | ||
|
||
use function Flow\ETL\DSL\array_to_row; | ||
use Flow\ETL\Exception\InvalidArgumentException; | ||
use Flow\ETL\Row; | ||
|
||
final class OnEach extends ScalarFunctionChain | ||
{ | ||
public function __construct( | ||
private readonly ScalarFunction $ref, | ||
private readonly ScalarFunction $function, | ||
private readonly ScalarFunction|bool $preserveKeys = true | ||
) { | ||
} | ||
|
||
public function eval(Row $row) : mixed | ||
{ | ||
$value = $this->ref->eval($row); | ||
|
||
if (!\is_array($value)) { | ||
return null; | ||
} | ||
|
||
$preserveKeys = \is_bool($this->preserveKeys) ? $this->preserveKeys : (bool) $this->preserveKeys->eval($row); | ||
|
||
$output = []; | ||
|
||
foreach ($value as $key => $item) { | ||
if ($preserveKeys) { | ||
try { | ||
$output[$key] = $this->function->eval(array_to_row(['element' => $item])); | ||
} catch (InvalidArgumentException $e) { | ||
$output[$key] = null; | ||
} | ||
} else { | ||
try { | ||
$output[] = $this->function->eval(array_to_row(['element' => $item])); | ||
} catch (InvalidArgumentException $e) { | ||
$output[] = null; | ||
} | ||
} | ||
} | ||
|
||
return $output; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/core/etl/tests/Flow/ETL/Tests/Integration/Function/OnEachTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Tests\Integration\Function; | ||
|
||
use function Flow\ETL\DSL\{df, from_array, ref, type_string}; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class OnEachTest extends TestCase | ||
{ | ||
public function test_on_each_function() : void | ||
{ | ||
$results = df() | ||
->read(from_array([ | ||
['array' => ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5]], | ||
['array' => ['f' => 1, 'g' => 2.3, 'h' => 3, 'i' => 4, 'j' => null]], | ||
])) | ||
->withEntry('array', ref('array')->onEach(ref('element')->cast(type_string()))) | ||
->fetch() | ||
->toArray(); | ||
|
||
self::assertEquals( | ||
[ | ||
['array' => ['a' => '1', 'b' => '2', 'c' => '3', 'd' => '4', 'e' => '5']], | ||
['array' => ['f' => '1', 'g' => '2.3', 'h' => '3', 'i' => '4', 'j' => null]], | ||
], | ||
$results | ||
); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
src/core/etl/tests/Flow/ETL/Tests/Unit/Function/OnEachTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Tests\Unit\Function; | ||
|
||
use function Flow\ETL\DSL\{array_entry, ref, row, type_string}; | ||
use Flow\ETL\Adapter\Elasticsearch\Tests\Integration\TestCase; | ||
|
||
final class OnEachTest extends TestCase | ||
{ | ||
public function test_executing_function_on_each_value_from_array() : void | ||
{ | ||
self::assertSame( | ||
['1', '2', '3', '4', '5'], | ||
ref('array')->onEach(ref('element')->cast(type_string())) | ||
->eval( | ||
row( | ||
array_entry( | ||
'array', | ||
[1, 2, 3, 4, 5] | ||
) | ||
) | ||
), | ||
); | ||
} | ||
|
||
public function test_executing_function_on_each_value_from_empty_array() : void | ||
{ | ||
self::assertSame( | ||
[], | ||
ref('array')->onEach(ref('element')->cast(type_string())) | ||
->eval( | ||
row( | ||
array_entry( | ||
'array', | ||
[] | ||
) | ||
) | ||
), | ||
); | ||
} | ||
|
||
public function test_executing_function_on_each_value_with_preserving_keys() : void | ||
{ | ||
self::assertSame( | ||
['a' => '1', 'b' => '2', 'c' => '3', 'd' => '4', 'e' => '5'], | ||
ref('array')->onEach(ref('element')->cast(type_string()), true) | ||
->eval( | ||
row( | ||
array_entry( | ||
'array', | ||
['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5] | ||
) | ||
) | ||
), | ||
); | ||
} | ||
|
||
public function test_executing_function_on_each_value_without_preserving_keys() : void | ||
{ | ||
self::assertSame( | ||
['1', '2', '3', '4', '5'], | ||
ref('array')->onEach(ref('element')->cast(type_string()), false) | ||
->eval( | ||
row( | ||
array_entry( | ||
'array', | ||
['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5] | ||
) | ||
) | ||
), | ||
); | ||
} | ||
} |