Permalink
Browse files

Changes to Doctrine/Common/Util/Inflector and Doctrine/Tests/Common/U…

…til/InflectorTest to follow coding standards
  • Loading branch information...
1 parent 1dff809 commit bdea5a83aebba22a6ea4f4a316758c50fc4522d2 @jbergler committed Nov 23, 2012
Showing with 154 additions and 225 deletions.
  1. +94 −115 lib/Doctrine/Common/Util/Inflector.php
  2. +60 −110 tests/Doctrine/Tests/Common/Util/InflectorTest.php
View
209 lib/Doctrine/Common/Util/Inflector.php
@@ -40,44 +40,11 @@
class Inflector
{
/**
- * Convert word in to the format for a Doctrine table name. Converts 'ModelName' to 'model_name'
- *
- * @param string $word Word to tableize
- * @return string $word Tableized word
- */
- public static function tableize($word)
- {
- return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $word));
- }
-
- /**
- * Convert a word in to the format for a Doctrine class name. Converts 'table_name' to 'TableName'
- *
- * @param string $word Word to classify
- * @return string $word Classified word
- */
- public static function classify($word)
- {
- return str_replace(" ", "", ucwords(strtr($word, "_-", " ")));
- }
-
- /**
- * Camelize a word. This uses the classify() method and turns the first character to lowercase
- *
- * @param string $word
- * @return string $word
- */
- public static function camelize($word)
- {
- return lcfirst(self::classify($word));
- }
-
- /**
* Plural inflector rules
*
* @var array
*/
- protected static $_plural = array(
+ protected static $plural = array(
'rules' => array(
'/(s)tatus$/i' => '\1\2tatuses',
'/(quiz)$/i' => '\1zes',
@@ -146,7 +113,7 @@ public static function camelize($word)
*
* @var array
*/
- protected static $_singular = array(
+ protected static $singular = array(
'rules' => array(
'/(s)tatuses$/i' => '\1\2tatus',
'/^(.*)(menu)s$/i' => '\1\2',
@@ -198,7 +165,7 @@ public static function camelize($word)
*
* @var array
*/
- protected static $_uninflected = array(
+ protected static $uninflected = array(
'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus',
'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps',
'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder',
@@ -218,34 +185,46 @@ public static function camelize($word)
*
* @var array
*/
- protected static $_cache = array();
+ protected static $cache = array();
/**
* The initial state of Inflector so reset() works.
*
* @var array
*/
- protected static $_initialState = array();
+ protected static $initialState = array();
/**
- * Cache inflected values, and return if already available
+ * Convert word in to the format for a Doctrine table name. Converts 'ModelName' to 'model_name'
*
- * @param string $type Inflection type
- * @param string $key Original value
- * @param string $value Inflected value
- * @return string Inflected value, from cache
+ * @param string $word Word to tableize
+ * @return string $word Tableized word
*/
- protected static function _cache($type, $key, $value = false) {
- $key = '_' . $key;
- $type = '_' . $type;
- if ($value !== false) {
- self::$_cache[$type][$key] = $value;
- return $value;
- }
- if (!isset(self::$_cache[$type][$key])) {
- return false;
- }
- return self::$_cache[$type][$key];
+ public static function tableize($word)
+ {
+ return strtolower(preg_replace('~(?<=\\w)([A-Z])~', '_$1', $word));
+ }
+
+ /**
+ * Convert a word in to the format for a Doctrine class name. Converts 'table_name' to 'TableName'
+ *
+ * @param string $word Word to classify
+ * @return string $word Classified word
+ */
+ public static function classify($word)
+ {
+ return str_replace(" ", "", ucwords(strtr($word, "_-", " ")));
+ }
+
+ /**
+ * Camelize a word. This uses the classify() method and turns the first character to lowercase
+ *
+ * @param string $word
+ * @return string $word
+ */
+ public static function camelize($word)
+ {
+ return lcfirst(self::classify($word));
}
/**
@@ -254,13 +233,14 @@ protected static function _cache($type, $key, $value = false) {
*
* @return void
*/
- public static function reset() {
- if (empty(self::$_initialState)) {
- self::$_initialState = get_class_vars('Inflector');
+ public static function reset()
+ {
+ if (empty(self::$initialState)) {
+ self::$initialState = get_class_vars('Inflector');
return;
}
- foreach (self::$_initialState as $key => $val) {
- if ($key != '_initialState') {
+ foreach (self::$initialState as $key => $val) {
+ if ($key != 'initialState') {
self::${$key} = $val;
}
}
@@ -286,32 +266,31 @@ public static function reset() {
* new rules that are being defined in $rules.
* @return void
*/
- public static function rules($type, $rules, $reset = false) {
- $var = '_' . $type;
-
+ public static function rules($type, $rules, $reset = false)
+ {
foreach ($rules as $rule => $pattern) {
if (is_array($pattern)) {
if ($reset) {
- self::${$var}[$rule] = $pattern;
+ self::${$type}[$rule] = $pattern;
} else {
if ($rule === 'uninflected') {
- self::${$var}[$rule] = array_merge($pattern, self::${$var}[$rule]);
+ self::${$type}[$rule] = array_merge($pattern, self::${$type}[$rule]);
} else {
- self::${$var}[$rule] = $pattern + self::${$var}[$rule];
+ self::${$type}[$rule] = $pattern + self::${$type}[$rule];
}
}
- unset($rules[$rule], self::${$var}['cache' . ucfirst($rule)]);
- if (isset(self::${$var}['merged'][$rule])) {
- unset(self::${$var}['merged'][$rule]);
+ unset($rules[$rule], self::${$type}['cache' . ucfirst($rule)]);
+ if (isset(self::${$type}['merged'][$rule])) {
+ unset(self::${$type}['merged'][$rule]);
}
if ($type === 'plural') {
- self::$_cache['pluralize'] = self::$_cache['tableize'] = array();
+ self::$cache['pluralize'] = self::$cache['tableize'] = array();
} elseif ($type === 'singular') {
- self::$_cache['singularize'] = array();
+ self::$cache['singularize'] = array();
}
}
}
- self::${$var}['rules'] = $rules + self::${$var}['rules'];
+ self::${$type}['rules'] = $rules + self::${$type}['rules'];
}
/**
@@ -320,38 +299,39 @@ public static function rules($type, $rules, $reset = false) {
* @param string $word Word in singular
* @return string Word in plural
*/
- public static function pluralize($word) {
- if (isset(self::$_cache['pluralize'][$word])) {
- return self::$_cache['pluralize'][$word];
+ public static function pluralize($word)
+ {
+ if (isset(self::$cache['pluralize'][$word])) {
+ return self::$cache['pluralize'][$word];
}
- if (!isset(self::$_plural['merged']['irregular'])) {
- self::$_plural['merged']['irregular'] = self::$_plural['irregular'];
+ if (!isset(self::$plural['merged']['irregular'])) {
+ self::$plural['merged']['irregular'] = self::$plural['irregular'];
}
- if (!isset(self::$_plural['merged']['uninflected'])) {
- self::$_plural['merged']['uninflected'] = array_merge(self::$_plural['uninflected'], self::$_uninflected);
+ if (!isset(self::$plural['merged']['uninflected'])) {
+ self::$plural['merged']['uninflected'] = array_merge(self::$plural['uninflected'], self::$uninflected);
}
- if (!isset(self::$_plural['cacheUninflected']) || !isset(self::$_plural['cacheIrregular'])) {
- self::$_plural['cacheUninflected'] = '(?:' . implode('|', self::$_plural['merged']['uninflected']) . ')';
- self::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_plural['merged']['irregular'])) . ')';
+ if (!isset(self::$plural['cacheUninflected']) || !isset(self::$plural['cacheIrregular'])) {
+ self::$plural['cacheUninflected'] = '(?:' . implode('|', self::$plural['merged']['uninflected']) . ')';
+ self::$plural['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$plural['merged']['irregular'])) . ')';
}
- if (preg_match('/(.*)\\b(' . self::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) {
- self::$_cache['pluralize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1);
- return self::$_cache['pluralize'][$word];
+ if (preg_match('/(.*)\\b(' . self::$plural['cacheIrregular'] . ')$/i', $word, $regs)) {
+ self::$cache['pluralize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$plural['merged']['irregular'][strtolower($regs[2])], 1);
+ return self::$cache['pluralize'][$word];
}
- if (preg_match('/^(' . self::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
- self::$_cache['pluralize'][$word] = $word;
+ if (preg_match('/^(' . self::$plural['cacheUninflected'] . ')$/i', $word, $regs)) {
+ self::$cache['pluralize'][$word] = $word;
return $word;
}
- foreach (self::$_plural['rules'] as $rule => $replacement) {
+ foreach (self::$plural['rules'] as $rule => $replacement) {
if (preg_match($rule, $word)) {
- self::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
- return self::$_cache['pluralize'][$word];
+ self::$cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
+ return self::$cache['pluralize'][$word];
}
}
}
@@ -362,49 +342,48 @@ public static function pluralize($word) {
* @param string $word Word in plural
* @return string Word in singular
*/
- public static function singularize($word) {
- if (isset(self::$_cache['singularize'][$word])) {
- return self::$_cache['singularize'][$word];
+ public static function singularize($word)
+ {
+ if (isset(self::$cache['singularize'][$word])) {
+ return self::$cache['singularize'][$word];
}
- if (!isset(self::$_singular['merged']['uninflected'])) {
- self::$_singular['merged']['uninflected'] = array_merge(
- self::$_singular['uninflected'],
- self::$_uninflected
+ if (!isset(self::$singular['merged']['uninflected'])) {
+ self::$singular['merged']['uninflected'] = array_merge(
+ self::$singular['uninflected'],
+ self::$uninflected
);
}
- if (!isset(self::$_singular['merged']['irregular'])) {
- self::$_singular['merged']['irregular'] = array_merge(
- self::$_singular['irregular'],
- array_flip(self::$_plural['irregular'])
+ if (!isset(self::$singular['merged']['irregular'])) {
+ self::$singular['merged']['irregular'] = array_merge(
+ self::$singular['irregular'],
+ array_flip(self::$plural['irregular'])
);
}
- if (!isset(self::$_singular['cacheUninflected']) || !isset(self::$_singular['cacheIrregular'])) {
- self::$_singular['cacheUninflected'] = '(?:' . join('|', self::$_singular['merged']['uninflected']) . ')';
- self::$_singular['cacheIrregular'] = '(?:' . join('|', array_keys(self::$_singular['merged']['irregular'])) . ')';
+ if (!isset(self::$singular['cacheUninflected']) || !isset(self::$singular['cacheIrregular'])) {
+ self::$singular['cacheUninflected'] = '(?:' . join('|', self::$singular['merged']['uninflected']) . ')';
+ self::$singular['cacheIrregular'] = '(?:' . join('|', array_keys(self::$singular['merged']['irregular'])) . ')';
}
- if (preg_match('/(.*)\\b(' . self::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
- self::$_cache['singularize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1);
- return self::$_cache['singularize'][$word];
+ if (preg_match('/(.*)\\b(' . self::$singular['cacheIrregular'] . ')$/i', $word, $regs)) {
+ self::$cache['singularize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$singular['merged']['irregular'][strtolower($regs[2])], 1);
+ return self::$cache['singularize'][$word];
}
- if (preg_match('/^(' . self::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
- self::$_cache['singularize'][$word] = $word;
+ if (preg_match('/^(' . self::$singular['cacheUninflected'] . ')$/i', $word, $regs)) {
+ self::$cache['singularize'][$word] = $word;
return $word;
}
- foreach (self::$_singular['rules'] as $rule => $replacement) {
+ foreach (self::$singular['rules'] as $rule => $replacement) {
if (preg_match($rule, $word)) {
- self::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
- return self::$_cache['singularize'][$word];
+ self::$cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
+ return self::$cache['singularize'][$word];
}
}
- self::$_cache['singularize'][$word] = $word;
+ self::$cache['singularize'][$word] = $word;
return $word;
}
-}
-
-Inflector::reset();
+}
View
170 tests/Doctrine/Tests/Common/Util/InflectorTest.php
@@ -7,128 +7,78 @@
class InflectorTest extends DoctrineTestCase
{
+
+ /**
+ * Singular & Plural test data. Returns an array of sample words.
+ *
+ * @return array
+ */
+ public function dataSampleWords() {
+ Inflector::reset();
+ // in the format array('singular', 'plural')
+ return array(
+ array('categoria', 'categorias'),
+ array('house', 'houses'),
+ array('powerhouse', 'powerhouses'),
+ array('Bus', 'Buses'),
+ array('bus', 'buses'),
+ array('menu', 'menus'),
+ array('news', 'news'),
+ array('food_menu', 'food_menus'),
+ array('Menu', 'Menus'),
+ array('FoodMenu', 'FoodMenus'),
+ array('quiz', 'quizzes'),
+ array('matrix_row', 'matrix_rows'),
+ array('matrix', 'matrices'),
+ array('vertex', 'vertices'),
+ array('index', 'indices'),
+ array('Alias', 'Aliases'),
+ array('Media', 'Media'),
+ array('NodeMedia', 'NodeMedia'),
+ array('alumnus', 'alumni'),
+ array('bacillus', 'bacilli'),
+ array('cactus', 'cacti'),
+ array('focus', 'foci'),
+ array('fungus', 'fungi'),
+ array('nucleus', 'nuclei'),
+ array('octopus', 'octopuses'),
+ array('radius', 'radii'),
+ array('stimulus', 'stimuli'),
+ array('syllabus', 'syllabi'),
+ array('terminus', 'termini'),
+ array('virus', 'viri'),
+ array('person', 'people'),
+ array('glove', 'gloves'),
+ array('crisis', 'crises'),
+ array('tax', 'taxes'),
+ array('wave', 'waves'),
+ array('bureau', 'bureaus'),
+ array('cafe', 'cafes'),
+ array('roof', 'roofs'),
+ array('foe', 'foes'),
+ array('cookie', 'cookies'),
+ array('', ''),
+ );
+ }
+
/**
* testInflectingSingulars method
*
+ * @dataProvider dataSampleWords
* @return void
*/
- public function testInflectingSingulars() {
- Inflector::reset();
- $this->assertEquals(Inflector::singularize('categorias'), 'categoria');
- $this->assertEquals(Inflector::singularize('menus'), 'menu');
- $this->assertEquals(Inflector::singularize('news'), 'news');
- $this->assertEquals(Inflector::singularize('food_menus'), 'food_menu');
- $this->assertEquals(Inflector::singularize('Menus'), 'Menu');
- $this->assertEquals(Inflector::singularize('FoodMenus'), 'FoodMenu');
- $this->assertEquals(Inflector::singularize('houses'), 'house');
- $this->assertEquals(Inflector::singularize('powerhouses'), 'powerhouse');
- $this->assertEquals(Inflector::singularize('quizzes'), 'quiz');
- $this->assertEquals(Inflector::singularize('Buses'), 'Bus');
- $this->assertEquals(Inflector::singularize('buses'), 'bus');
- $this->assertEquals(Inflector::singularize('matrix_rows'), 'matrix_row');
- $this->assertEquals(Inflector::singularize('matrices'), 'matrix');
- $this->assertEquals(Inflector::singularize('vertices'), 'vertex');
- $this->assertEquals(Inflector::singularize('indices'), 'index');
- $this->assertEquals(Inflector::singularize('Aliases'), 'Alias');
- $this->assertEquals(Inflector::singularize('Alias'), 'Alias');
- $this->assertEquals(Inflector::singularize('Media'), 'Media');
- $this->assertEquals(Inflector::singularize('NodeMedia'), 'NodeMedia');
- $this->assertEquals(Inflector::singularize('alumni'), 'alumnus');
- $this->assertEquals(Inflector::singularize('bacilli'), 'bacillus');
- $this->assertEquals(Inflector::singularize('cacti'), 'cactus');
- $this->assertEquals(Inflector::singularize('foci'), 'focus');
- $this->assertEquals(Inflector::singularize('fungi'), 'fungus');
- $this->assertEquals(Inflector::singularize('nuclei'), 'nucleus');
- $this->assertEquals(Inflector::singularize('octopuses'), 'octopus');
- $this->assertEquals(Inflector::singularize('radii'), 'radius');
- $this->assertEquals(Inflector::singularize('stimuli'), 'stimulus');
- $this->assertEquals(Inflector::singularize('syllabi'), 'syllabus');
- $this->assertEquals(Inflector::singularize('termini'), 'terminus');
- $this->assertEquals(Inflector::singularize('viri'), 'virus');
- $this->assertEquals(Inflector::singularize('people'), 'person');
- $this->assertEquals(Inflector::singularize('gloves'), 'glove');
- $this->assertEquals(Inflector::singularize('doves'), 'dove');
- $this->assertEquals(Inflector::singularize('lives'), 'life');
- $this->assertEquals(Inflector::singularize('knives'), 'knife');
- $this->assertEquals(Inflector::singularize('wolves'), 'wolf');
- $this->assertEquals(Inflector::singularize('slaves'), 'slave');
- $this->assertEquals(Inflector::singularize('shelves'), 'shelf');
- $this->assertEquals(Inflector::singularize('taxis'), 'taxi');
- $this->assertEquals(Inflector::singularize('taxes'), 'tax');
- $this->assertEquals(Inflector::singularize('Taxes'), 'Tax');
- $this->assertEquals(Inflector::singularize('AwesomeTaxes'), 'AwesomeTax');
- $this->assertEquals(Inflector::singularize('faxes'), 'fax');
- $this->assertEquals(Inflector::singularize('waxes'), 'wax');
- $this->assertEquals(Inflector::singularize('niches'), 'niche');
- $this->assertEquals(Inflector::singularize('waves'), 'wave');
- $this->assertEquals(Inflector::singularize('bureaus'), 'bureau');
- $this->assertEquals(Inflector::singularize('genetic_analyses'), 'genetic_analysis');
- $this->assertEquals(Inflector::singularize('doctor_diagnoses'), 'doctor_diagnosis');
- $this->assertEquals(Inflector::singularize('parantheses'), 'paranthesis');
- $this->assertEquals(Inflector::singularize('Causes'), 'Cause');
- $this->assertEquals(Inflector::singularize('colossuses'), 'colossus');
- $this->assertEquals(Inflector::singularize('diagnoses'), 'diagnosis');
- $this->assertEquals(Inflector::singularize('bases'), 'basis');
- $this->assertEquals(Inflector::singularize('analyses'), 'analysis');
- $this->assertEquals(Inflector::singularize('curves'), 'curve');
- $this->assertEquals(Inflector::singularize('cafes'), 'cafe');
- $this->assertEquals(Inflector::singularize('roofs'), 'roof');
- $this->assertEquals(Inflector::singularize('foes'), 'foe');
- $this->assertEquals(Inflector::singularize('databases'), 'database');
- $this->assertEquals(Inflector::singularize('cookies'), 'cookie');
- $this->assertEquals(Inflector::singularize(''), '');
+ public function testInflectingSingulars($singular, $plural) {
+ $this->assertEquals($singular, Inflector::singularize($plural), "'$plural' should be singularized to '$singular'");
}
/**
* testInflectingPlurals method
*
+ * @dataProvider dataSampleWords
* @return void
*/
- public function testInflectingPlurals() {
- Inflector::reset();
- $this->assertEquals(Inflector::pluralize('categoria'), 'categorias');
- $this->assertEquals(Inflector::pluralize('house'), 'houses');
- $this->assertEquals(Inflector::pluralize('powerhouse'), 'powerhouses');
- $this->assertEquals(Inflector::pluralize('Bus'), 'Buses');
- $this->assertEquals(Inflector::pluralize('bus'), 'buses');
- $this->assertEquals(Inflector::pluralize('menu'), 'menus');
- $this->assertEquals(Inflector::pluralize('news'), 'news');
- $this->assertEquals(Inflector::pluralize('food_menu'), 'food_menus');
- $this->assertEquals(Inflector::pluralize('Menu'), 'Menus');
- $this->assertEquals(Inflector::pluralize('FoodMenu'), 'FoodMenus');
- $this->assertEquals(Inflector::pluralize('quiz'), 'quizzes');
- $this->assertEquals(Inflector::pluralize('matrix_row'), 'matrix_rows');
- $this->assertEquals(Inflector::pluralize('matrix'), 'matrices');
- $this->assertEquals(Inflector::pluralize('vertex'), 'vertices');
- $this->assertEquals(Inflector::pluralize('index'), 'indices');
- $this->assertEquals(Inflector::pluralize('Alias'), 'Aliases');
- $this->assertEquals(Inflector::pluralize('Aliases'), 'Aliases');
- $this->assertEquals(Inflector::pluralize('Media'), 'Media');
- $this->assertEquals(Inflector::pluralize('NodeMedia'), 'NodeMedia');
- $this->assertEquals(Inflector::pluralize('alumnus'), 'alumni');
- $this->assertEquals(Inflector::pluralize('bacillus'), 'bacilli');
- $this->assertEquals(Inflector::pluralize('cactus'), 'cacti');
- $this->assertEquals(Inflector::pluralize('focus'), 'foci');
- $this->assertEquals(Inflector::pluralize('fungus'), 'fungi');
- $this->assertEquals(Inflector::pluralize('nucleus'), 'nuclei');
- $this->assertEquals(Inflector::pluralize('octopus'), 'octopuses');
- $this->assertEquals(Inflector::pluralize('radius'), 'radii');
- $this->assertEquals(Inflector::pluralize('stimulus'), 'stimuli');
- $this->assertEquals(Inflector::pluralize('syllabus'), 'syllabi');
- $this->assertEquals(Inflector::pluralize('terminus'), 'termini');
- $this->assertEquals(Inflector::pluralize('virus'), 'viri');
- $this->assertEquals(Inflector::pluralize('person'), 'people');
- $this->assertEquals(Inflector::pluralize('people'), 'people');
- $this->assertEquals(Inflector::pluralize('glove'), 'gloves');
- $this->assertEquals(Inflector::pluralize('crisis'), 'crises');
- $this->assertEquals(Inflector::pluralize('tax'), 'taxes');
- $this->assertEquals(Inflector::pluralize('wave'), 'waves');
- $this->assertEquals(Inflector::pluralize('bureau'), 'bureaus');
- $this->assertEquals(Inflector::pluralize('cafe'), 'cafes');
- $this->assertEquals(Inflector::pluralize('roof'), 'roofs');
- $this->assertEquals(Inflector::pluralize('foe'), 'foes');
- $this->assertEquals(Inflector::pluralize('cookie'), 'cookies');
- $this->assertEquals(Inflector::pluralize(''), '');
+ public function testInflectingPlurals($singular, $plural) {
+ $this->assertEquals($plural, Inflector::pluralize($singular), "'$singular' should be pluralized to '$plural'");
}
/**

0 comments on commit bdea5a8

Please sign in to comment.