-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9f4bb17
commit 386ed87
Showing
7 changed files
with
193 additions
and
7 deletions.
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
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
50 changes: 50 additions & 0 deletions
50
src/core/etl/src/Flow/ETL/Function/ConcatWithSeparator.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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Function; | ||
|
||
use function Flow\ETL\DSL\type_string; | ||
use Flow\ETL\PHP\Type\Caster; | ||
use Flow\ETL\Row; | ||
|
||
final class ConcatWithSeparator extends ScalarFunctionChain | ||
{ | ||
/** | ||
* @var array<ScalarFunction|string> | ||
*/ | ||
private readonly array $refs; | ||
|
||
public function __construct( | ||
private readonly ScalarFunction|string $separator, | ||
ScalarFunction|string ...$refs, | ||
) { | ||
$this->refs = $refs; | ||
} | ||
|
||
public function eval(Row $row) : mixed | ||
{ | ||
$separator = (new Parameter($this->separator))->asString($row); | ||
|
||
if (!\is_string($separator)) { | ||
return ''; | ||
} | ||
|
||
$values = \array_map(fn (ScalarFunction|string $string) : mixed => \is_string($string) ? $string : Caster::default()->to(type_string(true))->value($string->eval($row)), $this->refs); | ||
|
||
$concatValues = []; | ||
|
||
foreach ($values as $value) { | ||
if (\is_string($value)) { | ||
$concatValues[] = $value; | ||
} | ||
} | ||
|
||
if (\count($concatValues) === 0) { | ||
return ''; | ||
} | ||
|
||
/** @var array<string> $values */ | ||
return \implode($separator, $concatValues); | ||
} | ||
} |
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
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
87 changes: 87 additions & 0 deletions
87
src/core/etl/tests/Flow/ETL/Tests/Integration/Function/ConcatWithSeparatorTest.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,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Tests\Integration\Function; | ||
|
||
use function Flow\ETL\DSL\data_frame; | ||
use function Flow\ETL\DSL\{concat_ws, from_array, lit, ref, to_memory}; | ||
use Flow\ETL\Memory\ArrayMemory; | ||
use Flow\ETL\Tests\FlowTestCase; | ||
|
||
final class ConcatWithSeparatorTest extends FlowTestCase | ||
{ | ||
public function test_concat_on_non_string_value() : void | ||
{ | ||
(data_frame()) | ||
->read( | ||
from_array( | ||
[ | ||
['id' => 1], | ||
['id' => 2], | ||
] | ||
) | ||
) | ||
->withEntry('concat', concat_ws(lit(','), ref('id'), lit(null))) | ||
->write(to_memory($memory = new ArrayMemory())) | ||
->run(); | ||
|
||
self::assertSame( | ||
[ | ||
['id' => 1, 'concat' => '1'], | ||
['id' => 2, 'concat' => '2'], | ||
], | ||
$memory->dump() | ||
); | ||
} | ||
|
||
public function test_concat_on_nulls() : void | ||
{ | ||
(data_frame()) | ||
->read( | ||
from_array( | ||
[ | ||
['id' => 1, 'array' => ['field' => 'value']], | ||
['id' => 2], | ||
] | ||
) | ||
) | ||
->withEntry('concat', concat_ws(lit(null), lit(null))) | ||
->drop('array') | ||
->write(to_memory($memory = new ArrayMemory())) | ||
->run(); | ||
|
||
self::assertSame( | ||
[ | ||
['id' => 1, 'concat' => ''], | ||
['id' => 2, 'concat' => ''], | ||
], | ||
$memory->dump() | ||
); | ||
} | ||
|
||
public function test_concat_with_separator() : void | ||
{ | ||
(data_frame()) | ||
->read( | ||
from_array( | ||
[ | ||
['id' => 1], | ||
['id' => 2], | ||
] | ||
) | ||
) | ||
->withEntry('concat', concat_ws(lit('->'), lit('id'), ref('id'))) | ||
->drop('array') | ||
->write(to_memory($memory = new ArrayMemory())) | ||
->run(); | ||
|
||
self::assertSame( | ||
[ | ||
['id' => 1, 'concat' => 'id->1'], | ||
['id' => 2, 'concat' => 'id->2'], | ||
], | ||
$memory->dump() | ||
); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.