Browse Source

Merge pull request #5859 from cakephp/3.0-stricter-inflector

3.0 stricter inflector
Mark Story 11 years ago
parent
commit
647fccef39
2 changed files with 104 additions and 46 deletions
  1. 72 37
      src/Utility/Inflector.php
  2. 32 9
      tests/TestCase/Utility/InflectorTest.php

+ 72 - 37
src/Utility/Inflector.php

@@ -576,69 +576,95 @@ class Inflector
     }
 
     /**
-     * Returns the given lower_case_and_underscored_word as a CamelCased word.
+     * Returns the input lower_case_delimited_string as a CamelCasedString.
      *
-     * @param string $lowerCaseAndUnderscoredWord Word to camelize
-     * @return string Camelized word. LikeThis.
+     * @param string $string String to camelize
+     * @param string $delimiter the delimiter in the input string
+     * @return string CamelizedStringLikeThis.
      * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-camelcase-and-under-scored-forms
      */
-    public static function camelize($lowerCaseAndUnderscoredWord)
+    public static function camelize($string, $delimiter = '_')
     {
-        if (!($result = static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
-            $result = str_replace(' ', '', static::humanize($lowerCaseAndUnderscoredWord));
-            static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
+        $cacheKey = __FUNCTION__ . $delimiter;
+
+        $result = static::_cache($cacheKey, $string);
+
+        if ($result === false) {
+            $result = str_replace(' ', '', static::humanize($string, $delimiter));
+            static::_cache(__FUNCTION__, $string, $result);
         }
+
         return $result;
     }
 
     /**
-     * Returns the given camelCasedWord as an underscored_word.
+     * Returns the input CamelCasedString as an underscored_string.
      *
-     * @param string $camelCasedWord Camel-cased word to be "underscorized"
-     * @return string Underscore-syntaxed version of the $camelCasedWord
+     * Also replaces dashes with underscores
+     *
+     * @param string $string CamelCasedString to be "underscorized"
+     * @return string underscore_version of the input string
      * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-camelcase-and-under-scored-forms
      */
-    public static function underscore($camelCasedWord)
+    public static function underscore($string)
     {
-        if (!($result = static::_cache(__FUNCTION__, $camelCasedWord))) {
-            $result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
-            static::_cache(__FUNCTION__, $camelCasedWord, $result);
-        }
-        return $result;
+        return static::delimit(str_replace('-', '_', $string), '_');
     }
 
     /**
-     * Returns the given CamelCasedWordGroup as an dashed-word-group.
+     * Returns the input CamelCasedString as an dashed-string.
+     *
+     * Also replaces underscores with dashes
      *
-     * @param string $wordGroup The string to dasherize.
-     * @return string Dashed version of the word group
+     * @param string $string The string to dasherize.
+     * @return string Dashed version of the input string
      */
-    public static function dasherize($wordGroup)
+    public static function dasherize($string)
     {
-        $result = static::_cache(__FUNCTION__, $wordGroup);
-        if ($result !== false) {
-            return $result;
-        }
-
-        $result = str_replace('_', '-', static::underscore($wordGroup));
-        static::_cache(__FUNCTION__, $wordGroup, $result);
-        return $result;
+        return static::delimit(str_replace('_', '-', $string), '-');
     }
 
     /**
-     * Returns the given underscored_word_group as a Human Readable Word Group.
+     * Returns the input lower_case_delimited_string as 'A Human Readable String'.
      * (Underscores are replaced by spaces and capitalized following words.)
      *
-     * @param string $lowerCaseAndUnderscoredWord String to be made more readable
+     * @param string $string String to be humanized
+     * @param string $delimiter the character to replace with a space
      * @return string Human-readable string
      * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-human-readable-forms
      */
-    public static function humanize($lowerCaseAndUnderscoredWord)
+    public static function humanize($string, $delimiter = '_')
     {
-        if (!($result = static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
-            $result = ucwords(str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
-            static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
+        $cacheKey = __FUNCTION__ . $delimiter;
+
+        $result = static::_cache($cacheKey, $string);
+
+        if ($result === false) {
+            $result = ucwords(str_replace($delimiter, ' ', $string));
+            static::_cache($cacheKey, $string, $result);
         }
+
+        return $result;
+    }
+
+    /**
+     * Expects a CamelCasedInputString, and produces a lower_case_delimited_string
+     *
+     * @param string $string String to delimit
+     * @param string $delimiter the character to use as a delimiter
+     * @return string delimited string
+     */
+    public static function delimit($string, $delimiter = '_')
+    {
+        $cacheKey = __FUNCTION__ . $delimiter;
+
+        $result = static::_cache($cacheKey, $string);
+
+        if ($result === false) {
+            $result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', $delimiter . '\\1', $string));
+            static::_cache($cacheKey, $string, $result);
+        }
+
         return $result;
     }
 
@@ -651,10 +677,13 @@ class Inflector
      */
     public static function tableize($className)
     {
-        if (!($result = static::_cache(__FUNCTION__, $className))) {
+        $result = static::_cache(__FUNCTION__, $className);
+
+        if ($result === false) {
             $result = static::pluralize(static::underscore($className));
             static::_cache(__FUNCTION__, $className, $result);
         }
+
         return $result;
     }
 
@@ -667,10 +696,13 @@ class Inflector
      */
     public static function classify($tableName)
     {
-        if (!($result = static::_cache(__FUNCTION__, $tableName))) {
+        $result = static::_cache(__FUNCTION__, $tableName);
+
+        if ($result === false) {
             $result = static::camelize(static::singularize($tableName));
             static::_cache(__FUNCTION__, $tableName, $result);
         }
+
         return $result;
     }
 
@@ -683,12 +715,15 @@ class Inflector
      */
     public static function variable($string)
     {
-        if (!($result = static::_cache(__FUNCTION__, $string))) {
+        $result = static::_cache(__FUNCTION__, $string);
+
+        if ($result === false) {
             $camelized = static::camelize(static::underscore($string));
             $replace = strtolower(substr($camelized, 0, 1));
             $result = preg_replace('/\\w/', $replace, $camelized, 1);
             static::_cache(__FUNCTION__, $string, $result);
         }
+
         return $result;
     }
 

+ 32 - 9
tests/TestCase/Utility/InflectorTest.php

@@ -255,11 +255,11 @@ class InflectorTest extends TestCase
     }
 
     /**
-     * testInflectorSlug method
+     * testSlug method
      *
      * @return void
      */
-    public function testInflectorSlug()
+    public function testSlug()
     {
         $result = Inflector::slug('Foo Bar: Not just for breakfast any-more');
         $expected = 'Foo-Bar-Not-just-for-breakfast-any-more';
@@ -326,7 +326,7 @@ class InflectorTest extends TestCase
      *
      * @return void
      */
-    public function testInflectorSlugCharList()
+    public function testSlugCharList()
     {
         foreach (self::$maps as $language => $list) {
             foreach ($list as $from => $to) {
@@ -337,11 +337,11 @@ class InflectorTest extends TestCase
     }
 
     /**
-     * testInflectorSlugWithMap method
+     * testSlugWithMap method
      *
      * @return void
      */
-    public function testInflectorSlugWithMap()
+    public function testSlugWithMap()
     {
         Inflector::rules('transliteration', ['r' => '1']);
         $result = Inflector::slug('replace every r');
@@ -354,11 +354,11 @@ class InflectorTest extends TestCase
     }
 
     /**
-     * testInflectorSlugWithMapOverridingDefault method
+     * testSlugWithMapOverridingDefault method
      *
      * @return void
      */
-    public function testInflectorSlugWithMapOverridingDefault()
+    public function testSlugWithMapOverridingDefault()
     {
         Inflector::rules('transliteration', ['å' => 'aa', 'ø' => 'oe']);
         $result = Inflector::slug('Testing æ ø å', '-');
@@ -367,16 +367,17 @@ class InflectorTest extends TestCase
     }
 
     /**
-     * testInflectorUnderscore method
+     * testUnderscore method
      *
      * @return void
      */
-    public function testInflectorUnderscore()
+    public function testUnderscore()
     {
         $this->assertSame('test_thing', Inflector::underscore('TestThing'));
         $this->assertSame('test_thing', Inflector::underscore('testThing'));
         $this->assertSame('test_thing_extra', Inflector::underscore('TestThingExtra'));
         $this->assertSame('test_thing_extra', Inflector::underscore('testThingExtra'));
+        $this->assertSame('test_this_thing', Inflector::underscore('test-this-thing'));
 
         // Identical checks test the cache code path.
         $this->assertSame('test_thing', Inflector::underscore('TestThing'));
@@ -411,6 +412,28 @@ class InflectorTest extends TestCase
     }
 
     /**
+     * Demonstrate the expected output for bad inputs
+     *
+     * @return void
+     */
+    public function testCamelize()
+    {
+        $this->assertSame('TestThing', Inflector::camelize('test_thing'));
+        $this->assertSame('Test-thing', Inflector::camelize('test-thing'));
+        $this->assertSame('TestThing', Inflector::camelize('test thing'));
+
+        $this->assertSame('Test_thing', Inflector::camelize('test_thing', '-'));
+        $this->assertSame('TestThing', Inflector::camelize('test-thing', '-'));
+        $this->assertSame('TestThing', Inflector::camelize('test thing', '-'));
+
+        $this->assertSame('Test_thing', Inflector::camelize('test_thing', ' '));
+        $this->assertSame('Test-thing', Inflector::camelize('test-thing', ' '));
+        $this->assertSame('TestThing', Inflector::camelize('test thing', ' '));
+
+        $this->assertSame('TestPlugin.TestPluginComments', Inflector::camelize('TestPlugin.TestPluginComments'));
+    }
+
+    /**
      * testVariableNaming method
      *
      * @return void