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: 27: 28: 29: 30:
31: class Inflector {
32:
33: 34: 35: 36: 37:
38: protected static $_plural = array(
39: 'rules' => array(
40: '/(s)tatus$/i' => '\1\2tatuses',
41: '/(quiz)$/i' => '\1zes',
42: '/^(ox)$/i' => '\1\2en',
43: '/([m|l])ouse$/i' => '\1ice',
44: '/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
45: '/(x|ch|ss|sh)$/i' => '\1es',
46: '/([^aeiouy]|qu)y$/i' => '\1ies',
47: '/(hive)$/i' => '\1s',
48: '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
49: '/sis$/i' => 'ses',
50: '/([ti])um$/i' => '\1a',
51: '/(p)erson$/i' => '\1eople',
52: '/(m)an$/i' => '\1en',
53: '/(c)hild$/i' => '\1hildren',
54: '/(buffal|tomat)o$/i' => '\1\2oes',
55: '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
56: '/us$/i' => 'uses',
57: '/(alias)$/i' => '\1es',
58: '/(ax|cris|test)is$/i' => '\1es',
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: 103: 104: 105:
106: protected static $_singular = array(
107: 'rules' => array(
108: '/(s)tatuses$/i' => '\1\2tatus',
109: '/^(.*)(menu)s$/i' => '\1\2',
110: '/(quiz)zes$/i' => '\\1',
111: '/(matr)ices$/i' => '\1ix',
112: '/(vert|ind)ices$/i' => '\1ex',
113: '/^(ox)en/i' => '\1',
114: '/(alias)(es)*$/i' => '\1',
115: '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
116: '/([ftw]ax)es/i' => '\1',
117: '/(cris|ax|test)es$/i' => '\1is',
118: '/(shoe|slave)s$/i' => '\1',
119: '/(o)es$/i' => '\1',
120: '/ouses$/' => 'ouse',
121: '/([^a])uses$/' => '\1us',
122: '/([m|l])ice$/i' => '\1ouse',
123: '/(x|ch|ss|sh)es$/i' => '\1',
124: '/(m)ovies$/i' => '\1\2ovie',
125: '/(s)eries$/i' => '\1\2eries',
126: '/([^aeiouy]|qu)ies$/i' => '\1y',
127: '/([lr])ves$/i' => '\1f',
128: '/(tive)s$/i' => '\1',
129: '/(hive)s$/i' => '\1',
130: '/(drive)s$/i' => '\1',
131: '/([^fo])ves$/i' => '\1fe',
132: '/(^analy)ses$/i' => '\1sis',
133: '/(analy|ba|diagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
134: '/([ti])a$/i' => '\1um',
135: '/(p)eople$/i' => '\1\2erson',
136: '/(m)en$/i' => '\1an',
137: '/(c)hildren$/i' => '\1\2hild',
138: '/(n)ews$/i' => '\1\2ews',
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: 155: 156: 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: 175: 176: 177:
178: protected static $_transliteration = array(
179: '/ä|æ|ǽ/' => 'ae',
180: '/ö|œ/' => 'oe',
181: '/ü/' => 'ue',
182: '/Ä/' => 'Ae',
183: '/Ü/' => 'Ue',
184: '/Ö/' => 'Oe',
185: '/À|Á|Â|Ã|Ä|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A',
186: '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' => 'a',
187: '/Ç|Ć|Ĉ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/' => '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: 233: 234: 235:
236: protected static $_cache = array();
237:
238: 239: 240: 241: 242:
243: protected static $_initialState = array();
244:
245: 246: 247: 248: 249: 250: 251: 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: 268: 269: 270: 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: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 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: 347: 348: 349: 350: 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: 391: 392: 393: 394: 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: 443: 444: 445: 446: 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: 458: 459: 460: 461: 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: 473: 474: 475: 476: 477: 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: 489: 490: 491: 492: 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: 504: 505: 506: 507: 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: 519: 520: 521: 522: 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: 536: 537: 538: 539: 540: 541: 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:
558: Inflector::reset();
559: