Skip to content

Commit 2a779d0

Browse files
alex-bacartnicolas-grekas
authored andcommitted
[String] Fix of title() method
1 parent d9852f6 commit 2a779d0

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

AbstractUnicodeString.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,11 @@ public function title(bool $allWords = false): parent
348348
{
349349
$str = clone $this;
350350

351-
if ($allWords) {
352-
$str->string = preg_replace_callback('/\b./u', static function ($m) {
353-
return mb_convert_case($m[0], MB_CASE_TITLE, 'UTF-8');
354-
}, $str->string);
355-
} else {
356-
$firstChar = mb_substr($str->string, 0, 1, 'UTF-8');
357-
$str->string = mb_convert_case($firstChar, MB_CASE_TITLE, 'UTF-8').substr($str->string, \strlen($firstChar));
358-
}
351+
$limit = $allWords ? -1 : 1;
352+
353+
$str->string = preg_replace_callback('/\b./u', static function (array $m): string {
354+
return mb_convert_case($m[0], MB_CASE_TITLE, 'UTF-8');
355+
}, $str->string, $limit);
359356

360357
return $str;
361358
}

Tests/AbstractUnicodeTestCase.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,19 @@ public static function provideTitle(): array
292292
['DEJa', 'dEJa', false],
293293
['ΣσΣ', 'σσΣ', false],
294294
['Deja Σσς DEJa ΣσΣ', 'deja σσς dEJa σσΣ', true],
295+
296+
// Spanish
297+
['Última prueba', 'última prueba', false],
298+
['ÚLTIMA pRUEBA', 'úLTIMA pRUEBA', false],
299+
300+
['¡Hola spain!', '¡hola spain!', false],
301+
['¡HOLA sPAIN!', '¡hOLA sPAIN!', false],
302+
303+
['¡Hola Spain!', '¡hola spain!', true],
304+
['¡HOLA SPAIN!', '¡hOLA sPAIN!', true],
305+
306+
['Última Prueba', 'última prueba', true],
307+
['ÚLTIMA PRUEBA', 'úLTIMA pRUEBA', true],
295308
]
296309
);
297310
}

0 commit comments

Comments
 (0)