Skip to content

Commit

Permalink
bug #34048 [String] Fix of title() method (alex-bacart)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 5.0-dev branch.

Discussion
----------

[String] Fix of title() method

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #34045
| License       | MIT

Fix of `title()` method for Spanish

Commits
-------

af0f541 [String] Fix of title() method
  • Loading branch information
nicolas-grekas committed Oct 21, 2019
2 parents 14dda41 + af0f541 commit df1bb4e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/Symfony/Component/String/AbstractUnicodeString.php
Expand Up @@ -348,14 +348,11 @@ public function title(bool $allWords = false): parent
{
$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;
}
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/String/Tests/AbstractUnicodeTestCase.php
Expand Up @@ -292,6 +292,19 @@ public static function provideTitle(): array
['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],
]
);
}
Expand Down

0 comments on commit df1bb4e

Please sign in to comment.