1: <?php
2: /**
3: * Pluralize and singularize English words.
4: *
5: * Used by Cake's naming conventions throughout the framework.
6: *
7: * PHP 5
8: *
9: * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
10: * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
11: *
12: * Licensed under The MIT License
13: * Redistributions of files must retain the above copyright notice.
14: *
15: * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
16: * @link http://cakephp.org CakePHP(tm) Project
17: * @package Cake.Utility
18: * @since CakePHP(tm) v 0.2.9
19: * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
20: */
21:
22: /**
23: * Pluralize and singularize English words.
24: *
25: * Inflector pluralizes and singularizes English nouns.
26: * Used by Cake's naming conventions throughout the framework.
27: *
28: * @package Cake.Utility
29: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html
30: */
31: class Inflector {
32:
33: /**
34: * Plural inflector rules
35: *
36: * @var array
37: */
38: protected static $_plural = array(
39: 'rules' => array(
40: '/(s)tatus$/i' => '1円2円tatuses',
41: '/(quiz)$/i' => '1円zes',
42: '/^(ox)$/i' => '1円2円en',
43: '/([m|l])ouse$/i' => '1円ice',
44: '/(matr|vert|ind)(ix|ex)$/i' => '1円ices',
45: '/(x|ch|ss|sh)$/i' => '1円es',
46: '/([^aeiouy]|qu)y$/i' => '1円ies',
47: '/(hive)$/i' => '1円s',
48: '/(?:([^f])fe|([lr])f)$/i' => '1円2円ves',
49: '/sis$/i' => 'ses',
50: '/([ti])um$/i' => '1円a',
51: '/(p)erson$/i' => '1円eople',
52: '/(m)an$/i' => '1円en',
53: '/(c)hild$/i' => '1円hildren',
54: '/(buffal|tomat)o$/i' => '1円2円oes',
55: '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '1円i',
56: '/us$/i' => 'uses',
57: '/(alias)$/i' => '1円es',
58: '/(ax|cris|test)is$/i' => '1円es',
59: '/s$/' => 's',
60: '/^$/' => '',
61: '/$/' => 's',
62: ),
63: 'uninflected' => array(
64: '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', 'people'
65: ),
66: 'irregular' => array(
67: 'atlas' => 'atlases',
68: 'beef' => 'beefs',
69: 'brother' => 'brothers',
70: 'cafe' => 'cafes',
71: 'child' => 'children',
72: 'corpus' => 'corpuses',
73: 'cow' => 'cows',
74: 'ganglion' => 'ganglions',
75: 'genie' => 'genies',
76: 'genus' => 'genera',
77: 'graffito' => 'graffiti',
78: 'hoof' => 'hoofs',
79: 'loaf' => 'loaves',
80: 'man' => 'men',
81: 'money' => 'monies',
82: 'mongoose' => 'mongooses',
83: 'move' => 'moves',
84: 'mythos' => 'mythoi',
85: 'niche' => 'niches',
86: 'numen' => 'numina',
87: 'occiput' => 'occiputs',
88: 'octopus' => 'octopuses',
89: 'opus' => 'opuses',
90: 'ox' => 'oxen',
91: 'penis' => 'penises',
92: 'person' => 'people',
93: 'sex' => 'sexes',
94: 'soliloquy' => 'soliloquies',
95: 'testis' => 'testes',
96: 'trilby' => 'trilbys',
97: 'turf' => 'turfs'
98: )
99: );
100:
101: /**
102: * Singular inflector rules
103: *
104: * @var array
105: */
106: protected static $_singular = array(
107: 'rules' => array(
108: '/(s)tatuses$/i' => '1円2円tatus',
109: '/^(.*)(menu)s$/i' => '1円2円',
110: '/(quiz)zes$/i' => '\1円',
111: '/(matr)ices$/i' => '1円ix',
112: '/(vert|ind)ices$/i' => '1円ex',
113: '/^(ox)en/i' => '1円',
114: '/(alias)(es)*$/i' => '1円',
115: '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '1円us',
116: '/([ftw]ax)es/i' => '1円',
117: '/(cris|ax|test)es$/i' => '1円is',
118: '/(shoe|slave)s$/i' => '1円',
119: '/(o)es$/i' => '1円',
120: '/ouses$/' => 'ouse',
121: '/([^a])uses$/' => '1円us',
122: '/([m|l])ice$/i' => '1円ouse',
123: '/(x|ch|ss|sh)es$/i' => '1円',
124: '/(m)ovies$/i' => '1円2円ovie',
125: '/(s)eries$/i' => '1円2円eries',
126: '/([^aeiouy]|qu)ies$/i' => '1円y',
127: '/([lr])ves$/i' => '1円f',
128: '/(tive)s$/i' => '1円',
129: '/(hive)s$/i' => '1円',
130: '/(drive)s$/i' => '1円',
131: '/([^fo])ves$/i' => '1円fe',
132: '/(^analy)ses$/i' => '1円sis',
133: '/(analy|ba|diagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '1円2円sis',
134: '/([ti])a$/i' => '1円um',
135: '/(p)eople$/i' => '1円2円erson',
136: '/(m)en$/i' => '1円an',
137: '/(c)hildren$/i' => '1円2円hild',
138: '/(n)ews$/i' => '1円2円ews',
139: '/eaus$/' => 'eau',
140: '/^(.*us)$/' => '\1円',
141: '/s$/i' => ''
142: ),
143: 'uninflected' => array(
144: '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss'
145: ),
146: 'irregular' => array(
147: 'foes' => 'foe',
148: 'waves' => 'wave',
149: 'curves' => 'curve'
150: )
151: );
152:
153: /**
154: * Words that should not be inflected
155: *
156: * @var array
157: */
158: protected static $_uninflected = array(
159: 'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus',
160: 'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps',
161: 'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder',
162: 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
163: 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings',
164: 'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', '.*?media',
165: 'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
166: 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
167: 'proceedings', 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
168: 'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
169: 'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
170: 'Yengeese'
171: );
172:
173: /**
174: * Default map of accented and special characters to ASCII characters
175: *
176: * @var array
177: */
178: protected static $_transliteration = array(
179: '/ä|æ|ǽ/' => 'ae',
180: '/ö|œ/' => 'oe',
181: '/ü/' => 'ue',
182: '/Ä/' => 'Ae',
183: '/Ü/' => 'Ue',
184: '/Ö/' => 'Oe',
185: '/À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A',
186: '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|a/' => 'a',
187: '/Ç|Ć|Ĉ|Ċ|Č/' => 'C',
188: '/ç|ć|ĉ|ċ|č/' => 'c',
189: '/Ð|Ď|Đ/' => 'D',
190: '/ð|ď|đ/' => 'd',
191: '/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/' => 'E',
192: '/è|é|ê|ë|ē|ĕ|ė|ę|ě/' => 'e',
193: '/Ĝ|Ğ|Ġ|Ģ/' => 'G',
194: '/ĝ|ğ|ġ|ģ/' => 'g',
195: '/Ĥ|Ħ/' => 'H',
196: '/ĥ|ħ/' => 'h',
197: '/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ/' => 'I',
198: '/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı/' => 'i',
199: '/Ĵ/' => 'J',
200: '/ĵ/' => 'j',
201: '/Ķ/' => 'K',
202: '/ķ/' => 'k',
203: '/Ĺ|Ļ|Ľ|L·|Ł/' => 'L',
204: '/ĺ|ļ|ľ|l·|ł/' => 'l',
205: '/Ñ|Ń|Ņ|Ň/' => 'N',
206: '/ñ|ń|ņ|ň|ʼn/' => 'n',
207: '/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ/' => 'O',
208: '/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|o/' => 'o',
209: '/Ŕ|Ŗ|Ř/' => 'R',
210: '/ŕ|ŗ|ř/' => 'r',
211: '/Ś|Ŝ|Ş|Š/' => 'S',
212: '/ś|ŝ|ş|š|s/' => 's',
213: '/Ţ|Ť|Ŧ/' => 'T',
214: '/ţ|ť|ŧ/' => 't',
215: '/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/' => 'U',
216: '/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u',
217: '/Ý|Ÿ|Ŷ/' => 'Y',
218: '/ý|ÿ|ŷ/' => 'y',
219: '/Ŵ/' => 'W',
220: '/ŵ/' => 'w',
221: '/Ź|Ż|Ž/' => 'Z',
222: '/ź|ż|ž/' => 'z',
223: '/Æ|Ǽ/' => 'AE',
224: '/ß/' => 'ss',
225: '/IJ/' => 'IJ',
226: '/ij/' => 'ij',
227: '/Œ/' => 'OE',
228: '/ƒ/' => 'f'
229: );
230:
231: /**
232: * Method cache array.
233: *
234: * @var array
235: */
236: protected static $_cache = array();
237:
238: /**
239: * The initial state of Inflector so reset() works.
240: *
241: * @var array
242: */
243: protected static $_initialState = array();
244:
245: /**
246: * Cache inflected values, and return if already available
247: *
248: * @param string $type Inflection type
249: * @param string $key Original value
250: * @param string $value Inflected value
251: * @return string Inflected value, from cache
252: */
253: protected static function _cache($type, $key, $value = false) {
254: $key = '_' . $key;
255: $type = '_' . $type;
256: if ($value !== false) {
257: self::$_cache[$type][$key] = $value;
258: return $value;
259: }
260: if (!isset(self::$_cache[$type][$key])) {
261: return false;
262: }
263: return self::$_cache[$type][$key];
264: }
265:
266: /**
267: * Clears Inflectors inflected value caches. And resets the inflection
268: * rules to the initial values.
269: *
270: * @return void
271: */
272: public static function reset() {
273: if (empty(self::$_initialState)) {
274: self::$_initialState = get_class_vars('Inflector');
275: return;
276: }
277: foreach (self::$_initialState as $key => $val) {
278: if ($key != '_initialState') {
279: self::${$key} = $val;
280: }
281: }
282: }
283:
284: /**
285: * Adds custom inflection $rules, of either 'plural', 'singular' or 'transliteration' $type.
286: *
287: * ### Usage:
288: *
289: * {{{
290: * Inflector::rules('plural', array('/^(inflect)or$/i' => '1円ables'));
291: * Inflector::rules('plural', array(
292: * 'rules' => array('/^(inflect)ors$/i' => '1円ables'),
293: * 'uninflected' => array('dontinflectme'),
294: * 'irregular' => array('red' => 'redlings')
295: * ));
296: * Inflector::rules('transliteration', array('/å/' => 'aa'));
297: * }}}
298: *
299: * @param string $type The type of inflection, either 'plural', 'singular' or 'transliteration'
300: * @param array $rules Array of rules to be added.
301: * @param boolean $reset If true, will unset default inflections for all
302: * new rules that are being defined in $rules.
303: * @return void
304: */
305: public static function rules($type, $rules, $reset = false) {
306: $var = '_' . $type;
307:
308: switch ($type) {
309: case 'transliteration':
310: if ($reset) {
311: self::$_transliteration = $rules;
312: } else {
313: self::$_transliteration = $rules + self::$_transliteration;
314: }
315: break;
316:
317: default:
318: foreach ($rules as $rule => $pattern) {
319: if (is_array($pattern)) {
320: if ($reset) {
321: self::${$var}[$rule] = $pattern;
322: } else {
323: if ($rule === 'uninflected') {
324: self::${$var}[$rule] = array_merge($pattern, self::${$var}[$rule]);
325: } else {
326: self::${$var}[$rule] = $pattern + self::${$var}[$rule];
327: }
328: }
329: unset($rules[$rule], self::${$var}['cache' . ucfirst($rule)]);
330: if (isset(self::${$var}['merged'][$rule])) {
331: unset(self::${$var}['merged'][$rule]);
332: }
333: if ($type === 'plural') {
334: self::$_cache['pluralize'] = self::$_cache['tableize'] = array();
335: } elseif ($type === 'singular') {
336: self::$_cache['singularize'] = array();
337: }
338: }
339: }
340: self::${$var}['rules'] = $rules + self::${$var}['rules'];
341: break;
342: }
343: }
344:
345: /**
346: * Return $word in plural form.
347: *
348: * @param string $word Word in singular
349: * @return string Word in plural
350: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::pluralize
351: */
352: public static function pluralize($word) {
353:
354: if (isset(self::$_cache['pluralize'][$word])) {
355: return self::$_cache['pluralize'][$word];
356: }
357:
358: if (!isset(self::$_plural['merged']['irregular'])) {
359: self::$_plural['merged']['irregular'] = self::$_plural['irregular'];
360: }
361:
362: if (!isset(self::$_plural['merged']['uninflected'])) {
363: self::$_plural['merged']['uninflected'] = array_merge(self::$_plural['uninflected'], self::$_uninflected);
364: }
365:
366: if (!isset(self::$_plural['cacheUninflected']) || !isset(self::$_plural['cacheIrregular'])) {
367: self::$_plural['cacheUninflected'] = '(?:' . implode('|', self::$_plural['merged']['uninflected']) . ')';
368: self::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_plural['merged']['irregular'])) . ')';
369: }
370:
371: if (preg_match('/(.*)\\b(' . self::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) {
372: self::$_cache['pluralize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1);
373: return self::$_cache['pluralize'][$word];
374: }
375:
376: if (preg_match('/^(' . self::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
377: self::$_cache['pluralize'][$word] = $word;
378: return $word;
379: }
380:
381: foreach (self::$_plural['rules'] as $rule => $replacement) {
382: if (preg_match($rule, $word)) {
383: self::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
384: return self::$_cache['pluralize'][$word];
385: }
386: }
387: }
388:
389: /**
390: * Return $word in singular form.
391: *
392: * @param string $word Word in plural
393: * @return string Word in singular
394: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::singularize
395: */
396: public static function singularize($word) {
397:
398: if (isset(self::$_cache['singularize'][$word])) {
399: return self::$_cache['singularize'][$word];
400: }
401:
402: if (!isset(self::$_singular['merged']['uninflected'])) {
403: self::$_singular['merged']['uninflected'] = array_merge(
404: self::$_singular['uninflected'],
405: self::$_uninflected
406: );
407: }
408:
409: if (!isset(self::$_singular['merged']['irregular'])) {
410: self::$_singular['merged']['irregular'] = array_merge(
411: self::$_singular['irregular'],
412: array_flip(self::$_plural['irregular'])
413: );
414: }
415:
416: if (!isset(self::$_singular['cacheUninflected']) || !isset(self::$_singular['cacheIrregular'])) {
417: self::$_singular['cacheUninflected'] = '(?:' . join( '|', self::$_singular['merged']['uninflected']) . ')';
418: self::$_singular['cacheIrregular'] = '(?:' . join( '|', array_keys(self::$_singular['merged']['irregular'])) . ')';
419: }
420:
421: if (preg_match('/(.*)\\b(' . self::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
422: self::$_cache['singularize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1);
423: return self::$_cache['singularize'][$word];
424: }
425:
426: if (preg_match('/^(' . self::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
427: self::$_cache['singularize'][$word] = $word;
428: return $word;
429: }
430:
431: foreach (self::$_singular['rules'] as $rule => $replacement) {
432: if (preg_match($rule, $word)) {
433: self::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
434: return self::$_cache['singularize'][$word];
435: }
436: }
437: self::$_cache['singularize'][$word] = $word;
438: return $word;
439: }
440:
441: /**
442: * Returns the given lower_case_and_underscored_word as a CamelCased word.
443: *
444: * @param string $lowerCaseAndUnderscoredWord Word to camelize
445: * @return string Camelized word. LikeThis.
446: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::camelize
447: */
448: public static function camelize($lowerCaseAndUnderscoredWord) {
449: if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
450: $result = str_replace(' ', '', Inflector::humanize($lowerCaseAndUnderscoredWord));
451: self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
452: }
453: return $result;
454: }
455:
456: /**
457: * Returns the given camelCasedWord as an underscored_word.
458: *
459: * @param string $camelCasedWord Camel-cased word to be "underscorized"
460: * @return string Underscore-syntaxed version of the $camelCasedWord
461: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::underscore
462: */
463: public static function underscore($camelCasedWord) {
464: if (!($result = self::_cache(__FUNCTION__, $camelCasedWord))) {
465: $result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\1円', $camelCasedWord));
466: self::_cache(__FUNCTION__, $camelCasedWord, $result);
467: }
468: return $result;
469: }
470:
471: /**
472: * Returns the given underscored_word_group as a Human Readable Word Group.
473: * (Underscores are replaced by spaces and capitalized following words.)
474: *
475: * @param string $lowerCaseAndUnderscoredWord String to be made more readable
476: * @return string Human-readable string
477: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::humanize
478: */
479: public static function humanize($lowerCaseAndUnderscoredWord) {
480: if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
481: $result = ucwords(str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
482: self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
483: }
484: return $result;
485: }
486:
487: /**
488: * Returns corresponding table name for given model $className. ("people" for the model class "Person").
489: *
490: * @param string $className Name of class to get database table name for
491: * @return string Name of the database table for given class
492: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::tableize
493: */
494: public static function tableize($className) {
495: if (!($result = self::_cache(__FUNCTION__, $className))) {
496: $result = Inflector::pluralize(Inflector::underscore($className));
497: self::_cache(__FUNCTION__, $className, $result);
498: }
499: return $result;
500: }
501:
502: /**
503: * Returns Cake model class name ("Person" for the database table "people".) for given database table.
504: *
505: * @param string $tableName Name of database table to get class name for
506: * @return string Class name
507: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::classify
508: */
509: public static function classify($tableName) {
510: if (!($result = self::_cache(__FUNCTION__, $tableName))) {
511: $result = Inflector::camelize(Inflector::singularize($tableName));
512: self::_cache(__FUNCTION__, $tableName, $result);
513: }
514: return $result;
515: }
516:
517: /**
518: * Returns camelBacked version of an underscored string.
519: *
520: * @param string $string
521: * @return string in variable form
522: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::variable
523: */
524: public static function variable($string) {
525: if (!($result = self::_cache(__FUNCTION__, $string))) {
526: $string2 = Inflector::camelize(Inflector::underscore($string));
527: $replace = strtolower(substr($string2, 0, 1));
528: $result = preg_replace('/\\w/', $replace, $string2, 1);
529: self::_cache(__FUNCTION__, $string, $result);
530: }
531: return $result;
532: }
533:
534: /**
535: * Returns a string with all spaces converted to underscores (by default), accented
536: * characters converted to non-accented characters, and non word characters removed.
537: *
538: * @param string $string the string you want to slug
539: * @param string $replacement will replace keys in map
540: * @return string
541: * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::slug
542: */
543: public static function slug($string, $replacement = '_') {
544: $quotedReplacement = preg_quote($replacement, '/');
545:
546: $merge = array(
547: '/[^\s\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
548: '/\\s+/' => $replacement,
549: sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
550: );
551:
552: $map = self::$_transliteration + $merge;
553: return preg_replace(array_keys($map), array_values($map), $string);
554: }
555: }
556:
557: // Store the initial state
558: Inflector::reset();
559: