From c6e4208bda27115d03148d4191c0d96baff8e61c Mon Sep 17 00:00:00 2001 From: nojimage Date: Tue, 26 May 2015 13:29:05 +0900 Subject: [PATCH] refs #6635 Inflector::underscore, humanize support multibyte string inputs --- lib/Cake/Test/Case/Utility/InflectorTest.php | 4 ++++ lib/Cake/Utility/Inflector.php | 10 ++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/Cake/Test/Case/Utility/InflectorTest.php b/lib/Cake/Test/Case/Utility/InflectorTest.php index b621a1920..c1abaf15a 100644 --- a/lib/Cake/Test/Case/Utility/InflectorTest.php +++ b/lib/Cake/Test/Case/Utility/InflectorTest.php @@ -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'); } /** diff --git a/lib/Cake/Utility/Inflector.php b/lib/Cake/Utility/Inflector.php index 426334fa5..79eb448f3 100644 --- a/lib/Cake/Utility/Inflector.php +++ b/lib/Cake/Utility/Inflector.php @@ -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);