[String] Fix of title() method

This commit is contained in:
Alex Bacart 2019-10-21 17:41:38 +03:00 committed by Nicolas Grekas
parent 14dda4160e
commit af0f5419bb
2 changed files with 18 additions and 8 deletions

View File

@ -348,14 +348,11 @@ abstract class AbstractUnicodeString extends AbstractString
{
$str = clone $this;
if ($allWords) {
$str->string = preg_replace_callback('/\b./u', static function ($m) {
return mb_convert_case($m[0], MB_CASE_TITLE, 'UTF-8');
}, $str->string);
} else {
$firstChar = mb_substr($str->string, 0, 1, 'UTF-8');
$str->string = mb_convert_case($firstChar, MB_CASE_TITLE, 'UTF-8').substr($str->string, \strlen($firstChar));
}
$limit = $allWords ? -1 : 1;
$str->string = preg_replace_callback('/\b./u', static function (array $m): string {
return mb_convert_case($m[0], MB_CASE_TITLE, 'UTF-8');
}, $str->string, $limit);
return $str;
}

View File

@ -292,6 +292,19 @@ abstract class AbstractUnicodeTestCase extends AbstractAsciiTestCase
['DEJa', 'dEJa', false],
['ΣσΣ', 'σσΣ', false],
['Deja Σσς DEJa ΣσΣ', 'deja σσς dEJa σσΣ', true],
// Spanish
['Última prueba', 'última prueba', false],
['ÚLTIMA pRUEBA', 'úLTIMA pRUEBA', false],
['¡Hola spain!', '¡hola spain!', false],
['¡HOLA sPAIN!', '¡hOLA sPAIN!', false],
['¡Hola Spain!', '¡hola spain!', true],
['¡HOLA SPAIN!', '¡hOLA sPAIN!', true],
['Última Prueba', 'última prueba', true],
['ÚLTIMA PRUEBA', 'úLTIMA pRUEBA', true],
]
);
}