-
-
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.
Showing
5 changed files
with
127 additions
and
4 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 StringBeforeLast 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) : ?string | ||
{ | ||
$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)->beforeLast($needle, $includeNeedle)->toString(); | ||
} | ||
|
||
public function returns() : Type | ||
{ | ||
return type_string(); | ||
} | ||
} |
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
75 changes: 75 additions & 0 deletions
75
src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringBeforeLastTest.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,75 @@ | ||
<?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\StringBeforeLast; | ||
use Flow\ETL\PHP\Type\Type; | ||
use Flow\ETL\Tests\FlowTestCase; | ||
|
||
final class StringBeforeLastTest extends FlowTestCase | ||
{ | ||
public function test_returns_method_returns_string_type() : void | ||
{ | ||
$stringBeforeLastFunction = new StringBeforeLast('str', 't', false); | ||
$returnType = $stringBeforeLastFunction->returns(); | ||
|
||
self::assertInstanceOf(Type::class, $returnType); | ||
|
||
self::assertTrue($returnType->isEqual(type_string())); | ||
} | ||
|
||
public function test_string_before_last() : void | ||
{ | ||
self::assertSame( | ||
'hello w', | ||
ref('str')->stringBeforeLast(ref('needle'))->eval( | ||
row( | ||
str_entry('str', 'hello world'), | ||
str_entry('needle', 'o') | ||
) | ||
) | ||
); | ||
} | ||
|
||
public function test_string_before_last_including_needle() : void | ||
{ | ||
self::assertSame( | ||
'hello wo', | ||
ref('str')->stringBeforeLast(ref('needle'), includeNeedle: true)->eval( | ||
row( | ||
str_entry('str', 'hello world'), | ||
str_entry('needle', 'o') | ||
) | ||
) | ||
); | ||
} | ||
|
||
public function test_string_before_last_returns_empty_string() : void | ||
{ | ||
self::assertSame( | ||
'', | ||
ref('str')->stringBeforeLast(ref('needle'))->eval( | ||
row( | ||
str_entry('str', ''), | ||
str_entry('needle', 'o') | ||
) | ||
) | ||
); | ||
} | ||
|
||
public function test_string_before_last_returns_null() : void | ||
{ | ||
self::assertNull( | ||
ref('str')->stringBeforeLast(ref('needle'))->eval( | ||
row( | ||
str_entry('str', null), | ||
str_entry('needle', 'o') | ||
) | ||
) | ||
); | ||
} | ||
} |
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