2014-04-23 10:51:50 +08:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
2018-03-18 09:24:15 +08:00
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
|
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2014-04-23 10:51:50 +08:00
|
|
|
|
*
|
|
|
|
|
* Licensed under The MIT License
|
|
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
|
*
|
2018-03-18 09:24:15 +08:00
|
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
|
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2014-04-23 10:51:50 +08:00
|
|
|
|
* @package Cake.Utility
|
|
|
|
|
* @since CakePHP(tm) v 0.2.9
|
2018-03-18 09:24:15 +08:00
|
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2014-04-23 10:51:50 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Pluralize and singularize English words.
|
|
|
|
|
*
|
|
|
|
|
* Inflector pluralizes and singularizes English nouns.
|
|
|
|
|
* Used by CakePHP's naming conventions throughout the framework.
|
|
|
|
|
*
|
|
|
|
|
* @package Cake.Utility
|
2018-03-18 09:24:15 +08:00
|
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html
|
2014-04-23 10:51:50 +08:00
|
|
|
|
*/
|
|
|
|
|
class Inflector {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Plural inflector rules
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected static $_plural = array(
|
|
|
|
|
'rules' => array(
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'/(s)tatus$/i' => '\1tatuses',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'/(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|([lre])f)$/i' => '\1\2ves',
|
|
|
|
|
'/sis$/i' => 'ses',
|
|
|
|
|
'/([ti])um$/i' => '\1a',
|
|
|
|
|
'/(p)erson$/i' => '\1eople',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'/(?<!u)(m)an$/i' => '\1en',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'/(c)hild$/i' => '\1hildren',
|
|
|
|
|
'/(buffal|tomat)o$/i' => '\1\2oes',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin)us$/i' => '\1i',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'/us$/i' => 'uses',
|
|
|
|
|
'/(alias)$/i' => '\1es',
|
|
|
|
|
'/(ax|cris|test)is$/i' => '\1es',
|
|
|
|
|
'/s$/' => 's',
|
|
|
|
|
'/^$/' => '',
|
|
|
|
|
'/$/' => 's',
|
|
|
|
|
),
|
|
|
|
|
'uninflected' => array(
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'.*[nrlm]ese',
|
|
|
|
|
'.*data',
|
|
|
|
|
'.*deer',
|
|
|
|
|
'.*fish',
|
|
|
|
|
'.*measles',
|
|
|
|
|
'.*ois',
|
|
|
|
|
'.*pox',
|
|
|
|
|
'.*sheep',
|
|
|
|
|
'people',
|
|
|
|
|
'feedback',
|
|
|
|
|
'stadia'
|
2014-04-23 10:51:50 +08:00
|
|
|
|
),
|
|
|
|
|
'irregular' => array(
|
|
|
|
|
'atlas' => 'atlases',
|
|
|
|
|
'beef' => 'beefs',
|
|
|
|
|
'brief' => 'briefs',
|
|
|
|
|
'brother' => 'brothers',
|
|
|
|
|
'cafe' => 'cafes',
|
|
|
|
|
'child' => 'children',
|
|
|
|
|
'cookie' => 'cookies',
|
|
|
|
|
'corpus' => 'corpuses',
|
|
|
|
|
'cow' => 'cows',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'criterion' => 'criteria',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'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',
|
|
|
|
|
'potato' => 'potatoes',
|
|
|
|
|
'hero' => 'heroes',
|
|
|
|
|
'tooth' => 'teeth',
|
|
|
|
|
'goose' => 'geese',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'foot' => 'feet',
|
|
|
|
|
'sieve' => 'sieves'
|
2014-04-23 10:51:50 +08:00
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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)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',
|
|
|
|
|
'/(tive)s$/i' => '\1',
|
|
|
|
|
'/(hive)s$/i' => '\1',
|
|
|
|
|
'/(drive)s$/i' => '\1',
|
|
|
|
|
'/([le])ves$/i' => '\1f',
|
|
|
|
|
'/([^rfoa])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(
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'.*data',
|
|
|
|
|
'.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss', 'feedback'
|
2014-04-23 10:51:50 +08:00
|
|
|
|
),
|
|
|
|
|
'irregular' => array(
|
|
|
|
|
'foes' => 'foe',
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'proceedings', 'rabies', 'research', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
|
|
|
|
|
'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
|
|
|
|
|
'Yengeese'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Default map of accented and special characters to ASCII characters
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected static $_transliteration = array(
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A',
|
|
|
|
|
'/Æ|Ǽ/' => 'AE',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'/Ä/' => 'Ae',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'/Ç|Ć|Ĉ|Ċ|Č/' => 'C',
|
|
|
|
|
'/Ð|Ď|Đ/' => 'D',
|
|
|
|
|
'/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/' => 'E',
|
|
|
|
|
'/Ĝ|Ğ|Ġ|Ģ|Ґ/' => 'G',
|
|
|
|
|
'/Ĥ|Ħ/' => 'H',
|
|
|
|
|
'/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ|І/' => 'I',
|
|
|
|
|
'/IJ/' => 'IJ',
|
|
|
|
|
'/Ĵ/' => 'J',
|
|
|
|
|
'/Ķ/' => 'K',
|
|
|
|
|
'/Ĺ|Ļ|Ľ|Ŀ|Ł/' => 'L',
|
|
|
|
|
'/Ñ|Ń|Ņ|Ň/' => 'N',
|
|
|
|
|
'/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ/' => 'O',
|
|
|
|
|
'/Œ/' => 'OE',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'/Ö/' => 'Oe',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'/Ŕ|Ŗ|Ř/' => 'R',
|
|
|
|
|
'/Ś|Ŝ|Ş|Ș|Š/' => 'S',
|
|
|
|
|
'/ẞ/' => 'SS',
|
|
|
|
|
'/Ţ|Ț|Ť|Ŧ/' => 'T',
|
|
|
|
|
'/Þ/' => 'TH',
|
|
|
|
|
'/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/' => 'U',
|
|
|
|
|
'/Ü/' => 'Ue',
|
|
|
|
|
'/Ŵ/' => 'W',
|
|
|
|
|
'/Ý|Ÿ|Ŷ/' => 'Y',
|
|
|
|
|
'/Є/' => 'Ye',
|
|
|
|
|
'/Ї/' => 'Yi',
|
|
|
|
|
'/Ź|Ż|Ž/' => 'Z',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' => 'a',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'/ä|æ|ǽ/' => 'ae',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'/ç|ć|ĉ|ċ|č/' => 'c',
|
|
|
|
|
'/ð|ď|đ/' => 'd',
|
|
|
|
|
'/è|é|ê|ë|ē|ĕ|ė|ę|ě/' => 'e',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'/ƒ/' => 'f',
|
|
|
|
|
'/ĝ|ğ|ġ|ģ|ґ/' => 'g',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'/ĥ|ħ/' => 'h',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı|і/' => 'i',
|
|
|
|
|
'/ij/' => 'ij',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'/ĵ/' => 'j',
|
|
|
|
|
'/ķ/' => 'k',
|
|
|
|
|
'/ĺ|ļ|ľ|ŀ|ł/' => 'l',
|
|
|
|
|
'/ñ|ń|ņ|ň|ʼn/' => 'n',
|
|
|
|
|
'/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º/' => 'o',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'/ö|œ/' => 'oe',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'/ŕ|ŗ|ř/' => 'r',
|
|
|
|
|
'/ś|ŝ|ş|ș|š|ſ/' => 's',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'/ß/' => 'ss',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'/ţ|ț|ť|ŧ/' => 't',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'/þ/' => 'th',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'/ü/' => 'ue',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'/ŵ/' => 'w',
|
2016-02-25 20:25:24 +08:00
|
|
|
|
'/ý|ÿ|ŷ/' => 'y',
|
|
|
|
|
'/є/' => 'ye',
|
|
|
|
|
'/ї/' => 'yi',
|
2014-04-23 10:51:50 +08:00
|
|
|
|
'/ź|ż|ž/' => 'z',
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::$_cache[$type][$key] = $value;
|
2014-04-23 10:51:50 +08:00
|
|
|
|
return $value;
|
|
|
|
|
}
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (!isset(static::$_cache[$type][$key])) {
|
2014-04-23 10:51:50 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2016-02-25 20:25:24 +08:00
|
|
|
|
return static::$_cache[$type][$key];
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Clears Inflectors inflected value caches. And resets the inflection
|
|
|
|
|
* rules to the initial values.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function reset() {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (empty(static::$_initialState)) {
|
|
|
|
|
static::$_initialState = get_class_vars('Inflector');
|
2014-04-23 10:51:50 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-02-25 20:25:24 +08:00
|
|
|
|
foreach (static::$_initialState as $key => $val) {
|
2014-04-23 10:51:50 +08:00
|
|
|
|
if ($key !== '_initialState') {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::${$key} = $val;
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adds custom inflection $rules, of either 'plural', 'singular' or 'transliteration' $type.
|
|
|
|
|
*
|
|
|
|
|
* ### Usage:
|
|
|
|
|
*
|
2016-02-25 20:25:24 +08:00
|
|
|
|
* ```
|
2014-04-23 10:51:50 +08:00
|
|
|
|
* 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')
|
|
|
|
|
* ));
|
|
|
|
|
* Inflector::rules('transliteration', array('/å/' => 'aa'));
|
2016-02-25 20:25:24 +08:00
|
|
|
|
* ```
|
2014-04-23 10:51:50 +08:00
|
|
|
|
*
|
|
|
|
|
* @param string $type The type of inflection, either 'plural', 'singular' or 'transliteration'
|
|
|
|
|
* @param array $rules Array of rules to be added.
|
2016-02-25 20:25:24 +08:00
|
|
|
|
* @param bool $reset If true, will unset default inflections for all
|
2014-04-23 10:51:50 +08:00
|
|
|
|
* new rules that are being defined in $rules.
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function rules($type, $rules, $reset = false) {
|
|
|
|
|
$var = '_' . $type;
|
|
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
|
case 'transliteration':
|
|
|
|
|
if ($reset) {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::$_transliteration = $rules;
|
2014-04-23 10:51:50 +08:00
|
|
|
|
} else {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::$_transliteration = $rules + static::$_transliteration;
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
foreach ($rules as $rule => $pattern) {
|
|
|
|
|
if (is_array($pattern)) {
|
|
|
|
|
if ($reset) {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::${$var}[$rule] = $pattern;
|
2014-04-23 10:51:50 +08:00
|
|
|
|
} else {
|
|
|
|
|
if ($rule === 'uninflected') {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::${$var}[$rule] = array_merge($pattern, static::${$var}[$rule]);
|
2014-04-23 10:51:50 +08:00
|
|
|
|
} else {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::${$var}[$rule] = $pattern + static::${$var}[$rule];
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-25 20:25:24 +08:00
|
|
|
|
unset($rules[$rule], static::${$var}['cache' . ucfirst($rule)]);
|
|
|
|
|
if (isset(static::${$var}['merged'][$rule])) {
|
|
|
|
|
unset(static::${$var}['merged'][$rule]);
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
if ($type === 'plural') {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::$_cache['pluralize'] = static::$_cache['tableize'] = array();
|
2014-04-23 10:51:50 +08:00
|
|
|
|
} elseif ($type === 'singular') {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::$_cache['singularize'] = array();
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::${$var}['rules'] = $rules + static::${$var}['rules'];
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return $word in plural form.
|
|
|
|
|
*
|
|
|
|
|
* @param string $word Word in singular
|
|
|
|
|
* @return string Word in plural
|
2018-03-18 09:24:15 +08:00
|
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::pluralize
|
2014-04-23 10:51:50 +08:00
|
|
|
|
*/
|
|
|
|
|
public static function pluralize($word) {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (isset(static::$_cache['pluralize'][$word])) {
|
|
|
|
|
return static::$_cache['pluralize'][$word];
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (!isset(static::$_plural['merged']['irregular'])) {
|
|
|
|
|
static::$_plural['merged']['irregular'] = static::$_plural['irregular'];
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (!isset(static::$_plural['merged']['uninflected'])) {
|
|
|
|
|
static::$_plural['merged']['uninflected'] = array_merge(static::$_plural['uninflected'], static::$_uninflected);
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (!isset(static::$_plural['cacheUninflected']) || !isset(static::$_plural['cacheIrregular'])) {
|
|
|
|
|
static::$_plural['cacheUninflected'] = '(?:' . implode('|', static::$_plural['merged']['uninflected']) . ')';
|
|
|
|
|
static::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(static::$_plural['merged']['irregular'])) . ')';
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (preg_match('/(.*?(?:\\b|_))(' . static::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) {
|
|
|
|
|
static::$_cache['pluralize'][$word] = $regs[1] .
|
|
|
|
|
substr($regs[2], 0, 1) .
|
|
|
|
|
substr(static::$_plural['merged']['irregular'][strtolower($regs[2])], 1);
|
|
|
|
|
return static::$_cache['pluralize'][$word];
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (preg_match('/^(' . static::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
|
|
|
|
|
static::$_cache['pluralize'][$word] = $word;
|
2014-04-23 10:51:50 +08:00
|
|
|
|
return $word;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 20:25:24 +08:00
|
|
|
|
foreach (static::$_plural['rules'] as $rule => $replacement) {
|
2014-04-23 10:51:50 +08:00
|
|
|
|
if (preg_match($rule, $word)) {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
|
|
|
|
|
return static::$_cache['pluralize'][$word];
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return $word in singular form.
|
|
|
|
|
*
|
|
|
|
|
* @param string $word Word in plural
|
|
|
|
|
* @return string Word in singular
|
2018-03-18 09:24:15 +08:00
|
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::singularize
|
2014-04-23 10:51:50 +08:00
|
|
|
|
*/
|
|
|
|
|
public static function singularize($word) {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (isset(static::$_cache['singularize'][$word])) {
|
|
|
|
|
return static::$_cache['singularize'][$word];
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (!isset(static::$_singular['merged']['uninflected'])) {
|
|
|
|
|
static::$_singular['merged']['uninflected'] = array_merge(
|
|
|
|
|
static::$_singular['uninflected'],
|
|
|
|
|
static::$_uninflected
|
2014-04-23 10:51:50 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (!isset(static::$_singular['merged']['irregular'])) {
|
|
|
|
|
static::$_singular['merged']['irregular'] = array_merge(
|
|
|
|
|
static::$_singular['irregular'],
|
|
|
|
|
array_flip(static::$_plural['irregular'])
|
2014-04-23 10:51:50 +08:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (!isset(static::$_singular['cacheUninflected']) || !isset(static::$_singular['cacheIrregular'])) {
|
|
|
|
|
static::$_singular['cacheUninflected'] = '(?:' . implode('|', static::$_singular['merged']['uninflected']) . ')';
|
|
|
|
|
static::$_singular['cacheIrregular'] = '(?:' . implode('|', array_keys(static::$_singular['merged']['irregular'])) . ')';
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (preg_match('/(.*?(?:\\b|_))(' . static::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
|
|
|
|
|
static::$_cache['singularize'][$word] = $regs[1] .
|
|
|
|
|
substr($regs[2], 0, 1) .
|
|
|
|
|
substr(static::$_singular['merged']['irregular'][strtolower($regs[2])], 1);
|
|
|
|
|
return static::$_cache['singularize'][$word];
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (preg_match('/^(' . static::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
|
|
|
|
|
static::$_cache['singularize'][$word] = $word;
|
2014-04-23 10:51:50 +08:00
|
|
|
|
return $word;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 20:25:24 +08:00
|
|
|
|
foreach (static::$_singular['rules'] as $rule => $replacement) {
|
2014-04-23 10:51:50 +08:00
|
|
|
|
if (preg_match($rule, $word)) {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
|
|
|
|
|
return static::$_cache['singularize'][$word];
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::$_cache['singularize'][$word] = $word;
|
2014-04-23 10:51:50 +08:00
|
|
|
|
return $word;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the given lower_case_and_underscored_word as a CamelCased word.
|
|
|
|
|
*
|
|
|
|
|
* @param string $lowerCaseAndUnderscoredWord Word to camelize
|
|
|
|
|
* @return string Camelized word. LikeThis.
|
2018-03-18 09:24:15 +08:00
|
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::camelize
|
2014-04-23 10:51:50 +08:00
|
|
|
|
*/
|
|
|
|
|
public static function camelize($lowerCaseAndUnderscoredWord) {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (!($result = static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
|
2014-04-23 10:51:50 +08:00
|
|
|
|
$result = str_replace(' ', '', Inflector::humanize($lowerCaseAndUnderscoredWord));
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the given camelCasedWord as an underscored_word.
|
|
|
|
|
*
|
|
|
|
|
* @param string $camelCasedWord Camel-cased word to be "underscorized"
|
|
|
|
|
* @return string Underscore-syntaxed version of the $camelCasedWord
|
2018-03-18 09:24:15 +08:00
|
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::underscore
|
2014-04-23 10:51:50 +08:00
|
|
|
|
*/
|
|
|
|
|
public static function underscore($camelCasedWord) {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (!($result = static::_cache(__FUNCTION__, $camelCasedWord))) {
|
|
|
|
|
$underscoredWord = preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord);
|
|
|
|
|
$result = mb_strtolower($underscoredWord);
|
|
|
|
|
static::_cache(__FUNCTION__, $camelCasedWord, $result);
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the given underscored_word_group as a Human Readable Word Group.
|
|
|
|
|
* (Underscores are replaced by spaces and capitalized following words.)
|
|
|
|
|
*
|
|
|
|
|
* @param string $lowerCaseAndUnderscoredWord String to be made more readable
|
|
|
|
|
* @return string Human-readable string
|
2018-03-18 09:24:15 +08:00
|
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::humanize
|
2014-04-23 10:51:50 +08:00
|
|
|
|
*/
|
|
|
|
|
public static function humanize($lowerCaseAndUnderscoredWord) {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (!($result = static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
|
|
|
|
|
$result = explode(' ', str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
|
|
|
|
|
foreach ($result as &$word) {
|
|
|
|
|
$word = mb_strtoupper(mb_substr($word, 0, 1)) . mb_substr($word, 1);
|
|
|
|
|
}
|
|
|
|
|
$result = implode(' ', $result);
|
|
|
|
|
static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns corresponding table name for given model $className. ("people" for the model class "Person").
|
|
|
|
|
*
|
|
|
|
|
* @param string $className Name of class to get database table name for
|
|
|
|
|
* @return string Name of the database table for given class
|
2018-03-18 09:24:15 +08:00
|
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::tableize
|
2014-04-23 10:51:50 +08:00
|
|
|
|
*/
|
|
|
|
|
public static function tableize($className) {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (!($result = static::_cache(__FUNCTION__, $className))) {
|
2014-04-23 10:51:50 +08:00
|
|
|
|
$result = Inflector::pluralize(Inflector::underscore($className));
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::_cache(__FUNCTION__, $className, $result);
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns Cake model class name ("Person" for the database table "people".) for given database table.
|
|
|
|
|
*
|
|
|
|
|
* @param string $tableName Name of database table to get class name for
|
|
|
|
|
* @return string Class name
|
2018-03-18 09:24:15 +08:00
|
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::classify
|
2014-04-23 10:51:50 +08:00
|
|
|
|
*/
|
|
|
|
|
public static function classify($tableName) {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (!($result = static::_cache(__FUNCTION__, $tableName))) {
|
2014-04-23 10:51:50 +08:00
|
|
|
|
$result = Inflector::camelize(Inflector::singularize($tableName));
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::_cache(__FUNCTION__, $tableName, $result);
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns camelBacked version of an underscored string.
|
|
|
|
|
*
|
2016-02-25 20:25:24 +08:00
|
|
|
|
* @param string $string String to convert.
|
2014-04-23 10:51:50 +08:00
|
|
|
|
* @return string in variable form
|
2018-03-18 09:24:15 +08:00
|
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::variable
|
2014-04-23 10:51:50 +08:00
|
|
|
|
*/
|
|
|
|
|
public static function variable($string) {
|
2016-02-25 20:25:24 +08:00
|
|
|
|
if (!($result = static::_cache(__FUNCTION__, $string))) {
|
2014-04-23 10:51:50 +08:00
|
|
|
|
$camelized = Inflector::camelize(Inflector::underscore($string));
|
|
|
|
|
$replace = strtolower(substr($camelized, 0, 1));
|
|
|
|
|
$result = preg_replace('/\\w/', $replace, $camelized, 1);
|
2016-02-25 20:25:24 +08:00
|
|
|
|
static::_cache(__FUNCTION__, $string, $result);
|
2014-04-23 10:51:50 +08:00
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a string with all spaces converted to underscores (by default), accented
|
|
|
|
|
* characters converted to non-accented characters, and non word characters removed.
|
|
|
|
|
*
|
|
|
|
|
* @param string $string the string you want to slug
|
|
|
|
|
* @param string $replacement will replace keys in map
|
|
|
|
|
* @return string
|
2018-03-18 09:24:15 +08:00
|
|
|
|
* @link https://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::slug
|
2014-04-23 10:51:50 +08:00
|
|
|
|
*/
|
|
|
|
|
public static function slug($string, $replacement = '_') {
|
|
|
|
|
$quotedReplacement = preg_quote($replacement, '/');
|
|
|
|
|
|
|
|
|
|
$merge = array(
|
|
|
|
|
'/[^\s\p{Zs}\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
|
|
|
|
|
'/[\s\p{Zs}]+/mu' => $replacement,
|
|
|
|
|
sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
|
|
|
|
|
);
|
|
|
|
|
|
2016-02-25 20:25:24 +08:00
|
|
|
|
$map = static::$_transliteration + $merge;
|
2014-04-23 10:51:50 +08:00
|
|
|
|
return preg_replace(array_keys($map), array_values($map), $string);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Store the initial state
|
|
|
|
|
Inflector::reset();
|