mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-18 01:07:37 +08:00
Refactored Inflector.
This commit is contained in:
@ -17,16 +17,17 @@ 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',
|
||||||
|
'/^(sea[- ]bass)$/i' => '\1',
|
||||||
'/(m)ove$/i' => '\1oves',
|
'/(m)ove$/i' => '\1oves',
|
||||||
'/(f)oot$/i' => '\1eet',
|
'/(f)oot$/i' => '\1eet',
|
||||||
'/(h)uman$/i' => '\1umans',
|
'/(h)uman$/i' => '\1umans',
|
||||||
'/(s)tatus$/i' => '\1\2tatuses',
|
'/(s)tatus$/i' => '\1tatuses',
|
||||||
'/(s)taff$/i' => '\1taff',
|
'/(s)taff$/i' => '\1taff',
|
||||||
'/(t)ooth$/i' => '\1eeth',
|
'/(t)ooth$/i' => '\1eeth',
|
||||||
'/(quiz)$/i' => '\1zes',
|
'/(quiz)$/i' => '\1zes',
|
||||||
@ -50,58 +51,15 @@ class Inflector
|
|||||||
'/s$/' => 's',
|
'/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',
|
||||||
|
'/(s)tatuses$/i' => '\1tatus',
|
||||||
'/(f)eet$/i' => '\1oot',
|
'/(f)eet$/i' => '\1oot',
|
||||||
'/(t)eeth$/i' => '\1ooth',
|
'/(t)eeth$/i' => '\1ooth',
|
||||||
'/^(.*)(menu)s$/i' => '\1\2',
|
'/^(.*)(menu)s$/i' => '\1\2',
|
||||||
@ -136,114 +94,129 @@ class Inflector
|
|||||||
'/(n)ews$/i' => '\1\2ews',
|
'/(n)ews$/i' => '\1\2ews',
|
||||||
'/eaus$/' => 'eau',
|
'/eaus$/' => 'eau',
|
||||||
'/^(.*us)$/' => '\\1',
|
'/^(.*us)$/' => '\\1',
|
||||||
'/s$/i' => ''
|
'/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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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(
|
||||||
|
Reference in New Issue
Block a user