1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16:
17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class Inflector {
27:
28: 29: 30: 31: 32:
33: protected static $_plural = array(
34: 'rules' => array(
35: '/(s)tatus$/i' => '\1tatuses',
36: '/(quiz)$/i' => '\1zes',
37: '/^(ox)$/i' => '\1\2en',
38: '/([m|l])ouse$/i' => '\1ice',
39: '/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
40: '/(x|ch|ss|sh)$/i' => '\1es',
41: '/([^aeiouy]|qu)y$/i' => '\1ies',
42: '/(hive)$/i' => '\1s',
43: '/(?:([^f])fe|([lre])f)$/i' => '\1\2ves',
44: '/sis$/i' => 'ses',
45: '/([ti])um$/i' => '\1a',
46: '/(p)erson$/i' => '\1eople',
47: '/(?<!u)(m)an$/i' => '\1en',
48: '/(c)hild$/i' => '\1hildren',
49: '/(buffal|tomat)o$/i' => '\1\2oes',
50: '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin)us$/i' => '\1i',
51: '/us$/i' => 'uses',
52: '/(alias)$/i' => '\1es',
53: '/(ax|cris|test)is$/i' => '\1es',
54: '/s$/' => 's',
55: '/^$/' => '',
56: '/$/' => 's',
57: ),
58: 'uninflected' => array(
59: '.*[nrlm]ese',
60: '.*data',
61: '.*deer',
62: '.*fish',
63: '.*measles',
64: '.*ois',
65: '.*pox',
66: '.*sheep',
67: 'people',
68: 'feedback',
69: 'stadia'
70: ),
71: 'irregular' => array(
72: 'atlas' => 'atlases',
73: 'beef' => 'beefs',
74: 'brief' => 'briefs',
75: 'brother' => 'brothers',
76: 'cafe' => 'cafes',
77: 'child' => 'children',
78: 'cookie' => 'cookies',
79: 'corpus' => 'corpuses',
80: 'cow' => 'cows',
81: 'criterion' => 'criteria',
82: 'ganglion' => 'ganglions',
83: 'genie' => 'genies',
84: 'genus' => 'genera',
85: 'graffito' => 'graffiti',
86: 'hoof' => 'hoofs',
87: 'loaf' => 'loaves',
88: 'man' => 'men',
89: 'money' => 'monies',
90: 'mongoose' => 'mongooses',
91: 'move' => 'moves',
92: 'mythos' => 'mythoi',
93: 'niche' => 'niches',
94: 'numen' => 'numina',
95: 'occiput' => 'occiputs',
96: 'octopus' => 'octopuses',
97: 'opus' => 'opuses',
98: 'ox' => 'oxen',
99: 'penis' => 'penises',
100: 'person' => 'people',
101: 'sex' => 'sexes',
102: 'soliloquy' => 'soliloquies',
103: 'testis' => 'testes',
104: 'trilby' => 'trilbys',
105: 'turf' => 'turfs',
106: 'potato' => 'potatoes',
107: 'hero' => 'heroes',
108: 'tooth' => 'teeth',
109: 'goose' => 'geese',
110: 'foot' => 'feet',
111: 'sieve' => 'sieves'
112: )
113: );
114:
115: 116: 117: 118: 119:
120: protected static $_singular = array(
121: 'rules' => array(
122: '/(s)tatuses$/i' => '\1\2tatus',
123: '/^(.*)(menu)s$/i' => '\1\2',
124: '/(quiz)zes$/i' => '\\1',
125: '/(matr)ices$/i' => '\1ix',
126: '/(vert|ind)ices$/i' => '\1ex',
127: '/^(ox)en/i' => '\1',
128: '/(alias)(es)*$/i' => '\1',
129: '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
130: '/([ftw]ax)es/i' => '\1',
131: '/(cris|ax|test)es$/i' => '\1is',
132: '/(shoe)s$/i' => '\1',
133: '/(o)es$/i' => '\1',
134: '/ouses$/' => 'ouse',
135: '/([^a])uses$/' => '\1us',
136: '/([m|l])ice$/i' => '\1ouse',
137: '/(x|ch|ss|sh)es$/i' => '\1',
138: '/(m)ovies$/i' => '\1\2ovie',
139: '/(s)eries$/i' => '\1\2eries',
140: '/([^aeiouy]|qu)ies$/i' => '\1y',
141: '/(tive)s$/i' => '\1',
142: '/(hive)s$/i' => '\1',
143: '/(drive)s$/i' => '\1',
144: '/([le])ves$/i' => '\1f',
145: '/([^rfoa])ves$/i' => '\1fe',
146: '/(^analy)ses$/i' => '\1sis',
147: '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
148: '/([ti])a$/i' => '\1um',
149: '/(p)eople$/i' => '\1\2erson',
150: '/(m)en$/i' => '\1an',
151: '/(c)hildren$/i' => '\1\2hild',
152: '/(n)ews$/i' => '\1\2ews',
153: '/eaus$/' => 'eau',
154: '/^(.*us)$/' => '\\1',
155: '/s$/i' => ''
156: ),
157: 'uninflected' => array(
158: '.*data',
159: '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss', 'feedback'
160: ),
161: 'irregular' => array(
162: 'foes' => 'foe',
163: )
164: );
165:
166: 167: 168: 169: 170:
171: protected static $_uninflected = array(
172: 'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus',
173: 'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps',
174: 'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder',
175: 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
176: 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings',
177: 'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', '.*?media',
178: 'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
179: 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
180: 'proceedings', 'rabies', 'research', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
181: 'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
182: 'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
183: 'Yengeese'
184: );
185:
186: 187: 188: 189: 190:
191: protected static $_transliteration = array(
192: '/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A',
193: '/Æ|Ǽ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|ǐ|į|ı|і/' => 'i',
230: '/ij/' => 'ij',
231: '/ĵ/' => 'j',
232: '/ķ/' => 'k',
233: '/ĺ|ļ|ľ|ŀ|ł/' => 'l',
234: '/ñ|ń|ņ|ň|ʼn/' => 'n',
235: '/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º/' => 'o',
236: '/ö|œ/' => 'oe',
237: '/ŕ|ŗ|ř/' => 'r',
238: '/ś|ŝ|ş|ș|š|ſ/' => 's',
239: '/ß/' => 'ss',
240: '/ţ|ț|ť|ŧ/' => 't',
241: '/þ/' => 'th',
242: '/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u',
243: '/ü/' => 'ue',
244: '/ŵ/' => 'w',
245: '/ý|ÿ|ŷ/' => 'y',
246: '/є/' => 'ye',
247: '/ї/' => 'yi',
248: '/ź|ż|ž/' => 'z',
249: );
250:
251: 252: 253: 254: 255:
256: protected static $_cache = array();
257:
258: 259: 260: 261: 262:
263: protected static $_initialState = array();
264:
265: 266: 267: 268: 269: 270: 271: 272:
273: protected static function _cache($type, $key, $value = false) {
274: $key = '_' . $key;
275: $type = '_' . $type;
276: if ($value !== false) {
277: static::$_cache[$type][$key] = $value;
278: return $value;
279: }
280: if (!isset(static::$_cache[$type][$key])) {
281: return false;
282: }
283: return static::$_cache[$type][$key];
284: }
285:
286: 287: 288: 289: 290: 291:
292: public static function reset() {
293: if (empty(static::$_initialState)) {
294: static::$_initialState = get_class_vars('Inflector');
295: return;
296: }
297: foreach (static::$_initialState as $key => $val) {
298: if ($key !== '_initialState') {
299: static::${$key} = $val;
300: }
301: }
302: }
303:
304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324:
325: public static function rules($type, $rules, $reset = false) {
326: $var = '_' . $type;
327:
328: switch ($type) {
329: case 'transliteration':
330: if ($reset) {
331: static::$_transliteration = $rules;
332: } else {
333: static::$_transliteration = $rules + static::$_transliteration;
334: }
335: break;
336:
337: default:
338: foreach ($rules as $rule => $pattern) {
339: if (is_array($pattern)) {
340: if ($reset) {
341: static::${$var}[$rule] = $pattern;
342: } else {
343: if ($rule === 'uninflected') {
344: static::${$var}[$rule] = array_merge($pattern, static::${$var}[$rule]);
345: } else {
346: static::${$var}[$rule] = $pattern + static::${$var}[$rule];
347: }
348: }
349: unset($rules[$rule], static::${$var}['cache' . ucfirst($rule)]);
350: if (isset(static::${$var}['merged'][$rule])) {
351: unset(static::${$var}['merged'][$rule]);
352: }
353: if ($type === 'plural') {
354: static::$_cache['pluralize'] = static::$_cache['tableize'] = array();
355: } elseif ($type === 'singular') {
356: static::$_cache['singularize'] = array();
357: }
358: }
359: }
360: static::${$var}['rules'] = $rules + static::${$var}['rules'];
361: }
362: }
363:
364: 365: 366: 367: 368: 369: 370:
371: public static function pluralize($word) {
372: if (isset(static::$_cache['pluralize'][$word])) {
373: return static::$_cache['pluralize'][$word];
374: }
375:
376: if (!isset(static::$_plural['merged']['irregular'])) {
377: static::$_plural['merged']['irregular'] = static::$_plural['irregular'];
378: }
379:
380: if (!isset(static::$_plural['merged']['uninflected'])) {
381: static::$_plural['merged']['uninflected'] = array_merge(static::$_plural['uninflected'], static::$_uninflected);
382: }
383:
384: if (!isset(static::$_plural['cacheUninflected']) || !isset(static::$_plural['cacheIrregular'])) {
385: static::$_plural['cacheUninflected'] = '(?:' . implode('|', static::$_plural['merged']['uninflected']) . ')';
386: static::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(static::$_plural['merged']['irregular'])) . ')';
387: }
388:
389: if (preg_match('/(.*?(?:\\b|_))(' . static::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) {
390: static::$_cache['pluralize'][$word] = $regs[1] .
391: substr($regs[2], 0, 1) .
392: substr(static::$_plural['merged']['irregular'][strtolower($regs[2])], 1);
393: return static::$_cache['pluralize'][$word];
394: }
395:
396: if (preg_match('/^(' . static::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
397: static::$_cache['pluralize'][$word] = $word;
398: return $word;
399: }
400:
401: foreach (static::$_plural['rules'] as $rule => $replacement) {
402: if (preg_match($rule, $word)) {
403: static::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
404: return static::$_cache['pluralize'][$word];
405: }
406: }
407: }
408:
409: 410: 411: 412: 413: 414: 415:
416: public static function singularize($word) {
417: if (isset(static::$_cache['singularize'][$word])) {
418: return static::$_cache['singularize'][$word];
419: }
420:
421: if (!isset(static::$_singular['merged']['uninflected'])) {
422: static::$_singular['merged']['uninflected'] = array_merge(
423: static::$_singular['uninflected'],
424: static::$_uninflected
425: );
426: }
427:
428: if (!isset(static::$_singular['merged']['irregular'])) {
429: static::$_singular['merged']['irregular'] = array_merge(
430: static::$_singular['irregular'],
431: array_flip(static::$_plural['irregular'])
432: );
433: }
434:
435: if (!isset(static::$_singular['cacheUninflected']) || !isset(static::$_singular['cacheIrregular'])) {
436: static::$_singular['cacheUninflected'] = '(?:' . implode('|', static::$_singular['merged']['uninflected']) . ')';
437: static::$_singular['cacheIrregular'] = '(?:' . implode('|', array_keys(static::$_singular['merged']['irregular'])) . ')';
438: }
439:
440: if (preg_match('/(.*?(?:\\b|_))(' . static::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
441: static::$_cache['singularize'][$word] = $regs[1] .
442: substr($regs[2], 0, 1) .
443: substr(static::$_singular['merged']['irregular'][strtolower($regs[2])], 1);
444: return static::$_cache['singularize'][$word];
445: }
446:
447: if (preg_match('/^(' . static::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
448: static::$_cache['singularize'][$word] = $word;
449: return $word;
450: }
451:
452: foreach (static::$_singular['rules'] as $rule => $replacement) {
453: if (preg_match($rule, $word)) {
454: static::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
455: return static::$_cache['singularize'][$word];
456: }
457: }
458: static::$_cache['singularize'][$word] = $word;
459: return $word;
460: }
461:
462: 463: 464: 465: 466: 467: 468:
469: public static function camelize($lowerCaseAndUnderscoredWord) {
470: if (!($result = static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
471: $result = str_replace(' ', '', Inflector::humanize($lowerCaseAndUnderscoredWord));
472: static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
473: }
474: return $result;
475: }
476:
477: 478: 479: 480: 481: 482: 483:
484: public static function underscore($camelCasedWord) {
485: if (!($result = static::_cache(__FUNCTION__, $camelCasedWord))) {
486: $underscoredWord = preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord);
487: $result = mb_strtolower($underscoredWord);
488: static::_cache(__FUNCTION__, $camelCasedWord, $result);
489: }
490: return $result;
491: }
492:
493: 494: 495: 496: 497: 498: 499: 500:
501: public static function humanize($lowerCaseAndUnderscoredWord) {
502: if (!($result = static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
503: $result = explode(' ', str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
504: foreach ($result as &$word) {
505: $word = mb_strtoupper(mb_substr($word, 0, 1)) . mb_substr($word, 1);
506: }
507: $result = implode(' ', $result);
508: static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
509: }
510: return $result;
511: }
512:
513: 514: 515: 516: 517: 518: 519:
520: public static function tableize($className) {
521: if (!($result = static::_cache(__FUNCTION__, $className))) {
522: $result = Inflector::pluralize(Inflector::underscore($className));
523: static::_cache(__FUNCTION__, $className, $result);
524: }
525: return $result;
526: }
527:
528: 529: 530: 531: 532: 533: 534:
535: public static function classify($tableName) {
536: if (!($result = static::_cache(__FUNCTION__, $tableName))) {
537: $result = Inflector::camelize(Inflector::singularize($tableName));
538: static::_cache(__FUNCTION__, $tableName, $result);
539: }
540: return $result;
541: }
542:
543: 544: 545: 546: 547: 548: 549:
550: public static function variable($string) {
551: if (!($result = static::_cache(__FUNCTION__, $string))) {
552: $camelized = Inflector::camelize(Inflector::underscore($string));
553: $replace = strtolower(substr($camelized, 0, 1));
554: $result = preg_replace('/\\w/', $replace, $camelized, 1);
555: static::_cache(__FUNCTION__, $string, $result);
556: }
557: return $result;
558: }
559:
560: 561: 562: 563: 564: 565: 566: 567: 568:
569: public static function slug($string, $replacement = '_') {
570: $quotedReplacement = preg_quote($replacement, '/');
571:
572: $merge = array(
573: '/[^\s\p{Zs}\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
574: '/[\s\p{Zs}]+/mu' => $replacement,
575: sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
576: );
577:
578: $map = static::$_transliteration + $merge;
579: return preg_replace(array_keys($map), array_values($map), $string);
580: }
581:
582: }
583:
584:
585: Inflector::reset();
586: