-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Title function with allWords parameter set default to false. (#1477)
Add Test for function with and without allWords and return type
- Loading branch information
1 parent
b0e1149
commit d03af95
Showing
3 changed files
with
86 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,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Flow\ETL\Function; | ||
|
||
use function Flow\ETL\DSL\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 StringTitle extends ScalarFunctionChain implements TypedScalarFunction | ||
{ | ||
public function __construct( | ||
private readonly ScalarFunction|string $string, | ||
private readonly ScalarFunction|bool $allWords = false, | ||
) { | ||
} | ||
|
||
public function eval(Row $row) : mixed | ||
{ | ||
$string = (new Parameter($this->string))->asString($row); | ||
$allWords = (new Parameter($this->allWords))->asBoolean($row); | ||
|
||
if ($string === null) { | ||
return null; | ||
} | ||
|
||
return u($string)->title(allWords: $allWords)->toString(); | ||
} | ||
|
||
public function returns() : Type | ||
{ | ||
return type_string(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringTitleTest.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,44 @@ | ||
<?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 StringTitleTest 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_title() : void | ||
{ | ||
self::assertSame( | ||
'Foo ijssel', | ||
ref('str')->stringTitle()->eval( | ||
row(str_entry('str', 'foo ijssel')) | ||
) | ||
); | ||
} | ||
|
||
public function test_string_title_allwords() : void | ||
{ | ||
self::assertSame( | ||
'Foo Ijssel', | ||
ref('str')->stringTitle(allWords: true)->eval( | ||
row(str_entry('str', 'foo ijssel')) | ||
) | ||
); | ||
} | ||
} |