Browse Source

Refactored Inflector.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
291f3b35d0
  1. 470
      framework/yii/helpers/base/Inflector.php
  2. 2
      tests/unit/framework/helpers/InflectorTest.php

470
framework/yii/helpers/base/Inflector.php

@ -17,233 +17,206 @@ use Yii;
*/ */
class Inflector class Inflector
{ {
/** /**
* @var array rules of plural words * @var array the rules for converting a word into its plural form.
* The keys are the regular expressions and the values are the corresponding replacements.
*/ */
protected static $plural = array( public static $plurals = array(
'rules' => array( '/([nrlm]ese|deer|fish|sheep|measles|ois|pox|media)$/i' => '\1',
'/(m)ove$/i' => '\1oves', '/^(sea[- ]bass)$/i' => '\1',
'/(f)oot$/i' => '\1eet', '/(m)ove$/i' => '\1oves',
'/(h)uman$/i' => '\1umans', '/(f)oot$/i' => '\1eet',
'/(s)tatus$/i' => '\1\2tatuses', '/(h)uman$/i' => '\1umans',
'/(s)taff$/i' => '\1taff', '/(s)tatus$/i' => '\1tatuses',
'/(t)ooth$/i' => '\1eeth', '/(s)taff$/i' => '\1taff',
'/(quiz)$/i' => '\1zes', '/(t)ooth$/i' => '\1eeth',
'/^(ox)$/i' => '\1\2en', '/(quiz)$/i' => '\1zes',
'/([m|l])ouse$/i' => '\1ice', '/^(ox)$/i' => '\1\2en',
'/(matr|vert|ind)(ix|ex)$/i' => '\1ices', '/([m|l])ouse$/i' => '\1ice',
'/(x|ch|ss|sh)$/i' => '\1es', '/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
'/([^aeiouy]|qu)y$/i' => '\1ies', '/(x|ch|ss|sh)$/i' => '\1es',
'/(hive)$/i' => '\1s', '/([^aeiouy]|qu)y$/i' => '\1ies',
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves', '/(hive)$/i' => '\1s',
'/sis$/i' => 'ses', '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
'/([ti])um$/i' => '\1a', '/sis$/i' => 'ses',
'/(p)erson$/i' => '\1eople', '/([ti])um$/i' => '\1a',
'/(m)an$/i' => '\1en', '/(p)erson$/i' => '\1eople',
'/(c)hild$/i' => '\1hildren', '/(m)an$/i' => '\1en',
'/(buffal|tomat|potat|ech|her|vet)o$/i' => '\1oes', '/(c)hild$/i' => '\1hildren',
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i', '/(buffal|tomat|potat|ech|her|vet)o$/i' => '\1oes',
'/us$/i' => 'uses', '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
'/(alias)$/i' => '\1es', '/us$/i' => 'uses',
'/(ax|cris|test)is$/i' => '\1es', '/(alias)$/i' => '\1es',
'/s$/' => 's', '/(ax|cris|test)is$/i' => '\1es',
'/^$/' => '', '/s$/' => 's',
'/$/' => 's', '/^$/' => '',
), '/$/' => 's',
'uninflected' => array(
'.*[nrlm]ese',
'.*deer',
'.*fish',
'.*measles',
'.*ois',
'.*pox',
'.*sheep',
'people'
),
'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'
)
); );
/** /**
* @var array the rules to singular inflector * @var array the rules for converting a word into its singular form.
* The keys are the regular expressions and the values are the corresponding replacements.
*/ */
protected static $singular = array( public static $singulars = array(
'rules' => array( '/([nrlm]ese|deer|fish|sheep|measles|ois|pox|media|ss)$/i' => '\1',
'/(s)tatuses$/i' => '\1\2tatus', '/^(sea[- ]bass)$/i' => '\1',
'/(f)eet$/i' => '\1oot', '/(s)tatuses$/i' => '\1tatus',
'/(t)eeth$/i' => '\1ooth', '/(f)eet$/i' => '\1oot',
'/^(.*)(menu)s$/i' => '\1\2', '/(t)eeth$/i' => '\1ooth',
'/(quiz)zes$/i' => '\\1', '/^(.*)(menu)s$/i' => '\1\2',
'/(matr)ices$/i' => '\1ix', '/(quiz)zes$/i' => '\\1',
'/(vert|ind)ices$/i' => '\1ex', '/(matr)ices$/i' => '\1ix',
'/^(ox)en/i' => '\1', '/(vert|ind)ices$/i' => '\1ex',
'/(alias)(es)*$/i' => '\1', '/^(ox)en/i' => '\1',
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us', '/(alias)(es)*$/i' => '\1',
'/([ftw]ax)es/i' => '\1', '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
'/(cris|ax|test)es$/i' => '\1is', '/([ftw]ax)es/i' => '\1',
'/(shoe|slave)s$/i' => '\1', '/(cris|ax|test)es$/i' => '\1is',
'/(o)es$/i' => '\1', '/(shoe|slave)s$/i' => '\1',
'/ouses$/' => 'ouse', '/(o)es$/i' => '\1',
'/([^a])uses$/' => '\1us', '/ouses$/' => 'ouse',
'/([m|l])ice$/i' => '\1ouse', '/([^a])uses$/' => '\1us',
'/(x|ch|ss|sh)es$/i' => '\1', '/([m|l])ice$/i' => '\1ouse',
'/(m)ovies$/i' => '\1\2ovie', '/(x|ch|ss|sh)es$/i' => '\1',
'/(s)eries$/i' => '\1\2eries', '/(m)ovies$/i' => '\1\2ovie',
'/([^aeiouy]|qu)ies$/i' => '\1y', '/(s)eries$/i' => '\1\2eries',
'/([lr])ves$/i' => '\1f', '/([^aeiouy]|qu)ies$/i' => '\1y',
'/(tive)s$/i' => '\1', '/([lr])ves$/i' => '\1f',
'/(hive)s$/i' => '\1', '/(tive)s$/i' => '\1',
'/(drive)s$/i' => '\1', '/(hive)s$/i' => '\1',
'/([^fo])ves$/i' => '\1fe', '/(drive)s$/i' => '\1',
'/(^analy)ses$/i' => '\1sis', '/([^fo])ves$/i' => '\1fe',
'/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis', '/(^analy)ses$/i' => '\1sis',
'/([ti])a$/i' => '\1um', '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
'/(p)eople$/i' => '\1\2erson', '/([ti])a$/i' => '\1um',
'/(m)en$/i' => '\1an', '/(p)eople$/i' => '\1\2erson',
'/(c)hildren$/i' => '\1\2hild', '/(m)en$/i' => '\1an',
'/(n)ews$/i' => '\1\2ews', '/(c)hildren$/i' => '\1\2hild',
'/eaus$/' => 'eau', '/(n)ews$/i' => '\1\2ews',
'/^(.*us)$/' => '\\1', '/eaus$/' => 'eau',
'/s$/i' => '' '/^(.*us)$/' => '\\1',
), '/s$/i' => '',
'uninflected' => array(
'.*[nrlm]ese',
'.*deer',
'.*fish',
'.*measles',
'.*ois',
'.*pox',
'.*sheep',
'.*ss'
),
'irregular' => array(
'foes' => 'foe',
'waves' => 'wave',
'curves' => 'curve'
)
); );
/** /**
* @var array list of words that should not be inflected * @var array the special rules for converting a word between its plural form and singular form.
* The keys are the special words in singular form, and the values are the corresponding plural form.
*/ */
protected static $uninflected = array( public static $specials = array(
'Amoyese', 'atlas' => 'atlases',
'bison', 'beef' => 'beefs',
'Borghese', 'brother' => 'brothers',
'bream', 'cafe' => 'cafes',
'breeches', 'child' => 'children',
'britches', 'cookie' => 'cookies',
'buffalo', 'corpus' => 'corpuses',
'cantus', 'cow' => 'cows',
'carp', 'curve' => 'curves',
'chassis', 'foe' => 'foes',
'clippers', 'ganglion' => 'ganglions',
'cod', 'genie' => 'genies',
'coitus', 'genus' => 'genera',
'Congoese', 'graffito' => 'graffiti',
'contretemps', 'hoof' => 'hoofs',
'corps', 'loaf' => 'loaves',
'debris', 'man' => 'men',
'diabetes', 'money' => 'monies',
'djinn', 'mongoose' => 'mongooses',
'eland', 'move' => 'moves',
'elk', 'mythos' => 'mythoi',
'equipment', 'niche' => 'niches',
'Faroese', 'numen' => 'numina',
'flounder', 'occiput' => 'occiputs',
'Foochowese', 'octopus' => 'octopuses',
'gallows', 'opus' => 'opuses',
'Genevese', 'ox' => 'oxen',
'Genoese', 'penis' => 'penises',
'Gilbertese', 'sex' => 'sexes',
'graffiti', 'soliloquy' => 'soliloquies',
'headquarters', 'testis' => 'testes',
'herpes', 'trilby' => 'trilbys',
'hijinks', 'turf' => 'turfs',
'Hottentotese', 'wave' => 'waves',
'information', 'Amoyese' => 'Amoyese',
'innings', 'bison' => 'bison',
'jackanapes', 'Borghese' => 'Borghese',
'Kiplingese', 'bream' => 'bream',
'Kongoese', 'breeches' => 'breeches',
'Lucchese', 'britches' => 'britches',
'mackerel', 'buffalo' => 'buffalo',
'Maltese', 'cantus' => 'cantus',
'.*?media', 'carp' => 'carp',
'mews', 'chassis' => 'chassis',
'moose', 'clippers' => 'clippers',
'mumps', 'cod' => 'cod',
'Nankingese', 'coitus' => 'coitus',
'news', 'Congoese' => 'Congoese',
'nexus', 'contretemps' => 'contretemps',
'Niasese', 'corps' => 'corps',
'Pekingese', 'debris' => 'debris',
'Piedmontese', 'diabetes' => 'diabetes',
'pincers', 'djinn' => 'djinn',
'Pistoiese', 'eland' => 'eland',
'pliers', 'elk' => 'elk',
'Portuguese', 'equipment' => 'equipment',
'proceedings', 'Faroese' => 'Faroese',
'rabies', 'flounder' => 'flounder',
'rice', 'Foochowese' => 'Foochowese',
'rhinoceros', 'gallows' => 'gallows',
'salmon', 'Genevese' => 'Genevese',
'Sarawakese', 'Genoese' => 'Genoese',
'scissors', 'Gilbertese' => 'Gilbertese',
'sea[- ]bass', 'graffiti' => 'graffiti',
'series', 'headquarters' => 'headquarters',
'Shavese', 'herpes' => 'herpes',
'shears', 'hijinks' => 'hijinks',
'siemens', 'Hottentotese' => 'Hottentotese',
'species', 'information' => 'information',
'swine', 'innings' => 'innings',
'testes', 'jackanapes' => 'jackanapes',
'trousers', 'Kiplingese' => 'Kiplingese',
'trout', 'Kongoese' => 'Kongoese',
'tuna', 'Lucchese' => 'Lucchese',
'Vermontese', 'mackerel' => 'mackerel',
'Wenchowese', 'Maltese' => 'Maltese',
'whiting', 'mews' => 'mews',
'wildebeest', 'moose' => 'moose',
'Yengeese' 'mumps' => 'mumps',
'Nankingese' => 'Nankingese',
'news' => 'news',
'nexus' => 'nexus',
'Niasese' => 'Niasese',
'Pekingese' => 'Pekingese',
'Piedmontese' => 'Piedmontese',
'pincers' => 'pincers',
'Pistoiese' => 'Pistoiese',
'pliers' => 'pliers',
'Portuguese' => 'Portuguese',
'proceedings' => 'proceedings',
'rabies' => 'rabies',
'rice' => 'rice',
'rhinoceros' => 'rhinoceros',
'salmon' => 'salmon',
'Sarawakese' => 'Sarawakese',
'scissors' => 'scissors',
'series' => 'series',
'Shavese' => 'Shavese',
'shears' => 'shears',
'siemens' => 'siemens',
'species' => 'species',
'swine' => 'swine',
'testes' => 'testes',
'trousers' => 'trousers',
'trout' => 'trout',
'tuna' => 'tuna',
'Vermontese' => 'Vermontese',
'Wenchowese' => 'Wenchowese',
'whiting' => 'whiting',
'wildebeest' => 'wildebeest',
'Yengeese' => 'Yengeese',
); );
/** /**
* @var array map of special chars and its translation * @var array map of special chars and its translation. This is used by [[slug()]].
*/ */
protected static $transliteration = array( public static $transliteration = array(
'/ä|æ|ǽ/' => 'ae', '/ä|æ|ǽ/' => 'ae',
'/ö|œ/' => 'oe', '/ö|œ/' => 'oe',
'/ü/' => 'ue', '/ü/' => 'ue',
@ -305,19 +278,10 @@ class Inflector
*/ */
public static function pluralize($word) public static function pluralize($word)
{ {
$unInflected = ArrayHelper::merge(static::$plural['uninflected'], static::$uninflected); if (isset(self::$specials[$word])) {
$irregular = array_keys(static::$plural['irregular']); return self::$specials[$word];
}
$unInflectedRegex = '(?:' . implode('|', $unInflected) . ')'; foreach (static::$plurals as $rule => $replacement) {
$irregularRegex = '(?:' . implode('|', $irregular) . ')';
if (preg_match('/(.*)\\b(' . $irregularRegex . ')$/i', $word, $regs))
return $regs[1] . substr($word, 0, 1) . substr(static::$plural['irregular'][strtolower($regs[2])], 1);
if (preg_match('/^(' . $unInflectedRegex . ')$/i', $word, $regs))
return $word;
foreach (static::$plural['rules'] as $rule => $replacement) {
if (preg_match($rule, $word)) { if (preg_match($rule, $word)) {
return preg_replace($rule, $replacement, $word); return preg_replace($rule, $replacement, $word);
} }
@ -332,27 +296,11 @@ class Inflector
*/ */
public static function singularize($word) public static function singularize($word)
{ {
$result = array_search($word, self::$specials, true);
$unInflected = ArrayHelper::merge(static::$singular['uninflected'], static::$uninflected); if ($result !== false) {
return $result;
$irregular = array_merge( }
static::$singular['irregular'], foreach (static::$singulars as $rule => $replacement) {
array_flip(static::$plural['irregular'])
);
$unInflectedRegex = '(?:' . implode('|', $unInflected) . ')';
$irregularRegex = '(?:' . implode('|', array_keys($irregular)) . ')';
if (preg_match('/(.*)\\b(' . $irregularRegex . ')$/i', $word, $regs))
return $regs[1] . substr($word, 0, 1) . substr($irregular[strtolower($regs[2])], 1);
if (preg_match('/^(' . $unInflectedRegex . ')$/i', $word, $regs))
return $word;
foreach (static::$singular['rules'] as $rule => $replacement) {
if (preg_match($rule, $word)) { if (preg_match($rule, $word)) {
return preg_replace($rule, $replacement, $word); return preg_replace($rule, $replacement, $word);
} }
@ -369,7 +317,6 @@ class Inflector
*/ */
public static function titleize($words, $ucAll = false) public static function titleize($words, $ucAll = false)
{ {
$words = static::humanize(static::underscore($words), $ucAll); $words = static::humanize(static::underscore($words), $ucAll);
return $ucAll ? ucwords($words) : ucfirst($words); return $ucAll ? ucwords($words) : ucfirst($words);
} }
@ -492,7 +439,6 @@ class Inflector
*/ */
public static function slug($string, $replacement = '-') public static function slug($string, $replacement = '-')
{ {
$map = static::$transliteration + array( $map = static::$transliteration + array(
'/[^\w\s]/' => ' ', '/[^\w\s]/' => ' ',
'/\\s+/' => $replacement, '/\\s+/' => $replacement,
@ -521,20 +467,12 @@ class Inflector
{ {
if (in_array(($number % 100), range(11, 13))) { if (in_array(($number % 100), range(11, 13))) {
return $number . 'th'; return $number . 'th';
} else { }
switch (($number % 10)) { switch (($number % 10)) {
case 1: case 1: return $number . 'st';
return $number . 'st'; case 2: return $number . 'nd';
break; case 3: return $number . 'rd';
case 2: default: return $number . 'th';
return $number . 'nd';
break;
case 3:
return $number . 'rd';
default:
return $number . 'th';
break;
}
} }
} }
} }

2
tests/unit/framework/helpers/InflectorTest.php

@ -8,8 +8,6 @@ use yiiunit\TestCase;
class InflectorTest extends TestCase class InflectorTest extends TestCase
{ {
public function testPluralize() public function testPluralize()
{ {
$testData = array( $testData = array(

Loading…
Cancel
Save