Use mb* functions in Inflector humanize/underscore.

Use the mbstring shims we already provide to make Inflector more robust
than it currently is. This solves the invalid ID attribute generation in
a way that never varies between environments.

Refs #6635
This commit is contained in:
mark_story 2015-05-26 22:51:00 -04:00
parent c6e4208bda
commit 733ddc7ff4

View file

@ -480,11 +480,7 @@ class Inflector {
public static function underscore($camelCasedWord) { public static function underscore($camelCasedWord) {
if (!($result = self::_cache(__FUNCTION__, $camelCasedWord))) { if (!($result = self::_cache(__FUNCTION__, $camelCasedWord))) {
$underscoredWord = preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord); $underscoredWord = preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord);
if (function_exists('mb_convert_case')) { $result = mb_strtolower($underscoredWord);
$result = mb_convert_case($underscoredWord, MB_CASE_LOWER, Configure::read('App.encoding'));
} else {
$result = strtolower($underscoredWord);
}
self::_cache(__FUNCTION__, $camelCasedWord, $result); self::_cache(__FUNCTION__, $camelCasedWord, $result);
} }
return $result; return $result;
@ -501,12 +497,11 @@ class Inflector {
public static function humanize($lowerCaseAndUnderscoredWord) { public static function humanize($lowerCaseAndUnderscoredWord) {
if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) { if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
$lowerCaseAndUnderscoredWord = self::underscore($lowerCaseAndUnderscoredWord); $lowerCaseAndUnderscoredWord = self::underscore($lowerCaseAndUnderscoredWord);
$result = str_replace('_', ' ', $lowerCaseAndUnderscoredWord); $result = explode(' ', str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
if (function_exists('mb_convert_case')) { foreach ($result as &$word) {
$result = mb_convert_case($result, MB_CASE_TITLE, Configure::read('App.encoding')); $word = mb_strtoupper(mb_substr($word, 0, 1)) . mb_substr($word, 1);
} else {
$result = ucwords($result);
} }
$result = implode(' ', $result);
self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result); self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
} }
return $result; return $result;