2008-05-30 11:40:08 +00:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
2009-05-01 17:03:30 +00:00
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*
|
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2008-05-30 11:40:08 +00:00
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-01-26 19:18:20 +00:00
|
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2011-07-26 06:16:14 +00:00
|
|
|
|
* @package Cake.Utility
|
2008-10-30 17:30:26 +00:00
|
|
|
|
* @since CakePHP(tm) v 0.2.9
|
2013-05-30 22:11:14 +00:00
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*/
|
2009-07-24 19:18:37 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Pluralize and singularize English words.
|
|
|
|
|
*
|
|
|
|
|
* Inflector pluralizes and singularizes English nouns.
|
2013-09-27 17:36:43 +00:00
|
|
|
|
* Used by CakePHP's naming conventions throughout the framework.
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
|
* @package Cake.Utility
|
2011-10-15 17:12:23 +00:00
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*/
|
2009-09-16 01:44:27 +00:00
|
|
|
|
class Inflector {
|
2009-04-10 18:06:04 +00:00
|
|
|
|
|
2009-03-22 01:38:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* Plural inflector rules
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
2009-11-14 12:19:25 +00:00
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
protected static $_plural = array(
|
2009-03-18 00:22:36 +00:00
|
|
|
|
'rules' => array(
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'/(s)tatus$/i' => '\1\2tatuses',
|
|
|
|
|
'/(quiz)$/i' => '\1zes',
|
|
|
|
|
'/^(ox)$/i' => '\1\2en',
|
|
|
|
|
'/([m|l])ouse$/i' => '\1ice',
|
2011-12-16 07:00:07 +00:00
|
|
|
|
'/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'/(x|ch|ss|sh)$/i' => '\1es',
|
|
|
|
|
'/([^aeiouy]|qu)y$/i' => '\1ies',
|
|
|
|
|
'/(hive)$/i' => '\1s',
|
2013-07-10 15:46:01 +00:00
|
|
|
|
'/(?:([^f])fe|([lre])f)$/i' => '\1\2ves',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'/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',
|
2010-08-17 02:25:55 +00:00
|
|
|
|
'/us$/i' => 'uses',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'/(alias)$/i' => '\1es',
|
2009-02-22 04:09:39 +00:00
|
|
|
|
'/(ax|cris|test)is$/i' => '\1es',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'/s$/' => 's',
|
2008-08-26 18:30:54 +00:00
|
|
|
|
'/^$/' => '',
|
2009-03-18 00:22:36 +00:00
|
|
|
|
'/$/' => 's',
|
|
|
|
|
),
|
|
|
|
|
'uninflected' => array(
|
2012-12-24 23:21:12 +00:00
|
|
|
|
'.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', 'people'
|
2009-03-18 00:22:36 +00:00
|
|
|
|
),
|
|
|
|
|
'irregular' => array(
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'atlas' => 'atlases',
|
|
|
|
|
'beef' => 'beefs',
|
2013-09-04 08:19:37 +00:00
|
|
|
|
'brief' => 'briefs',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'brother' => 'brothers',
|
2011-08-29 01:45:16 +00:00
|
|
|
|
'cafe' => 'cafes',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'child' => 'children',
|
2012-10-22 21:07:42 +00:00
|
|
|
|
'cookie' => 'cookies',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'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',
|
2009-05-01 17:34:19 +00:00
|
|
|
|
'niche' => 'niches',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'numen' => 'numina',
|
|
|
|
|
'occiput' => 'occiputs',
|
|
|
|
|
'octopus' => 'octopuses',
|
|
|
|
|
'opus' => 'opuses',
|
|
|
|
|
'ox' => 'oxen',
|
|
|
|
|
'penis' => 'penises',
|
|
|
|
|
'person' => 'people',
|
|
|
|
|
'sex' => 'sexes',
|
|
|
|
|
'soliloquy' => 'soliloquies',
|
|
|
|
|
'testis' => 'testes',
|
|
|
|
|
'trilby' => 'trilbys',
|
2013-07-10 15:46:01 +00:00
|
|
|
|
'turf' => 'turfs',
|
2013-07-10 15:51:42 +00:00
|
|
|
|
'potato' => 'potatoes',
|
|
|
|
|
'hero' => 'heroes',
|
|
|
|
|
'tooth' => 'teeth',
|
|
|
|
|
'goose' => 'geese',
|
|
|
|
|
'foot' => 'feet'
|
2009-03-18 00:22:36 +00:00
|
|
|
|
)
|
|
|
|
|
);
|
2009-04-10 18:06:04 +00:00
|
|
|
|
|
2009-03-22 01:38:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* Singular inflector rules
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
2009-11-14 12:19:25 +00:00
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
protected static $_singular = array(
|
2009-03-18 00:22:36 +00:00
|
|
|
|
'rules' => array(
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'/(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',
|
2009-08-11 01:29:01 +00:00
|
|
|
|
'/([ftw]ax)es/i' => '\1',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'/(cris|ax|test)es$/i' => '\1is',
|
2009-07-30 19:29:16 +00:00
|
|
|
|
'/(shoe|slave)s$/i' => '\1',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'/(o)es$/i' => '\1',
|
|
|
|
|
'/ouses$/' => 'ouse',
|
2010-05-22 03:49:49 +00:00
|
|
|
|
'/([^a])uses$/' => '\1us',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'/([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',
|
2013-07-17 14:04:29 +00:00
|
|
|
|
'/(tive)s$/i' => '\1',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'/(hive)s$/i' => '\1',
|
|
|
|
|
'/(drive)s$/i' => '\1',
|
2013-09-03 16:50:02 +00:00
|
|
|
|
'/([lre])ves$/i' => '\1f',
|
|
|
|
|
'/([^fo])ves$/i' => '\1fe',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'/(^analy)ses$/i' => '\1sis',
|
2012-04-24 00:50:11 +00:00
|
|
|
|
'/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'/([ti])a$/i' => '\1um',
|
|
|
|
|
'/(p)eople$/i' => '\1\2erson',
|
|
|
|
|
'/(m)en$/i' => '\1an',
|
|
|
|
|
'/(c)hildren$/i' => '\1\2hild',
|
|
|
|
|
'/(n)ews$/i' => '\1\2ews',
|
2009-11-13 13:36:33 +00:00
|
|
|
|
'/eaus$/' => 'eau',
|
2008-06-23 03:08:35 +00:00
|
|
|
|
'/^(.*us)$/' => '\\1',
|
2009-03-18 00:22:36 +00:00
|
|
|
|
'/s$/i' => ''
|
|
|
|
|
),
|
|
|
|
|
'uninflected' => array(
|
2009-05-01 17:34:19 +00:00
|
|
|
|
'.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss'
|
2009-09-16 01:44:27 +00:00
|
|
|
|
),
|
|
|
|
|
'irregular' => array(
|
2012-01-06 02:22:40 +00:00
|
|
|
|
'foes' => 'foe',
|
2011-07-07 17:38:03 +00:00
|
|
|
|
'waves' => 'wave',
|
|
|
|
|
'curves' => 'curve'
|
2009-09-16 01:44:27 +00:00
|
|
|
|
)
|
2009-03-18 00:22:36 +00:00
|
|
|
|
);
|
2009-04-10 18:06:04 +00:00
|
|
|
|
|
2009-03-22 01:38:32 +00:00
|
|
|
|
/**
|
|
|
|
|
* Words that should not be inflected
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
2009-11-14 12:19:25 +00:00
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
protected static $_uninflected = array(
|
2009-05-01 17:34:19 +00:00
|
|
|
|
'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',
|
2011-10-25 01:05:08 +00:00
|
|
|
|
'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', '.*?media',
|
2009-05-01 17:34:19 +00:00
|
|
|
|
'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',
|
2011-10-25 01:05:08 +00:00
|
|
|
|
'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
|
2009-05-01 17:34:19 +00:00
|
|
|
|
'Yengeese'
|
2009-03-18 00:22:36 +00:00
|
|
|
|
);
|
|
|
|
|
|
2010-03-13 20:20:13 +00:00
|
|
|
|
/**
|
|
|
|
|
* Default map of accented and special characters to ASCII characters
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
protected static $_transliteration = array(
|
2010-04-10 23:12:17 +00:00
|
|
|
|
'/ä|æ|ǽ/' => 'ae',
|
|
|
|
|
'/ö|œ/' => 'oe',
|
2010-03-13 20:20:13 +00:00
|
|
|
|
'/ü/' => 'ue',
|
|
|
|
|
'/Ä/' => 'Ae',
|
|
|
|
|
'/Ü/' => 'Ue',
|
|
|
|
|
'/Ö/' => 'Oe',
|
2012-11-01 14:47:50 +00:00
|
|
|
|
'/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A',
|
2010-04-10 23:12:17 +00:00
|
|
|
|
'/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' => 'a',
|
2010-04-08 13:45:07 +00:00
|
|
|
|
'/Ç|Ć|Ĉ|Ċ|Č/' => 'C',
|
|
|
|
|
'/ç|ć|ĉ|ċ|č/' => 'c',
|
|
|
|
|
'/Ð|Ď|Đ/' => 'D',
|
|
|
|
|
'/ð|ď|đ/' => 'd',
|
|
|
|
|
'/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/' => 'E',
|
2010-04-14 20:26:38 +00:00
|
|
|
|
'/è|é|ê|ë|ē|ĕ|ė|ę|ě/' => 'e',
|
2010-04-10 23:12:17 +00:00
|
|
|
|
'/Ĝ|Ğ|Ġ|Ģ/' => 'G',
|
|
|
|
|
'/ĝ|ğ|ġ|ģ/' => 'g',
|
|
|
|
|
'/Ĥ|Ħ/' => 'H',
|
|
|
|
|
'/ĥ|ħ/' => 'h',
|
2010-04-08 13:45:07 +00:00
|
|
|
|
'/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ/' => 'I',
|
|
|
|
|
'/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı/' => 'i',
|
|
|
|
|
'/Ĵ/' => 'J',
|
|
|
|
|
'/ĵ/' => 'j',
|
|
|
|
|
'/Ķ/' => 'K',
|
|
|
|
|
'/ķ/' => 'k',
|
|
|
|
|
'/Ĺ|Ļ|Ľ|Ŀ|Ł/' => 'L',
|
|
|
|
|
'/ĺ|ļ|ľ|ŀ|ł/' => 'l',
|
|
|
|
|
'/Ñ|Ń|Ņ|Ň/' => 'N',
|
|
|
|
|
'/ñ|ń|ņ|ň|ʼn/' => 'n',
|
2010-04-10 23:12:17 +00:00
|
|
|
|
'/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ/' => 'O',
|
|
|
|
|
'/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º/' => 'o',
|
2010-04-08 13:45:07 +00:00
|
|
|
|
'/Ŕ|Ŗ|Ř/' => 'R',
|
|
|
|
|
'/ŕ|ŗ|ř/' => 'r',
|
2013-02-13 18:47:30 +00:00
|
|
|
|
'/Ś|Ŝ|Ş|Ș|Š/' => 'S',
|
|
|
|
|
'/ś|ŝ|ş|ș|š|ſ/' => 's',
|
|
|
|
|
'/Ţ|Ț|Ť|Ŧ/' => 'T',
|
|
|
|
|
'/ţ|ț|ť|ŧ/' => 't',
|
2010-04-10 23:12:17 +00:00
|
|
|
|
'/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/' => 'U',
|
|
|
|
|
'/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u',
|
|
|
|
|
'/Ý|Ÿ|Ŷ/' => 'Y',
|
|
|
|
|
'/ý|ÿ|ŷ/' => 'y',
|
2010-04-08 13:45:07 +00:00
|
|
|
|
'/Ŵ/' => 'W',
|
|
|
|
|
'/ŵ/' => 'w',
|
|
|
|
|
'/Ź|Ż|Ž/' => 'Z',
|
|
|
|
|
'/ź|ż|ž/' => 'z',
|
|
|
|
|
'/Æ|Ǽ/' => 'AE',
|
2011-12-01 07:21:31 +00:00
|
|
|
|
'/ß/' => 'ss',
|
2010-04-08 13:45:07 +00:00
|
|
|
|
'/IJ/' => 'IJ',
|
|
|
|
|
'/ij/' => 'ij',
|
|
|
|
|
'/Œ/' => 'OE',
|
|
|
|
|
'/ƒ/' => 'f'
|
2010-03-13 20:20:13 +00:00
|
|
|
|
);
|
|
|
|
|
|
2009-03-18 00:22:36 +00:00
|
|
|
|
/**
|
2010-07-05 01:46:48 +00:00
|
|
|
|
* Method cache array.
|
2009-03-18 00:22:36 +00:00
|
|
|
|
*
|
|
|
|
|
* @var array
|
2009-11-14 12:19:25 +00:00
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
protected static $_cache = array();
|
2009-04-10 18:06:04 +00:00
|
|
|
|
|
2010-09-26 03:04:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* The initial state of Inflector so reset() works.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected static $_initialState = array();
|
|
|
|
|
|
2010-01-05 11:05:02 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
protected static function _cache($type, $key, $value = false) {
|
2010-01-05 11:05:02 +00:00
|
|
|
|
$key = '_' . $key;
|
|
|
|
|
$type = '_' . $type;
|
|
|
|
|
if ($value !== false) {
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::$_cache[$type][$key] = $value;
|
2010-01-05 11:05:02 +00:00
|
|
|
|
return $value;
|
|
|
|
|
}
|
2010-07-05 01:46:48 +00:00
|
|
|
|
if (!isset(self::$_cache[$type][$key])) {
|
2010-01-05 11:05:02 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-07-05 01:46:48 +00:00
|
|
|
|
return self::$_cache[$type][$key];
|
2010-01-05 11:05:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-09-26 03:04:49 +00:00
|
|
|
|
/**
|
|
|
|
|
* 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) {
|
2013-02-12 02:38:08 +00:00
|
|
|
|
if ($key !== '_initialState') {
|
2010-09-26 03:04:49 +00:00
|
|
|
|
self::${$key} = $val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-18 00:22:36 +00:00
|
|
|
|
/**
|
2010-03-13 20:20:13 +00:00
|
|
|
|
* Adds custom inflection $rules, of either 'plural', 'singular' or 'transliteration' $type.
|
2009-03-18 00:22:36 +00:00
|
|
|
|
*
|
2010-01-25 20:37:55 +00:00
|
|
|
|
* ### 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')
|
|
|
|
|
* ));
|
2010-03-13 20:20:13 +00:00
|
|
|
|
* Inflector::rules('transliteration', array('/å/' => 'aa'));
|
2010-01-25 20:37:55 +00:00
|
|
|
|
* }}}
|
|
|
|
|
*
|
2010-03-20 17:33:51 +00:00
|
|
|
|
* @param string $type The type of inflection, either 'plural', 'singular' or 'transliteration'
|
2010-01-25 20:37:55 +00:00
|
|
|
|
* @param array $rules Array of rules to be added.
|
2010-01-14 23:14:20 +00:00
|
|
|
|
* @param boolean $reset If true, will unset default inflections for all
|
|
|
|
|
* new rules that are being defined in $rules.
|
2009-03-18 00:22:36 +00:00
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
public static function rules($type, $rules, $reset = false) {
|
|
|
|
|
$var = '_' . $type;
|
2009-03-18 00:22:36 +00:00
|
|
|
|
|
2010-03-13 20:20:13 +00:00
|
|
|
|
switch ($type) {
|
|
|
|
|
case 'transliteration':
|
2010-01-14 23:14:20 +00:00
|
|
|
|
if ($reset) {
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::$_transliteration = $rules;
|
2010-01-14 23:14:20 +00:00
|
|
|
|
} else {
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::$_transliteration = $rules + self::$_transliteration;
|
2010-01-14 23:14:20 +00:00
|
|
|
|
}
|
2013-07-02 22:52:48 +00:00
|
|
|
|
break;
|
2010-03-13 20:20:13 +00:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
foreach ($rules as $rule => $pattern) {
|
|
|
|
|
if (is_array($pattern)) {
|
|
|
|
|
if ($reset) {
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::${$var}[$rule] = $pattern;
|
2010-03-13 20:20:13 +00:00
|
|
|
|
} else {
|
2011-09-22 19:44:52 +00:00
|
|
|
|
if ($rule === 'uninflected') {
|
2011-10-02 02:38:20 +00:00
|
|
|
|
self::${$var}[$rule] = array_merge($pattern, self::${$var}[$rule]);
|
2011-09-22 19:44:52 +00:00
|
|
|
|
} else {
|
2011-10-02 02:38:20 +00:00
|
|
|
|
self::${$var}[$rule] = $pattern + self::${$var}[$rule];
|
2011-09-22 19:44:52 +00:00
|
|
|
|
}
|
2010-03-13 20:20:13 +00:00
|
|
|
|
}
|
2010-07-05 01:46:48 +00:00
|
|
|
|
unset($rules[$rule], self::${$var}['cache' . ucfirst($rule)]);
|
|
|
|
|
if (isset(self::${$var}['merged'][$rule])) {
|
|
|
|
|
unset(self::${$var}['merged'][$rule]);
|
2010-06-02 03:41:51 +00:00
|
|
|
|
}
|
2010-04-21 17:49:59 +00:00
|
|
|
|
if ($type === 'plural') {
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::$_cache['pluralize'] = self::$_cache['tableize'] = array();
|
2010-04-21 17:49:59 +00:00
|
|
|
|
} elseif ($type === 'singular') {
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::$_cache['singularize'] = array();
|
2010-04-21 17:49:59 +00:00
|
|
|
|
}
|
2010-03-13 20:20:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2011-10-02 02:38:20 +00:00
|
|
|
|
self::${$var}['rules'] = $rules + self::${$var}['rules'];
|
2009-05-01 17:34:19 +00:00
|
|
|
|
}
|
2009-03-18 00:22:36 +00:00
|
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
|
2009-03-18 00:22:36 +00:00
|
|
|
|
/**
|
|
|
|
|
* Return $word in plural form.
|
|
|
|
|
*
|
|
|
|
|
* @param string $word Word in singular
|
|
|
|
|
* @return string Word in plural
|
2011-10-15 17:12:23 +00:00
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::pluralize
|
2009-03-18 00:22:36 +00:00
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
public static function pluralize($word) {
|
|
|
|
|
if (isset(self::$_cache['pluralize'][$word])) {
|
|
|
|
|
return self::$_cache['pluralize'][$word];
|
2009-03-18 00:22:36 +00:00
|
|
|
|
}
|
2009-03-18 05:42:22 +00:00
|
|
|
|
|
2010-07-05 01:46:48 +00:00
|
|
|
|
if (!isset(self::$_plural['merged']['irregular'])) {
|
|
|
|
|
self::$_plural['merged']['irregular'] = self::$_plural['irregular'];
|
2009-05-01 17:34:19 +00:00
|
|
|
|
}
|
2009-04-10 17:03:38 +00:00
|
|
|
|
|
2010-07-05 01:46:48 +00:00
|
|
|
|
if (!isset(self::$_plural['merged']['uninflected'])) {
|
|
|
|
|
self::$_plural['merged']['uninflected'] = array_merge(self::$_plural['uninflected'], self::$_uninflected);
|
2009-05-01 17:34:19 +00:00
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
2010-07-05 01:46:48 +00:00
|
|
|
|
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'])) . ')';
|
2009-03-18 00:22:36 +00:00
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
2010-07-05 01:46:48 +00:00
|
|
|
|
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];
|
2009-03-18 00:22:36 +00:00
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
|
2010-07-05 01:46:48 +00:00
|
|
|
|
if (preg_match('/^(' . self::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
|
|
|
|
|
self::$_cache['pluralize'][$word] = $word;
|
2009-03-18 00:22:36 +00:00
|
|
|
|
return $word;
|
|
|
|
|
}
|
2008-08-27 03:42:17 +00:00
|
|
|
|
|
2010-07-05 01:46:48 +00:00
|
|
|
|
foreach (self::$_plural['rules'] as $rule => $replacement) {
|
2009-03-18 00:22:36 +00:00
|
|
|
|
if (preg_match($rule, $word)) {
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
|
|
|
|
|
return self::$_cache['pluralize'][$word];
|
2009-03-18 00:22:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2008-05-30 11:40:08 +00:00
|
|
|
|
}
|
2009-03-18 00:22:36 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
/**
|
|
|
|
|
* Return $word in singular form.
|
|
|
|
|
*
|
|
|
|
|
* @param string $word Word in plural
|
|
|
|
|
* @return string Word in singular
|
2011-10-15 17:12:23 +00:00
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::singularize
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
public static function singularize($word) {
|
|
|
|
|
if (isset(self::$_cache['singularize'][$word])) {
|
|
|
|
|
return self::$_cache['singularize'][$word];
|
2008-05-30 11:40:08 +00:00
|
|
|
|
}
|
2008-12-18 01:35:27 +00:00
|
|
|
|
|
2010-07-05 01:46:48 +00:00
|
|
|
|
if (!isset(self::$_singular['merged']['uninflected'])) {
|
|
|
|
|
self::$_singular['merged']['uninflected'] = array_merge(
|
2011-07-07 17:38:03 +00:00
|
|
|
|
self::$_singular['uninflected'],
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::$_uninflected
|
|
|
|
|
);
|
2009-05-01 17:34:19 +00:00
|
|
|
|
}
|
2009-04-10 17:03:38 +00:00
|
|
|
|
|
2010-07-05 01:46:48 +00:00
|
|
|
|
if (!isset(self::$_singular['merged']['irregular'])) {
|
|
|
|
|
self::$_singular['merged']['irregular'] = array_merge(
|
2011-07-07 17:38:03 +00:00
|
|
|
|
self::$_singular['irregular'],
|
2010-07-05 01:46:48 +00:00
|
|
|
|
array_flip(self::$_plural['irregular'])
|
|
|
|
|
);
|
2009-05-01 17:34:19 +00:00
|
|
|
|
}
|
2009-03-18 00:22:36 +00:00
|
|
|
|
|
2010-07-05 01:46:48 +00:00
|
|
|
|
if (!isset(self::$_singular['cacheUninflected']) || !isset(self::$_singular['cacheIrregular'])) {
|
2012-09-15 10:06:02 +00:00
|
|
|
|
self::$_singular['cacheUninflected'] = '(?:' . implode('|', self::$_singular['merged']['uninflected']) . ')';
|
|
|
|
|
self::$_singular['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_singular['merged']['irregular'])) . ')';
|
2008-05-30 11:40:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-07-05 01:46:48 +00:00
|
|
|
|
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];
|
2008-05-30 11:40:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2010-07-05 01:46:48 +00:00
|
|
|
|
if (preg_match('/^(' . self::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
|
|
|
|
|
self::$_cache['singularize'][$word] = $word;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
return $word;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-05 01:46:48 +00:00
|
|
|
|
foreach (self::$_singular['rules'] as $rule => $replacement) {
|
2008-05-30 11:40:08 +00:00
|
|
|
|
if (preg_match($rule, $word)) {
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
|
|
|
|
|
return self::$_cache['singularize'][$word];
|
2008-05-30 11:40:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::$_cache['singularize'][$word] = $word;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
return $word;
|
|
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
/**
|
2008-10-31 20:49:23 +00:00
|
|
|
|
* Returns the given lower_case_and_underscored_word as a CamelCased word.
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*
|
2011-07-29 03:24:31 +00:00
|
|
|
|
* @param string $lowerCaseAndUnderscoredWord Word to camelize
|
2008-06-23 03:08:35 +00:00
|
|
|
|
* @return string Camelized word. LikeThis.
|
2011-10-15 17:12:23 +00:00
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::camelize
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
public static function camelize($lowerCaseAndUnderscoredWord) {
|
|
|
|
|
if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
|
2010-01-05 11:05:02 +00:00
|
|
|
|
$result = str_replace(' ', '', Inflector::humanize($lowerCaseAndUnderscoredWord));
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
|
2009-12-25 07:53:38 +00:00
|
|
|
|
}
|
2010-01-05 11:05:02 +00:00
|
|
|
|
return $result;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
/**
|
2008-10-31 20:49:23 +00:00
|
|
|
|
* Returns the given camelCasedWord as an underscored_word.
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*
|
2008-10-23 00:10:44 +00:00
|
|
|
|
* @param string $camelCasedWord Camel-cased word to be "underscorized"
|
|
|
|
|
* @return string Underscore-syntaxed version of the $camelCasedWord
|
2011-10-15 17:12:23 +00:00
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::underscore
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
public static function underscore($camelCasedWord) {
|
|
|
|
|
if (!($result = self::_cache(__FUNCTION__, $camelCasedWord))) {
|
2010-01-05 11:05:02 +00:00
|
|
|
|
$result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::_cache(__FUNCTION__, $camelCasedWord, $result);
|
2009-12-25 07:53:38 +00:00
|
|
|
|
}
|
2010-01-05 11:05:02 +00:00
|
|
|
|
return $result;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
/**
|
2008-10-31 20:49:23 +00:00
|
|
|
|
* Returns the given underscored_word_group as a Human Readable Word Group.
|
|
|
|
|
* (Underscores are replaced by spaces and capitalized following words.)
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*
|
2011-07-29 03:24:31 +00:00
|
|
|
|
* @param string $lowerCaseAndUnderscoredWord String to be made more readable
|
2008-05-30 11:40:08 +00:00
|
|
|
|
* @return string Human-readable string
|
2011-10-15 17:12:23 +00:00
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::humanize
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
public static function humanize($lowerCaseAndUnderscoredWord) {
|
|
|
|
|
if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
|
2010-01-05 11:05:02 +00:00
|
|
|
|
$result = ucwords(str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
|
2009-12-25 07:53:38 +00:00
|
|
|
|
}
|
2010-01-05 11:05:02 +00:00
|
|
|
|
return $result;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
/**
|
2008-10-31 20:49:23 +00:00
|
|
|
|
* Returns corresponding table name for given model $className. ("people" for the model class "Person").
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*
|
2008-10-23 00:10:44 +00:00
|
|
|
|
* @param string $className Name of class to get database table name for
|
2008-05-30 11:40:08 +00:00
|
|
|
|
* @return string Name of the database table for given class
|
2011-10-15 17:12:23 +00:00
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::tableize
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
public static function tableize($className) {
|
|
|
|
|
if (!($result = self::_cache(__FUNCTION__, $className))) {
|
2010-01-05 11:05:02 +00:00
|
|
|
|
$result = Inflector::pluralize(Inflector::underscore($className));
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::_cache(__FUNCTION__, $className, $result);
|
2009-12-25 07:53:38 +00:00
|
|
|
|
}
|
2010-01-05 11:05:02 +00:00
|
|
|
|
return $result;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
/**
|
2008-10-31 20:49:23 +00:00
|
|
|
|
* Returns Cake model class name ("Person" for the database table "people".) for given database table.
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*
|
|
|
|
|
* @param string $tableName Name of database table to get class name for
|
2008-09-25 16:49:56 +00:00
|
|
|
|
* @return string Class name
|
2011-10-15 17:12:23 +00:00
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::classify
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
public static function classify($tableName) {
|
|
|
|
|
if (!($result = self::_cache(__FUNCTION__, $tableName))) {
|
2010-01-05 11:05:02 +00:00
|
|
|
|
$result = Inflector::camelize(Inflector::singularize($tableName));
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::_cache(__FUNCTION__, $tableName, $result);
|
2009-12-25 07:53:38 +00:00
|
|
|
|
}
|
2010-01-05 11:05:02 +00:00
|
|
|
|
return $result;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
/**
|
2008-10-31 20:49:23 +00:00
|
|
|
|
* Returns camelBacked version of an underscored string.
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*
|
|
|
|
|
* @param string $string
|
2008-09-25 16:49:56 +00:00
|
|
|
|
* @return string in variable form
|
2011-10-15 17:12:23 +00:00
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::variable
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*/
|
2010-07-05 01:46:48 +00:00
|
|
|
|
public static function variable($string) {
|
|
|
|
|
if (!($result = self::_cache(__FUNCTION__, $string))) {
|
2012-03-25 02:15:08 +00:00
|
|
|
|
$camelized = Inflector::camelize(Inflector::underscore($string));
|
|
|
|
|
$replace = strtolower(substr($camelized, 0, 1));
|
|
|
|
|
$result = preg_replace('/\\w/', $replace, $camelized, 1);
|
2010-07-05 01:46:48 +00:00
|
|
|
|
self::_cache(__FUNCTION__, $string, $result);
|
2009-12-25 07:53:38 +00:00
|
|
|
|
}
|
2010-01-05 11:05:02 +00:00
|
|
|
|
return $result;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
}
|
2009-04-10 18:06:04 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
/**
|
2008-10-31 20:49:23 +00:00
|
|
|
|
* Returns a string with all spaces converted to underscores (by default), accented
|
|
|
|
|
* characters converted to non-accented characters, and non word characters removed.
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*
|
2009-05-01 17:03:30 +00:00
|
|
|
|
* @param string $string the string you want to slug
|
|
|
|
|
* @param string $replacement will replace keys in map
|
2008-05-30 11:40:08 +00:00
|
|
|
|
* @return string
|
2011-10-15 17:12:23 +00:00
|
|
|
|
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::slug
|
2008-05-30 11:40:08 +00:00
|
|
|
|
*/
|
2011-03-24 02:31:27 +00:00
|
|
|
|
public static function slug($string, $replacement = '_') {
|
2009-05-01 17:34:19 +00:00
|
|
|
|
$quotedReplacement = preg_quote($replacement, '/');
|
2009-04-10 18:15:27 +00:00
|
|
|
|
|
2010-03-13 20:20:13 +00:00
|
|
|
|
$merge = array(
|
2009-07-25 19:10:21 +00:00
|
|
|
|
'/[^\s\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
|
2008-05-30 11:40:08 +00:00
|
|
|
|
'/\\s+/' => $replacement,
|
2009-05-01 17:34:19 +00:00
|
|
|
|
sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
|
2008-05-30 11:40:08 +00:00
|
|
|
|
);
|
2009-07-25 18:21:34 +00:00
|
|
|
|
|
2011-03-24 02:31:27 +00:00
|
|
|
|
$map = self::$_transliteration + $merge;
|
2008-12-18 01:35:27 +00:00
|
|
|
|
return preg_replace(array_keys($map), array_values($map), $string);
|
2008-05-30 11:40:08 +00:00
|
|
|
|
}
|
2012-03-03 22:31:47 +00:00
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
|
}
|
2010-09-26 03:04:49 +00:00
|
|
|
|
|
|
|
|
|
// Store the initial state
|
2011-06-23 19:48:06 +00:00
|
|
|
|
Inflector::reset();
|