Fixed BC breaking change in Inflector

Fixes #16239
This commit is contained in:
SilverFire - Dmitry Naumenko
2018-05-08 16:39:55 +03:00
parent ce38d5e5ef
commit 3d96a45f6b
4 changed files with 90 additions and 21 deletions

View File

@ -342,7 +342,7 @@ class BaseInflector
{
$words = static::humanize(static::underscore($words), $ucAll);
return $ucAll ? mb_convert_case($words, MB_CASE_TITLE, self::encoding()) : self::mb_ucfirst($words, self::encoding());
return $ucAll ? StringHelper::mb_ucwords($words, self::encoding()) : StringHelper::mb_ucfirst($words, self::encoding());
}
/**
@ -357,7 +357,7 @@ class BaseInflector
*/
public static function camelize($word)
{
return str_replace(' ', '', mb_convert_case(preg_replace('/[^\pL\pN]+/u', ' ', $word), MB_CASE_TITLE, self::encoding()));
return str_replace(' ', '', StringHelper::mb_ucwords(preg_replace('/[^\pL\pN]+/u', ' ', $word), self::encoding()));
}
/**
@ -375,7 +375,7 @@ class BaseInflector
'.',
], ' ', preg_replace('/(\p{Lu})/u', ' \0', $name))), self::encoding());
return $ucwords ? mb_convert_case($label, MB_CASE_TITLE, self::encoding()) : $label;
return $ucwords ? StringHelper::mb_ucwords($label, self::encoding()) : $label;
}
/**
@ -407,7 +407,7 @@ class BaseInflector
*/
public static function id2camel($id, $separator = '-')
{
return str_replace(' ', '', mb_convert_case(str_replace($separator, ' ', $id), MB_CASE_TITLE, self::encoding()));
return str_replace(' ', '', StringHelper::mb_ucwords(str_replace($separator, ' ', $id), self::encoding()));
}
/**
@ -431,7 +431,7 @@ class BaseInflector
$word = str_replace('_', ' ', preg_replace('/_id$/', '', $word));
$encoding = self::encoding();
return $ucAll ? mb_convert_case($word, MB_CASE_TITLE, $encoding) : self::mb_ucfirst($word, $encoding);
return $ucAll ? StringHelper::mb_ucwords($word, $encoding) : StringHelper::mb_ucfirst($word, $encoding);
}
/**
@ -609,18 +609,4 @@ class BaseInflector
return isset(Yii::$app) ? Yii::$app->charset : 'UTF-8';
}
/**
* The same as built-in `ucfirst`, but unicode-safe
*
* @param string $string
* @param string $encoding
* @return string
*/
private static function mb_ucfirst($string, $encoding)
{
$firstChar = mb_substr($string, 0, 1, $encoding);
$rest = mb_substr($string, 1, null, $encoding);
return mb_strtoupper($firstChar, $encoding) . $rest;
}
}

View File

@ -418,4 +418,40 @@ class BaseStringHelper
return preg_match($pattern, $string) === 1;
}
/**
* This method provides a unicode-safe implementation of built-in PHP function `ucfirst()`.
*
* @param string $string the string to be proceeded
* @param string $encoding Optional, defaults to "UTF-8"
* @return string
* @see http://php.net/manual/en/function.ucfirst.php
* @since 2.0.16
*/
public static function mb_ucfirst($string, $encoding = 'UTF-8')
{
$firstChar = mb_substr($string, 0, 1, $encoding);
$rest = mb_substr($string, 1, null, $encoding);
return mb_strtoupper($firstChar, $encoding) . $rest;
}
/**
* This method provides a unicode-safe implementation of built-in PHP function `ucwords()`.
*
* @param string $string the string to be proceeded
* @param string $encoding Optional, defaults to "UTF-8"
* @see http://php.net/manual/en/function.ucwords.php
* @return string
*/
public static function mb_ucwords($string, $encoding = 'UTF-8')
{
$words = preg_split("/\s/u", $string, -1, PREG_SPLIT_NO_EMPTY);
$titelized = array_map(function ($word) use ($encoding) {
return static::mb_ucfirst($word, $encoding);
}, $words);
return implode(' ', $titelized);
}
}