Skip to content

Commit

Permalink
Add Title function with allWords parameter set default to false. (#1477)
Browse files Browse the repository at this point in the history
Add Test for function with and without allWords and return type
  • Loading branch information
f-lapinski authored Feb 14, 2025
1 parent b0e1149 commit d03af95
Show file tree
Hide file tree
Showing 3 changed files with 86 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 @@ -492,6 +492,11 @@ public function stringStyle(ScalarFunction|string|StringStyles $style) : self
return new StringStyle($this, $style);
}

public function stringTitle(ScalarFunction|bool $allWords = false) : self
{
return new StringTitle($this, $allWords);
}

public function strPad(int $length, string $pad_string = ' ', int $type = STR_PAD_RIGHT) : self
{
return new StrPad($this, $length, $pad_string, $type);
Expand Down
37 changes: 37 additions & 0 deletions src/core/etl/src/Flow/ETL/Function/StringTitle.php
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();
}
}
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'))
)
);
}
}

0 comments on commit d03af95

Please sign in to comment.