diff --git a/yii/helpers/Inflector.php b/framework/yii/helpers/Inflector.php similarity index 100% rename from yii/helpers/Inflector.php rename to framework/yii/helpers/Inflector.php diff --git a/yii/helpers/base/Inflector.php b/framework/yii/helpers/base/Inflector.php similarity index 83% rename from yii/helpers/base/Inflector.php rename to framework/yii/helpers/base/Inflector.php index af3f266..f233547 100644 --- a/yii/helpers/base/Inflector.php +++ b/framework/yii/helpers/base/Inflector.php @@ -290,10 +290,11 @@ class Inflector ); /** - * Returns the plural of a $word - * - * @param string $word the word to pluralize - * @return string + * Converts a word to its plural form. + * Note that this is for English only! + * For example, 'apple' will become 'apples', and 'child' will become 'children'. + * @param string $word the word to be pluralized + * @return string the pluralized word */ public static function pluralize($word) { @@ -319,7 +320,6 @@ class Inflector /** * Returns the singular of the $word - * * @param string $word the english word to singularize * @return string Singular noun. */ @@ -356,7 +356,6 @@ class Inflector /** * Converts an underscored or CamelCase word into a English * sentence. - * * @param string $words * @param bool $ucAll whether to set all words to uppercase * @return string @@ -370,11 +369,9 @@ class Inflector /** * Returns given word as CamelCased - * * Converts a word like "send_email" to "SendEmail". It * will remove non alphanumeric character from the word, so * "who's online" will be converted to "WhoSOnline" - * * @see variablize * @param string $word the word to CamelCase * @return string @@ -385,19 +382,64 @@ class Inflector } /** + * Converts a CamelCase name into space-separated words. + * For example, 'PostTag' will be converted to 'Post Tag'. + * @param string $name the string to be converted + * @param boolean $ucwords whether to capitalize the first letter in each word + * @return string the resulting words + */ + public static function camel2words($name, $ucwords = true) + { + $label = trim(strtolower(str_replace(array( + '-', + '_', + '.' + ), ' ', preg_replace('/(?assertEquals("people", Inflector::pluralize('person')); - $this->assertEquals("fish", Inflector::pluralize('fish')); - $this->assertEquals("men", Inflector::pluralize('man')); - $this->assertEquals("tables", Inflector::pluralize('table')); + $testData = array( + 'move' => 'moves', + 'foot' => 'feet', + 'child' => 'children', + 'human' => 'humans', + 'man' => 'men', + 'staff' => 'staff', + 'tooth' => 'teeth', + 'person' => 'people', + 'mouse' => 'mice', + 'touch' => 'touches', + 'hash' => 'hashes', + 'shelf' => 'shelves', + 'potato' => 'potatoes', + 'bus' => 'buses', + 'test' => 'tests', + 'car' => 'cars', + ); + + foreach ($testData as $testIn => $testOut) { + $this->assertEquals($testOut, Inflector::pluralize($testIn)); + $this->assertEquals(ucfirst($testOut), ucfirst(Inflector::pluralize($testIn))); + } } public function testSingularize() @@ -42,10 +61,35 @@ class InflectorTest extends TestCase $this->assertEquals("me_my_self_and_i", Inflector::underscore('MeMySelfAndI')); } + public function testCamel2words() + { + $this->assertEquals('Camel Case', Inflector::camel2words('camelCase')); + $this->assertEquals('Lower Case', Inflector::camel2words('lower_case')); + $this->assertEquals('Tricky Stuff It Is Testing', Inflector::camel2words(' tricky_stuff.it-is testing... ')); + } + + public function testCamel2id() + { + $this->assertEquals('post-tag', Inflector::camel2id('PostTag')); + $this->assertEquals('post_tag', Inflector::camel2id('PostTag', '_')); + + $this->assertEquals('post-tag', Inflector::camel2id('postTag')); + $this->assertEquals('post_tag', Inflector::camel2id('postTag', '_')); + } + + public function testId2camel() + { + $this->assertEquals('PostTag', Inflector::id2camel('post-tag')); + $this->assertEquals('PostTag', Inflector::id2camel('post_tag', '_')); + + $this->assertEquals('PostTag', Inflector::id2camel('post-tag')); + $this->assertEquals('PostTag', Inflector::id2camel('post_tag', '_')); + } + public function testHumanize() { $this->assertEquals("Me my self and i", Inflector::humanize('me_my_self_and_i')); - $this->assertEquals("Me My Self And i", Inflector::humanize('me_my_self_and_i'), true); + $this->assertEquals("Me My Self And I", Inflector::humanize('me_my_self_and_i'), true); } public function testVariablize()