refs #6635 Inflector::underscore, humanize support multibyte string inputs

This commit is contained in:
nojimage 2015-05-26 13:29:05 +09:00
parent 8ebc9cdd87
commit c6e4208bda
2 changed files with 12 additions and 2 deletions

View file

@ -399,12 +399,14 @@ class InflectorTest extends CakeTestCase {
$this->assertSame(Inflector::underscore('testThing'), 'test_thing');
$this->assertSame(Inflector::underscore('TestThingExtra'), 'test_thing_extra');
$this->assertSame(Inflector::underscore('testThingExtra'), 'test_thing_extra');
$this->assertSame(Inflector::underscore('testThingExtrå'), 'test_thing_extrå');
// Identical checks test the cache code path.
$this->assertSame(Inflector::underscore('TestThing'), 'test_thing');
$this->assertSame(Inflector::underscore('testThing'), 'test_thing');
$this->assertSame(Inflector::underscore('TestThingExtra'), 'test_thing_extra');
$this->assertSame(Inflector::underscore('testThingExtra'), 'test_thing_extra');
$this->assertSame(Inflector::underscore('testThingExtrå'), 'test_thing_extrå');
// Test stupid values
$this->assertSame(Inflector::underscore(''), '');
@ -457,6 +459,8 @@ class InflectorTest extends CakeTestCase {
$this->assertEquals(Inflector::humanize('posts'), 'Posts');
$this->assertEquals(Inflector::humanize('posts_tags'), 'Posts Tags');
$this->assertEquals(Inflector::humanize('file_systems'), 'File Systems');
$this->assertEquals(Inflector::humanize('hello_wörld'), 'Hello Wörld');
$this->assertEquals(Inflector::humanize('福岡_city'), '福岡 City');
}
/**

View file

@ -479,7 +479,12 @@ class Inflector {
*/
public static function underscore($camelCasedWord) {
if (!($result = self::_cache(__FUNCTION__, $camelCasedWord))) {
$result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
$underscoredWord = preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord);
if (function_exists('mb_convert_case')) {
$result = mb_convert_case($underscoredWord, MB_CASE_LOWER, Configure::read('App.encoding'));
} else {
$result = strtolower($underscoredWord);
}
self::_cache(__FUNCTION__, $camelCasedWord, $result);
}
return $result;
@ -495,8 +500,9 @@ class Inflector {
*/
public static function humanize($lowerCaseAndUnderscoredWord) {
if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
$lowerCaseAndUnderscoredWord = self::underscore($lowerCaseAndUnderscoredWord);
$result = str_replace('_', ' ', $lowerCaseAndUnderscoredWord);
if (function_exists('mb_convert_case') && Multibyte::checkMultibyte($result)) {
if (function_exists('mb_convert_case')) {
$result = mb_convert_case($result, MB_CASE_TITLE, Configure::read('App.encoding'));
} else {
$result = ucwords($result);