Skip to content

Commit

Permalink
Add StringAfter function with test (#1485)
Browse files Browse the repository at this point in the history
  • Loading branch information
f-lapinski authored Feb 18, 2025
1 parent 3fd83c9 commit 0899a27
Show file tree
Hide file tree
Showing 3 changed files with 105 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 @@ -482,6 +482,11 @@ public function startsWith(ScalarFunction|string $needle) : self
return new StartsWith($this, $needle);
}

public function stringAfter(ScalarFunction|string $needle, ScalarFunction|bool $includeNeedle = false) : self
{
return new StringAfter($this, $needle, $includeNeedle);
}

public function stringFold() : self
{
return new StringFold($this);
Expand Down
40 changes: 40 additions & 0 deletions src/core/etl/src/Flow/ETL/Function/StringAfter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Function;

use function Flow\ETL\DSL\{type_list, 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 StringAfter extends ScalarFunctionChain implements TypedScalarFunction
{
public function __construct(
private readonly ScalarFunction|string $string,
private readonly ScalarFunction|string $needle,
private readonly ScalarFunction|bool $includeNeedle = false,
) {
}

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

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

$needle = (new Parameter($this->needle))->as($row, type_string(), type_list(type_string()));
$includeNeedle = (new Parameter($this->includeNeedle))->asBoolean($row);

return u($string)->after($needle, $includeNeedle)->toString();
}

public function returns() : Type
{
return type_string();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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, type_string};
use Flow\ETL\Function\StringTitle;
use Flow\ETL\PHP\Type\Type;
use Flow\ETL\Tests\FlowTestCase;

final class StringAfterTest extends FlowTestCase
{
public function test_returns_method_returns_string_type() : void
{
$stringTitleFunction = new StringTitle('str');
$returnType = $stringTitleFunction->returns();

self::assertInstanceOf(Type::class, $returnType);

self::assertTrue($returnType->isEqual(type_string()));
}

public function test_string_after() : void
{
self::assertSame(
' world',
ref('str')->stringAfter(ref('needle'))->eval(
row(
str_entry('str', 'hello world'),
str_entry('needle', 'hello')
)
)
);

self::assertSame(
' world',
ref('str')->stringAfter(ref('needle'))->eval(
row(
str_entry('str', 'hello world'),
str_entry('needle', 'o')
)
)
);
}

public function test_string_after_including_needle() : void
{
self::assertSame(
'o world',
ref('str')->stringAfter(ref('needle'), includeNeedle: true)->eval(
row(
str_entry('str', 'hello world'),
str_entry('needle', 'o')
)
)
);
}
}

0 comments on commit 0899a27

Please sign in to comment.