-
-
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 e5ede91
Showing
3 changed files
with
104 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,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; | ||
} | ||
|
||
return u($string)->before($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/StringBeforeTest.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 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') | ||
) | ||
) | ||
); | ||
} | ||
} |