Browse Source

Add Inflector::hyphenate().

ADmad 11 years ago
parent
commit
a9e008ff88
2 changed files with 35 additions and 0 deletions
  1. 17 0
      src/Utility/Inflector.php
  2. 18 0
      tests/TestCase/Utility/InflectorTest.php

+ 17 - 0
src/Utility/Inflector.php

@@ -599,6 +599,23 @@ class Inflector {
 	}
 
 /**
+ * Returns the given underscored_word or CamelCasedWord as an hyphenated-word.
+ *
+ * @param string $word The word to hyphenate.
+ * @return string Hyphenated version of the word.
+ */
+	public static function hyphenate($word) {
+		$result = static::_cache(__FUNCTION__, $word);
+		if ($result !== false) {
+			return $result;
+		}
+
+		$result = str_replace('_', '-', static::underscore($word));
+		static::_cache(__FUNCTION__, $word, $result);
+		return $result;
+	}
+
+/**
  * Returns the given underscored_word_group as a Human Readable Word Group.
  * (Underscores are replaced by spaces and capitalized following words.)
  *

+ 18 - 0
tests/TestCase/Utility/InflectorTest.php

@@ -376,6 +376,24 @@ class InflectorTest extends TestCase {
 	}
 
 /**
+ * testInflectorHyphenate method
+ *
+ * @return void
+ */
+	public function testInflectorHyphenate() {
+		$this->assertSame('test-thing', Inflector::hyphenate('TestThing'));
+		$this->assertSame('test-thing', Inflector::hyphenate('testThing'));
+		$this->assertSame('test-thing-extra', Inflector::hyphenate('TestThingExtra'));
+		$this->assertSame('test-thing-extra', Inflector::hyphenate('testThingExtra'));
+		$this->assertSame('test-this-thing', Inflector::hyphenate('test_this_thing'));
+
+		// Test stupid values
+		$this->assertSame('', Inflector::hyphenate(''));
+		$this->assertSame('0', Inflector::hyphenate(0));
+		$this->assertSame('', Inflector::hyphenate(false));
+	}
+
+/**
  * testVariableNaming method
  *
  * @return void