Browse Source

Merge pull request #4460 from cakephp/3.0-inflector

3.0 inflector
José Lorenzo Rodríguez 11 years ago
parent
commit
18756a01f6
2 changed files with 43 additions and 9 deletions
  1. 22 5
      src/Utility/Inflector.php
  2. 21 4
      tests/TestCase/Utility/InflectorTest.php

+ 22 - 5
src/Utility/Inflector.php

@@ -418,7 +418,7 @@ class Inflector {
  * @param string $type Inflection type
  * @param string $key Original value
  * @param string $value Inflected value
- * @return string Inflected value, from cache
+ * @return string|null Inflected value on cache hit or null on cache miss.
  */
 	protected static function _cache($type, $key, $value = false) {
 		$key = '_' . $key;
@@ -577,7 +577,7 @@ class Inflector {
  */
 	public static function camelize($lowerCaseAndUnderscoredWord) {
 		if (!($result = static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
-			$result = str_replace(' ', '', Inflector::humanize($lowerCaseAndUnderscoredWord));
+			$result = str_replace(' ', '', static::humanize($lowerCaseAndUnderscoredWord));
 			static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
 		}
 		return $result;
@@ -599,6 +599,23 @@ class Inflector {
 	}
 
 /**
+ * Returns the given CamelCasedWordGroup as an dashed-word-group.
+ *
+ * @param string $wordGroup The string to dasherize.
+ * @return string Dashed version of the word group
+ */
+	public static function dasherize($wordGroup) {
+		$result = static::_cache(__FUNCTION__, $wordGroup);
+		if ($result !== false) {
+			return $result;
+		}
+
+		$result = str_replace('_', '-', static::underscore($wordGroup));
+		static::_cache(__FUNCTION__, $wordGroup, $result);
+		return $result;
+	}
+
+/**
  * Returns the given underscored_word_group as a Human Readable Word Group.
  * (Underscores are replaced by spaces and capitalized following words.)
  *
@@ -623,7 +640,7 @@ class Inflector {
  */
 	public static function tableize($className) {
 		if (!($result = static::_cache(__FUNCTION__, $className))) {
-			$result = Inflector::pluralize(Inflector::underscore($className));
+			$result = static::pluralize(static::underscore($className));
 			static::_cache(__FUNCTION__, $className, $result);
 		}
 		return $result;
@@ -638,7 +655,7 @@ class Inflector {
  */
 	public static function classify($tableName) {
 		if (!($result = static::_cache(__FUNCTION__, $tableName))) {
-			$result = Inflector::camelize(Inflector::singularize($tableName));
+			$result = static::camelize(static::singularize($tableName));
 			static::_cache(__FUNCTION__, $tableName, $result);
 		}
 		return $result;
@@ -653,7 +670,7 @@ class Inflector {
  */
 	public static function variable($string) {
 		if (!($result = static::_cache(__FUNCTION__, $string))) {
-			$camelized = Inflector::camelize(Inflector::underscore($string));
+			$camelized = static::camelize(static::underscore($string));
 			$replace = strtolower(substr($camelized, 0, 1));
 			$result = preg_replace('/\\w/', $replace, $camelized, 1);
 			static::_cache(__FUNCTION__, $string, $result);

+ 21 - 4
tests/TestCase/Utility/InflectorTest.php

@@ -1,9 +1,5 @@
 <?php
 /**
- * InflectorTest
- *
- * InflectorTest is used to test cases on the Inflector class
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -376,6 +372,25 @@ class InflectorTest extends TestCase {
 	}
 
 /**
+ * testDasherized method
+ *
+ * @return void
+ */
+	public function testDasherized() {
+		$this->assertSame('test-thing', Inflector::dasherize('TestThing'));
+		$this->assertSame('test-thing', Inflector::dasherize('testThing'));
+		$this->assertSame('test-thing-extra', Inflector::dasherize('TestThingExtra'));
+		$this->assertSame('test-thing-extra', Inflector::dasherize('testThingExtra'));
+		$this->assertSame('test-this-thing', Inflector::dasherize('test_this_thing'));
+
+		// Test stupid values
+		$this->assertSame('', Inflector::dasherize(null));
+		$this->assertSame('', Inflector::dasherize(''));
+		$this->assertSame('0', Inflector::dasherize(0));
+		$this->assertSame('', Inflector::dasherize(false));
+	}
+
+/**
  * testVariableNaming method
  *
  * @return void
@@ -420,6 +435,8 @@ class InflectorTest extends TestCase {
 		$this->assertEquals(Inflector::humanize('posts'), 'Posts');
 		$this->assertEquals(Inflector::humanize('posts_tags'), 'Posts Tags');
 		$this->assertEquals(Inflector::humanize('file_systems'), 'File Systems');
+		$this->assertSame('', Inflector::humanize(null));
+		$this->assertSame('', Inflector::humanize(false));
 	}
 
 /**