Skip to content

Commit

Permalink
feat: create Folded Scalar function with Test (#1449)
Browse files Browse the repository at this point in the history
* feat: create Folded Scalar function with Test

* Add Folded ScalarFunction

* Remove from DSL and fix to ScalarFunctionChain
  • Loading branch information
f-lapinski authored Feb 6, 2025
1 parent efc9369 commit 75fbfea
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,11 @@ public function startsWith(ScalarFunction|string $needle) : self
return new StartsWith($this, $needle);
}

public function stringFold() : self
{
return new StringFold($this);
}

public function strPad(int $length, string $pad_string = ' ', int $type = STR_PAD_RIGHT) : self
{
return new StrPad($this, $length, $pad_string, $type);
Expand Down
34 changes: 34 additions & 0 deletions src/core/etl/src/Flow/ETL/Function/StringFold.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Function;

use function Flow\ETL\DSL\type_string;
use function Symfony\Component\String\u;
use Flow\ETL\Function\ScalarFunction\TypedScalarFunction;
use Flow\ETL\PHP\Type\Type;
use Flow\ETL\Row;

final class StringFold extends ScalarFunctionChain implements TypedScalarFunction
{
public function __construct(private readonly ScalarFunction|string $string)
{
}

public function eval(Row $row) : mixed
{
$string = (new Parameter($this->string))->asString($row);

if ($string === null) {
return null;
}

return u($string)->folded()->toString();
}

public function returns() : Type
{
return type_string();
}
}
22 changes: 22 additions & 0 deletions src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringFoldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Tests\Unit\Function;

use function Flow\ETL\DSL\row;
use function Flow\ETL\DSL\{ref, str_entry};
use Flow\ETL\Tests\FlowTestCase;

final class StringFoldTest extends FlowTestCase
{
public function test_string_folded() : void
{
self::assertSame(
"die o'brian strasse",
ref('str')->stringFold()->eval(
row(str_entry('str', "Die O'Brian Straße"))
)
);
}
}

0 comments on commit 75fbfea

Please sign in to comment.