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