Commit 7b98b424 by Antonio Ramirez

added inflection methods from StringHelper + some comment fixes

parent 641e6ee7
...@@ -290,10 +290,11 @@ class Inflector ...@@ -290,10 +290,11 @@ class Inflector
); );
/** /**
* Returns the plural of a $word * Converts a word to its plural form.
* * Note that this is for English only!
* @param string $word the word to pluralize * For example, 'apple' will become 'apples', and 'child' will become 'children'.
* @return string * @param string $word the word to be pluralized
* @return string the pluralized word
*/ */
public static function pluralize($word) public static function pluralize($word)
{ {
...@@ -319,7 +320,6 @@ class Inflector ...@@ -319,7 +320,6 @@ class Inflector
/** /**
* Returns the singular of the $word * Returns the singular of the $word
*
* @param string $word the english word to singularize * @param string $word the english word to singularize
* @return string Singular noun. * @return string Singular noun.
*/ */
...@@ -356,7 +356,6 @@ class Inflector ...@@ -356,7 +356,6 @@ class Inflector
/** /**
* Converts an underscored or CamelCase word into a English * Converts an underscored or CamelCase word into a English
* sentence. * sentence.
*
* @param string $words * @param string $words
* @param bool $ucAll whether to set all words to uppercase * @param bool $ucAll whether to set all words to uppercase
* @return string * @return string
...@@ -370,11 +369,9 @@ class Inflector ...@@ -370,11 +369,9 @@ class Inflector
/** /**
* Returns given word as CamelCased * Returns given word as CamelCased
*
* Converts a word like "send_email" to "SendEmail". It * Converts a word like "send_email" to "SendEmail". It
* will remove non alphanumeric character from the word, so * will remove non alphanumeric character from the word, so
* "who's online" will be converted to "WhoSOnline" * "who's online" will be converted to "WhoSOnline"
*
* @see variablize * @see variablize
* @param string $word the word to CamelCase * @param string $word the word to CamelCase
* @return string * @return string
...@@ -385,19 +382,64 @@ class Inflector ...@@ -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('/(?<![A-Z])[A-Z]/', ' \0', $name))));
return $ucwords ? ucwords($label) : $label;
}
/**
* Converts a CamelCase name into an ID in lowercase.
* Words in the ID may be concatenated using the specified character (defaults to '-').
* For example, 'PostTag' will be converted to 'post-tag'.
* @param string $name the string to be converted
* @param string $separator the character used to concatenate the words in the ID
* @return string the resulting ID
*/
public static function camel2id($name, $separator = '-')
{
if ($separator === '_') {
return trim(strtolower(preg_replace('/(?<![A-Z])[A-Z]/', '_\0', $name)), '_');
} else {
return trim(strtolower(str_replace('_', $separator, preg_replace('/(?<![A-Z])[A-Z]/', $separator . '\0', $name))), $separator);
}
}
/**
* Converts an ID into a CamelCase name.
* Words in the ID separated by `$separator` (defaults to '-') will be concatenated into a CamelCase name.
* For example, 'post-tag' is converted to 'PostTag'.
* @param string $id the ID to be converted
* @param string $separator the character used to separate the words in the ID
* @return string the resulting CamelCase name
*/
public static function id2camel($id, $separator = '-')
{
return str_replace(' ', '', ucwords(implode(' ', explode($separator, $id))));
}
/**
* Converts any "CamelCased" into an "underscored_word". * Converts any "CamelCased" into an "underscored_word".
*
* @param string $words the word(s) to underscore * @param string $words the word(s) to underscore
* @return string * @return string
*/ */
public static function underscore($words) public static function underscore($words)
{ {
return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $words)); return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $words));
} }
/** /**
* Returns a human-readable string from $word * Returns a human-readable string from $word
*
* @param string $word the string to humanize * @param string $word the string to humanize
* @param bool $ucAll whether to set all words to uppercase or not * @param bool $ucAll whether to set all words to uppercase or not
* @return string * @return string
...@@ -409,12 +451,10 @@ class Inflector ...@@ -409,12 +451,10 @@ class Inflector
} }
/** /**
* Same as camelize but first char is in lowercase * Same as camelize but first char is in lowercase.
*
* Converts a word like "send_email" to "sendEmail". It * Converts a word like "send_email" to "sendEmail". It
* will remove non alphanumeric character from the word, so * will remove non alphanumeric character from the word, so
* "who's online" will be converted to "whoSOnline" * "who's online" will be converted to "whoSOnline"
*
* @param string $word to lowerCamelCase * @param string $word to lowerCamelCase
* @return string * @return string
*/ */
...@@ -427,7 +467,6 @@ class Inflector ...@@ -427,7 +467,6 @@ class Inflector
/** /**
* Converts a class name to its table name (pluralized) * Converts a class name to its table name (pluralized)
* naming conventions. For example, converts "Person" to "people" * naming conventions. For example, converts "Person" to "people"
*
* @param string $class_name the class name for getting related table_name * @param string $class_name the class name for getting related table_name
* @return string * @return string
*/ */
...@@ -439,10 +478,9 @@ class Inflector ...@@ -439,10 +478,9 @@ class Inflector
/** /**
* Returns a string with all spaces converted to given replacement and * Returns a string with all spaces converted to given replacement and
* non word characters removed. Maps special characters to ASCII using * non word characters removed. Maps special characters to ASCII using
* `Inflector::$transliteration`. * `Inflector::$transliteration`
* * @param string $string An arbitrary string to convert
* @param string $string An arbitrary string to convert. * @param string $replacement The replacement to use for spaces
* @param string $replacement The replacement to use for spaces.
* @return string The converted string. * @return string The converted string.
*/ */
public static function slug($string, $replacement = '-') public static function slug($string, $replacement = '-')
...@@ -459,7 +497,6 @@ class Inflector ...@@ -459,7 +497,6 @@ class Inflector
/** /**
* Converts a table name to its class name. For example, converts "people" to "Person" * Converts a table name to its class name. For example, converts "people" to "Person"
*
* @param string $table_name * @param string $table_name
* @return string * @return string
*/ */
...@@ -469,10 +506,7 @@ class Inflector ...@@ -469,10 +506,7 @@ class Inflector
} }
/** /**
* Converts number to its ordinal English form. * Converts number to its ordinal English form. For example, converts 13 to 13th, 2 to 2nd ...
*
* This method converts 13 to 13th, 2 to 2nd ...
*
* @param int $number the number to get its ordinal value * @param int $number the number to get its ordinal value
* @return string * @return string
*/ */
......
...@@ -12,10 +12,29 @@ class InflectorTest extends TestCase ...@@ -12,10 +12,29 @@ class InflectorTest extends TestCase
public function testPluralize() public function testPluralize()
{ {
$this->assertEquals("people", Inflector::pluralize('person')); $testData = array(
$this->assertEquals("fish", Inflector::pluralize('fish')); 'move' => 'moves',
$this->assertEquals("men", Inflector::pluralize('man')); 'foot' => 'feet',
$this->assertEquals("tables", Inflector::pluralize('table')); '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() public function testSingularize()
...@@ -42,10 +61,35 @@ class InflectorTest extends TestCase ...@@ -42,10 +61,35 @@ class InflectorTest extends TestCase
$this->assertEquals("me_my_self_and_i", Inflector::underscore('MeMySelfAndI')); $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() 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'));
$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() public function testVariablize()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment