Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add localTitle function with Test for 'en' and 'nl' #1481

Open
wants to merge 3 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -487,6 +487,11 @@ public function stringFold() : self
return new StringFold($this);
}

public function stringLocaleTitle(ScalarFunction|string $locale) : self
{
return new StringLocaleTitle($this, $locale);
}

public function stringStyle(ScalarFunction|string|StringStyles $style) : self
{
return new StringStyle($this, $style);
Expand Down
43 changes: 43 additions & 0 deletions src/core/etl/src/Flow/ETL/Function/StringLocaleTitle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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;
use Symfony\Component\String\AbstractUnicodeString;

final class StringLocaleTitle extends ScalarFunctionChain implements TypedScalarFunction
{
public function __construct(
private readonly ScalarFunction|string $string,
private readonly ScalarFunction|string $locale,
) {
}

public function eval(Row $row) : mixed
{
$string = (new Parameter($this->string))->asString($row);
$locale = (new Parameter($this->locale))->asString($row);

if ($string === null || $locale === null) {
return null;

Check warning on line 28 in src/core/etl/src/Flow/ETL/Function/StringLocaleTitle.php

View check run for this annotation

Codecov / codecov/patch

src/core/etl/src/Flow/ETL/Function/StringLocaleTitle.php#L28

Added line #L28 was not covered by tests
}

/** @phpstan-ignore-next-line */
if (!method_exists(AbstractUnicodeString::class, 'localeTitle')) {
return u($string)->title()->toString();

Check warning on line 33 in src/core/etl/src/Flow/ETL/Function/StringLocaleTitle.php

View check run for this annotation

Codecov / codecov/patch

src/core/etl/src/Flow/ETL/Function/StringLocaleTitle.php#L33

Added line #L33 was not covered by tests
}

return u($string)->localeTitle($locale)->toString();
}

public function returns() : Type
{
return type_string();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?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\StringLocaleTitle;
use Flow\ETL\PHP\Type\Type;
use Flow\ETL\Tests\FlowTestCase;
use Symfony\Component\String\AbstractUnicodeString;

final class StringLocaleTitleTest extends FlowTestCase
{
public function test_returns_method_returns_string_type() : void
{
$stringTitleFunction = new StringLocaleTitle('str', 'en');
$returnType = $stringTitleFunction->returns();

self::assertInstanceOf(Type::class, $returnType);

self::assertTrue($returnType->isEqual(type_string()));
}

public function test_string_locale_title_en() : void
{
/** @phpstan-ignore-next-line */
if (method_exists(AbstractUnicodeString::class, 'localeTitle')) {
self::assertSame(
'Foo ijssel',
ref('str')->stringLocaleTitle('en')->eval(
row(str_entry('str', 'foo ijssel'))
)
);
} else {
self::assertSame(
'Foo ijssel',
ref('str')->stringTitle()->eval(
row(str_entry('str', 'foo ijssel'))
)
);
}
}

public function test_string_locale_title_nl() : void
{
/** @phpstan-ignore-next-line */
if (method_exists(AbstractUnicodeString::class, 'localeTitle')) {
self::assertSame(
'Foo IJssel',
ref('str')->stringLocaleTitle('nl')->eval(
row(str_entry('str', 'foo ijssel'))
)
);
} else {
self::assertNotSame(
'Foo IJssel',
ref('str')->stringTitle()->eval(
row(str_entry('str', 'foo ijssel'))
)
);
}

}
}
Loading