Permalink
Browse files

Adding pluralize/signularize functionality to inflector. This is to b…

…e used in Doctrine/ORM/Tools/EntityGenerator to fix [DDC-2160] properly.
  • Loading branch information...
1 parent a836c86 commit 1dff8090ae2ee32741ac18cc4f72e767372ff1d7 @jbergler committed Nov 23, 2012
Showing with 575 additions and 1 deletion.
  1. +339 −1 lib/Doctrine/Common/Util/Inflector.php
  2. +236 −0 tests/Doctrine/Tests/Common/Util/InflectorTest.php
View
340 lib/Doctrine/Common/Util/Inflector.php
@@ -26,7 +26,9 @@
*
* The methods in these classes are from several different sources collected
* across several different php projects and several different authors. The
- * original author names and emails are not known
+ * original author names and emails are not known.
+ *
+ * Plurialize & Singularize implementation are borrowed from CakePHP with some modifications.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
@@ -69,4 +71,340 @@ public static function camelize($word)
{
return lcfirst(self::classify($word));
}
+
+ /**
+ * Plural inflector rules
+ *
+ * @var array
+ */
+ protected static $_plural = array(
+ 'rules' => array(
+ '/(s)tatus$/i' => '\1\2tatuses',
+ '/(quiz)$/i' => '\1zes',
+ '/^(ox)$/i' => '\1\2en',
+ '/([m|l])ouse$/i' => '\1ice',
+ '/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
+ '/(x|ch|ss|sh)$/i' => '\1es',
+ '/([^aeiouy]|qu)y$/i' => '\1ies',
+ '/(hive)$/i' => '\1s',
+ '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
+ '/sis$/i' => 'ses',
+ '/([ti])um$/i' => '\1a',
+ '/(p)erson$/i' => '\1eople',
+ '/(m)an$/i' => '\1en',
+ '/(c)hild$/i' => '\1hildren',
+ '/(buffal|tomat)o$/i' => '\1\2oes',
+ '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
+ '/us$/i' => 'uses',
+ '/(alias)$/i' => '\1es',
+ '/(ax|cris|test)is$/i' => '\1es',
+ '/s$/' => 's',
+ '/^$/' => '',
+ '/$/' => 's',
+ ),
+ 'uninflected' => array(
+ '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', 'people', 'cookie'
+ ),
+ 'irregular' => array(
+ 'atlas' => 'atlases',
+ 'beef' => 'beefs',
+ 'brother' => 'brothers',
+ 'cafe' => 'cafes',
+ 'child' => 'children',
+ 'cookie' => 'cookies',
+ 'corpus' => 'corpuses',
+ 'cow' => 'cows',
+ 'ganglion' => 'ganglions',
+ 'genie' => 'genies',
+ 'genus' => 'genera',
+ 'graffito' => 'graffiti',
+ 'hoof' => 'hoofs',
+ 'loaf' => 'loaves',
+ 'man' => 'men',
+ 'money' => 'monies',
+ 'mongoose' => 'mongooses',
+ 'move' => 'moves',
+ 'mythos' => 'mythoi',
+ 'niche' => 'niches',
+ 'numen' => 'numina',
+ 'occiput' => 'occiputs',
+ 'octopus' => 'octopuses',
+ 'opus' => 'opuses',
+ 'ox' => 'oxen',
+ 'penis' => 'penises',
+ 'person' => 'people',
+ 'sex' => 'sexes',
+ 'soliloquy' => 'soliloquies',
+ 'testis' => 'testes',
+ 'trilby' => 'trilbys',
+ 'turf' => 'turfs'
+ )
+ );
+
+ /**
+ * Singular inflector rules
+ *
+ * @var array
+ */
+ protected static $_singular = array(
+ 'rules' => array(
+ '/(s)tatuses$/i' => '\1\2tatus',
+ '/^(.*)(menu)s$/i' => '\1\2',
+ '/(quiz)zes$/i' => '\\1',
+ '/(matr)ices$/i' => '\1ix',
+ '/(vert|ind)ices$/i' => '\1ex',
+ '/^(ox)en/i' => '\1',
+ '/(alias)(es)*$/i' => '\1',
+ '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
+ '/([ftw]ax)es/i' => '\1',
+ '/(cris|ax|test)es$/i' => '\1is',
+ '/(shoe|slave)s$/i' => '\1',
+ '/(o)es$/i' => '\1',
+ '/ouses$/' => 'ouse',
+ '/([^a])uses$/' => '\1us',
+ '/([m|l])ice$/i' => '\1ouse',
+ '/(x|ch|ss|sh)es$/i' => '\1',
+ '/(m)ovies$/i' => '\1\2ovie',
+ '/(s)eries$/i' => '\1\2eries',
+ '/([^aeiouy]|qu)ies$/i' => '\1y',
+ '/([lr])ves$/i' => '\1f',
+ '/(tive)s$/i' => '\1',
+ '/(hive)s$/i' => '\1',
+ '/(drive)s$/i' => '\1',
+ '/([^fo])ves$/i' => '\1fe',
+ '/(^analy)ses$/i' => '\1sis',
+ '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
+ '/([ti])a$/i' => '\1um',
+ '/(p)eople$/i' => '\1\2erson',
+ '/(m)en$/i' => '\1an',
+ '/(c)hildren$/i' => '\1\2hild',
+ '/(n)ews$/i' => '\1\2ews',
+ '/eaus$/' => 'eau',
+ '/^(.*us)$/' => '\\1',
+ '/s$/i' => ''
+ ),
+ 'uninflected' => array(
+ '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss'
+ ),
+ 'irregular' => array(
+ 'foes' => 'foe',
+ 'waves' => 'wave',
+ 'curves' => 'curve'
+ )
+ );
+
+ /**
+ * Words that should not be inflected
+ *
+ * @var 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',
+ 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
+ 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings',
+ 'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', '.*?media',
+ 'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
+ 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
+ 'proceedings', 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
+ 'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
+ 'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
+ 'Yengeese'
+ );
+
+ /**
+ * Method cache array.
+ *
+ * @var array
+ */
+ protected static $_cache = array();
+
+ /**
+ * The initial state of Inflector so reset() works.
+ *
+ * @var array
+ */
+ protected static $_initialState = array();
+
+ /**
+ * Cache inflected values, and return if already available
+ *
+ * @param string $type Inflection type
+ * @param string $key Original value
+ * @param string $value Inflected value
+ * @return string Inflected value, from cache
+ */
+ 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];
+ }
+
+ /**
+ * Clears Inflectors inflected value caches. And resets the inflection
+ * rules to the initial values.
+ *
+ * @return void
+ */
+ public static function reset() {
+ if (empty(self::$_initialState)) {
+ self::$_initialState = get_class_vars('Inflector');
+ return;
+ }
+ foreach (self::$_initialState as $key => $val) {
+ if ($key != '_initialState') {
+ self::${$key} = $val;
+ }
+ }
+ }
+
+ /**
+ * Adds custom inflection $rules, of either 'plural' or 'singular' $type.
+ *
+ * ### Usage:
+ *
+ * {{{
+ * Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
+ * Inflector::rules('plural', array(
+ * 'rules' => array('/^(inflect)ors$/i' => '\1ables'),
+ * 'uninflected' => array('dontinflectme'),
+ * 'irregular' => array('red' => 'redlings')
+ * ));
+ * }}}
+ *
+ * @param string $type The type of inflection, either 'plural' or 'singular'
+ * @param array $rules Array of rules to be added.
+ * @param boolean $reset If true, will unset default inflections for all
+ * new rules that are being defined in $rules.
+ * @return void
+ */
+ public static function rules($type, $rules, $reset = false) {
+ $var = '_' . $type;
+
+ foreach ($rules as $rule => $pattern) {
+ if (is_array($pattern)) {
+ if ($reset) {
+ self::${$var}[$rule] = $pattern;
+ } else {
+ if ($rule === 'uninflected') {
+ self::${$var}[$rule] = array_merge($pattern, self::${$var}[$rule]);
+ } else {
+ self::${$var}[$rule] = $pattern + self::${$var}[$rule];
+ }
+ }
+ unset($rules[$rule], self::${$var}['cache' . ucfirst($rule)]);
+ if (isset(self::${$var}['merged'][$rule])) {
+ unset(self::${$var}['merged'][$rule]);
+ }
+ if ($type === 'plural') {
+ self::$_cache['pluralize'] = self::$_cache['tableize'] = array();
+ } elseif ($type === 'singular') {
+ self::$_cache['singularize'] = array();
+ }
+ }
+ }
+ self::${$var}['rules'] = $rules + self::${$var}['rules'];
+ }
+
+ /**
+ * Return $word in plural form.
+ *
+ * @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];
+ }
+
+ 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['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('/^(' . self::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
+ self::$_cache['pluralize'][$word] = $word;
+ return $word;
+ }
+
+ 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];
+ }
+ }
+ }
+
+ /**
+ * Return $word in singular form.
+ *
+ * @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];
+ }
+
+ 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['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('/^(' . self::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
+ self::$_cache['singularize'][$word] = $word;
+ return $word;
+ }
+
+ 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] = $word;
+ return $word;
+ }
}
+
+Inflector::reset();
View
236 tests/Doctrine/Tests/Common/Util/InflectorTest.php
@@ -0,0 +1,236 @@
+<?php
+
+namespace Doctrine\Tests\Common\Util
+{
+ use Doctrine\Tests\DoctrineTestCase;
+ use Doctrine\Common\Util\Inflector;
+
+ class InflectorTest extends DoctrineTestCase
+ {
+ /**
+ * testInflectingSingulars method
+ *
+ * @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(''), '');
+ }
+
+ /**
+ * testInflectingPlurals method
+ *
+ * @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(''), '');
+ }
+
+ /**
+ * testCustomPluralRule method
+ *
+ * @return void
+ */
+ public function testCustomPluralRule() {
+ Inflector::reset();
+ Inflector::rules('plural', array('/^(custom)$/i' => '\1izables'));
+ $this->assertEquals(Inflector::pluralize('custom'), 'customizables');
+
+ Inflector::rules('plural', array('uninflected' => array('uninflectable')));
+ $this->assertEquals(Inflector::pluralize('uninflectable'), 'uninflectable');
+
+ Inflector::rules('plural', array(
+ 'rules' => array('/^(alert)$/i' => '\1ables'),
+ 'uninflected' => array('noflect', 'abtuse'),
+ 'irregular' => array('amaze' => 'amazable', 'phone' => 'phonezes')
+ ));
+ $this->assertEquals(Inflector::pluralize('noflect'), 'noflect');
+ $this->assertEquals(Inflector::pluralize('abtuse'), 'abtuse');
+ $this->assertEquals(Inflector::pluralize('alert'), 'alertables');
+ $this->assertEquals(Inflector::pluralize('amaze'), 'amazable');
+ $this->assertEquals(Inflector::pluralize('phone'), 'phonezes');
+ }
+
+ /**
+ * testCustomSingularRule method
+ *
+ * @return void
+ */
+ public function testCustomSingularRule() {
+ Inflector::reset();
+ Inflector::rules('singular', array('/(eple)r$/i' => '\1', '/(jente)r$/i' => '\1'));
+
+ $this->assertEquals(Inflector::singularize('epler'), 'eple');
+ $this->assertEquals(Inflector::singularize('jenter'), 'jente');
+
+ Inflector::rules('singular', array(
+ 'rules' => array('/^(bil)er$/i' => '\1', '/^(inflec|contribu)tors$/i' => '\1ta'),
+ 'uninflected' => array('singulars'),
+ 'irregular' => array('spins' => 'spinor')
+ ));
+
+ $this->assertEquals(Inflector::singularize('inflectors'), 'inflecta');
+ $this->assertEquals(Inflector::singularize('contributors'), 'contributa');
+ $this->assertEquals(Inflector::singularize('spins'), 'spinor');
+ $this->assertEquals(Inflector::singularize('singulars'), 'singulars');
+ }
+
+ /**
+ * test that setting new rules clears the inflector caches.
+ *
+ * @return void
+ */
+ public function testRulesClearsCaches() {
+ Inflector::reset();
+ $this->assertEquals(Inflector::singularize('Bananas'), 'Banana');
+ $this->assertEquals(Inflector::pluralize('Banana'), 'Bananas');
+
+ Inflector::rules('singular', array(
+ 'rules' => array('/(.*)nas$/i' => '\1zzz')
+ ));
+ $this->assertEquals('Banazzz', Inflector::singularize('Bananas'), 'Was inflected with old rules.');
+
+ Inflector::rules('plural', array(
+ 'rules' => array('/(.*)na$/i' => '\1zzz'),
+ 'irregular' => array('corpus' => 'corpora')
+ ));
+ $this->assertEquals(Inflector::pluralize('Banana'), 'Banazzz', 'Was inflected with old rules.');
+ $this->assertEquals(Inflector::pluralize('corpus'), 'corpora', 'Was inflected with old irregular form.');
+ }
+
+ /**
+ * Test resetting inflection rules.
+ *
+ * @return void
+ */
+ public function testCustomRuleWithReset() {
+ Inflector::reset();
+ $uninflected = array('atlas', 'lapis', 'onibus', 'pires', 'virus', '.*x');
+ $pluralIrregular = array('as' => 'ases');
+
+ Inflector::rules('singular', array(
+ 'rules' => array('/^(.*)(a|e|o|u)is$/i' => '\1\2l'),
+ 'uninflected' => $uninflected,
+ ), true);
+
+ Inflector::rules('plural', array(
+ 'rules' => array(
+ '/^(.*)(a|e|o|u)l$/i' => '\1\2is',
+ ),
+ 'uninflected' => $uninflected,
+ 'irregular' => $pluralIrregular
+ ), true);
+
+ $this->assertEquals(Inflector::pluralize('Alcool'), 'Alcoois');
+ $this->assertEquals(Inflector::pluralize('Atlas'), 'Atlas');
+ $this->assertEquals(Inflector::singularize('Alcoois'), 'Alcool');
+ $this->assertEquals(Inflector::singularize('Atlas'), 'Atlas');
+ }
+ }
+}
+

0 comments on commit 1dff809

Please sign in to comment.