From bcdde0eebb723c468960876122fe691691a9a57b Mon Sep 17 00:00:00 2001 From: f-lapinski Date: Fri, 14 Feb 2025 11:34:37 +0100 Subject: [PATCH 1/3] Add localTitle function with Test for 'en' and 'nl' --- .../Flow/ETL/Function/ScalarFunctionChain.php | 5 +++ .../Flow/ETL/Function/StringLocaleTitle.php | 37 ++++++++++++++++ .../Unit/Function/StringLocaleTitleTest.php | 44 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 src/core/etl/src/Flow/ETL/Function/StringLocaleTitle.php create mode 100644 src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringLocaleTitleTest.php diff --git a/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php b/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php index a89163b56..04e5fa06f 100644 --- a/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php +++ b/src/core/etl/src/Flow/ETL/Function/ScalarFunctionChain.php @@ -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); diff --git a/src/core/etl/src/Flow/ETL/Function/StringLocaleTitle.php b/src/core/etl/src/Flow/ETL/Function/StringLocaleTitle.php new file mode 100644 index 000000000..0287edd89 --- /dev/null +++ b/src/core/etl/src/Flow/ETL/Function/StringLocaleTitle.php @@ -0,0 +1,37 @@ +string))->asString($row); + $locale = (new Parameter($this->locale))->asString($row); + + if ($string === null || $locale === null) { + return null; + } + + return u($string)->localeTitle($locale)->toString(); + } + + public function returns() : Type + { + return type_string(); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringLocaleTitleTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringLocaleTitleTest.php new file mode 100644 index 000000000..c6220439d --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringLocaleTitleTest.php @@ -0,0 +1,44 @@ +returns(); + + self::assertInstanceOf(Type::class, $returnType); + + self::assertTrue($returnType->isEqual(type_string())); + } + + public function test_string_locale_title_en() : void + { + self::assertSame( + 'Foo ijssel', + ref('str')->stringLocaleTitle('en')->eval( + row(str_entry('str', 'foo ijssel')) + ) + ); + } + + public function test_string_locale_title_nl() : void + { + self::assertSame( + 'Foo IJssel', + ref('str')->stringLocaleTitle('nl')->eval( + row(str_entry('str', 'foo ijssel')) + ) + ); + } +} From 36a531e2e9aabda13bbbb0aeb7a874bb411ed5d9 Mon Sep 17 00:00:00 2001 From: f-lapinski Date: Fri, 14 Feb 2025 12:00:30 +0100 Subject: [PATCH 2/3] Fix for Symfony < 7.1 and replace localeTitle() for normal title() when method localTitle() doesn't exist --- .../Flow/ETL/Function/StringLocaleTitle.php | 6 +++ .../Unit/Function/StringLocaleTitleTest.php | 46 ++++++++++++++----- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/core/etl/src/Flow/ETL/Function/StringLocaleTitle.php b/src/core/etl/src/Flow/ETL/Function/StringLocaleTitle.php index 0287edd89..9ccce0d02 100644 --- a/src/core/etl/src/Flow/ETL/Function/StringLocaleTitle.php +++ b/src/core/etl/src/Flow/ETL/Function/StringLocaleTitle.php @@ -9,6 +9,7 @@ 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 { @@ -27,6 +28,11 @@ public function eval(Row $row) : mixed return null; } + /** @phpstan-ignore-next-line */ + if (!method_exists(AbstractUnicodeString::class, 'localeTitle')) { + return u($string)->title()->toString(); + } + return u($string)->localeTitle($locale)->toString(); } diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringLocaleTitleTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringLocaleTitleTest.php index c6220439d..b25921c3b 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringLocaleTitleTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringLocaleTitleTest.php @@ -9,6 +9,7 @@ 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 { @@ -24,21 +25,42 @@ public function test_returns_method_returns_string_type() : void public function test_string_locale_title_en() : void { - self::assertSame( - 'Foo ijssel', - ref('str')->stringLocaleTitle('en')->eval( - row(str_entry('str', 'foo ijssel')) - ) - ); + /** @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 { - self::assertSame( - 'Foo IJssel', - ref('str')->stringLocaleTitle('nl')->eval( - row(str_entry('str', 'foo ijssel')) - ) - ); + /** @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')) + ) + ); + } + } } From c845794dea9576d7c0b60e8b55f8eb4cc66a9c06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20=C5=81api=C5=84ski?= <143662975+f-lapinski@users.noreply.github.com> Date: Fri, 14 Feb 2025 12:06:37 +0100 Subject: [PATCH 3/3] Update StringLocaleTitleTest.php fix if statements --- .../Flow/ETL/Tests/Unit/Function/StringLocaleTitleTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringLocaleTitleTest.php b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringLocaleTitleTest.php index b25921c3b..5d0ffd781 100644 --- a/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringLocaleTitleTest.php +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringLocaleTitleTest.php @@ -26,7 +26,7 @@ public function test_returns_method_returns_string_type() : void public function test_string_locale_title_en() : void { /** @phpstan-ignore-next-line */ - if (!method_exists(AbstractUnicodeString::class, 'localeTitle')) { + if (method_exists(AbstractUnicodeString::class, 'localeTitle')) { self::assertSame( 'Foo ijssel', ref('str')->stringLocaleTitle('en')->eval( @@ -46,7 +46,7 @@ public function test_string_locale_title_en() : void public function test_string_locale_title_nl() : void { /** @phpstan-ignore-next-line */ - if (!method_exists(AbstractUnicodeString::class, 'localeTitle')) { + if (method_exists(AbstractUnicodeString::class, 'localeTitle')) { self::assertSame( 'Foo IJssel', ref('str')->stringLocaleTitle('nl')->eval(