Skip to content

Commit

Permalink
Add StringAfter function with test
Browse files Browse the repository at this point in the history
  • Loading branch information
f-lapinski committed Feb 17, 2025
1 parent 0cdb662 commit 21a3a4d
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;

Check warning on line 27 in src/core/etl/src/Flow/ETL/Function/StringAfter.php

View check run for this annotation

Codecov / codecov/patch

src/core/etl/src/Flow/ETL/Function/StringAfter.php#L27

Added line #L27 was not covered by tests
}

$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

Check warning on line 36 in src/core/etl/src/Flow/ETL/Function/StringAfter.php

View check run for this annotation

Codecov / codecov/patch

src/core/etl/src/Flow/ETL/Function/StringAfter.php#L36

Added line #L36 was not covered by tests
{
return type_string();

Check warning on line 38 in src/core/etl/src/Flow/ETL/Function/StringAfter.php

View check run for this annotation

Codecov / codecov/patch

src/core/etl/src/Flow/ETL/Function/StringAfter.php#L38

Added line #L38 was not covered by tests
}
}
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 21a3a4d

Please sign in to comment.