fix special-chars in titleize - fixes #732

This commit is contained in:
Andy Miller 2024-01-19 12:39:24 +00:00
parent 0d7cd64d0d
commit 0b16401a91
No known key found for this signature in database
GPG Key ID: 9F2CF38AEBDB0AE0
2 changed files with 11 additions and 3 deletions

View File

@ -161,9 +161,15 @@ class Inflector
*/
public static function titleize($word, $uppercase = '')
{
$uppercase = $uppercase === 'first' ? 'ucfirst' : 'ucwords';
$humanize_underscorize = static::humanize(static::underscorize($word));
if ($uppercase === 'first') {
$firstLetter = mb_strtoupper(mb_substr($humanize_underscorize, 0, 1, "UTF-8"), "UTF-8");
return $firstLetter . mb_substr($humanize_underscorize, 1, mb_strlen($humanize_underscorize, "UTF-8"), "UTF-8");
} else {
return mb_convert_case($humanize_underscorize, MB_CASE_TITLE, 'UTF-8');
}
return $uppercase(static::humanize(static::underscorize($word)));
}
/**
@ -198,7 +204,7 @@ class Inflector
{
$regex1 = preg_replace('/([A-Z]+)([A-Z][a-z])/', '\1_\2', $word);
$regex2 = preg_replace('/([a-zd])([A-Z])/', '\1_\2', $regex1);
$regex3 = preg_replace('/[^A-Z^a-z^0-9]+/', '_', $regex2);
$regex3 = preg_replace('/[^\p{L}^0-9]+/u', '_', $regex2);
return strtolower($regex3);
}

View File

@ -53,11 +53,13 @@ class InflectorTest extends \Codeception\TestCase\Test
self::assertSame('This String Is Titleized', $this->inflector->titleize('this string is titleized'));
self::assertSame('This String Is Titleized', $this->inflector->titleize('this_string_is_titleized'));
self::assertSame('This String Is Titleized', $this->inflector->titleize('this-string-is-titleized'));
self::assertSame('Échelle Synoptique', $this->inflector->titleize('échelle synoptique'));
self::assertSame('This string is titleized', $this->inflector->titleize('ThisStringIsTitleized', 'first'));
self::assertSame('This string is titleized', $this->inflector->titleize('this string is titleized', 'first'));
self::assertSame('This string is titleized', $this->inflector->titleize('this_string_is_titleized', 'first'));
self::assertSame('This string is titleized', $this->inflector->titleize('this-string-is-titleized', 'first'));
self::assertSame('Échelle synoptique', $this->inflector->titleize('échelle synoptique', 'first'));
}
public function testCamelize(): void