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..9ccce0d02 --- /dev/null +++ b/src/core/etl/src/Flow/ETL/Function/StringLocaleTitle.php @@ -0,0 +1,43 @@ +string))->asString($row); + $locale = (new Parameter($this->locale))->asString($row); + + if ($string === null || $locale === null) { + return null; + } + + /** @phpstan-ignore-next-line */ + if (!method_exists(AbstractUnicodeString::class, 'localeTitle')) { + return u($string)->title()->toString(); + } + + 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..5d0ffd781 --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Unit/Function/StringLocaleTitleTest.php @@ -0,0 +1,66 @@ +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')) + ) + ); + } + + } +}