-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
0cdb662
commit 21a3a4d
Showing
3 changed files
with
105 additions
and
0 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
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(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringAfterTest.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,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') | ||
) | ||
) | ||
); | ||
} | ||
} |