Skip to content

Commit

Permalink
Add StringBefore 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 e5ede91
Show file tree
Hide file tree
Showing 3 changed files with 104 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 stringBefore(ScalarFunction|string $needle, ScalarFunction|bool $includeNeedle = false) : self
{
return new StringBefore($this, $needle, $includeNeedle);
}

public function stringFold() : self
{
return new StringFold($this);
Expand Down
39 changes: 39 additions & 0 deletions src/core/etl/src/Flow/ETL/Function/StringBefore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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 StringBefore 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);
$needle = (new Parameter($this->needle))->as($row, type_string(), type_list(type_string()));
$includeNeedle = (new Parameter($this->includeNeedle))->asBoolean($row);

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

Check warning on line 29 in src/core/etl/src/Flow/ETL/Function/StringBefore.php

View check run for this annotation

Codecov / codecov/patch

src/core/etl/src/Flow/ETL/Function/StringBefore.php#L29

Added line #L29 was not covered by tests
}

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

public function returns() : Type

Check warning on line 35 in src/core/etl/src/Flow/ETL/Function/StringBefore.php

View check run for this annotation

Codecov / codecov/patch

src/core/etl/src/Flow/ETL/Function/StringBefore.php#L35

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

Check warning on line 37 in src/core/etl/src/Flow/ETL/Function/StringBefore.php

View check run for this annotation

Codecov / codecov/patch

src/core/etl/src/Flow/ETL/Function/StringBefore.php#L37

Added line #L37 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 StringBeforeTest 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_before() : void
{
self::assertSame(
'hello ',
ref('str')->stringBefore(ref('needle'))->eval(
row(
str_entry('str', 'hello world'),
str_entry('needle', 'world')
)
)
);

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

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

0 comments on commit e5ede91

Please sign in to comment.