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: class Inflector {
26:
27: 28: 29: 30: 31:
32: protected static $_plural = array(
33: 'rules' => array(
34: '/(s)tatus$/i' => '\1\2tatuses',
35: '/(quiz)$/i' => '\1zes',
36: '/^(ox)$/i' => '\1\2en',
37: '/([m|l])ouse$/i' => '\1ice',
38: '/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
39: '/(x|ch|ss|sh)$/i' => '\1es',
40: '/([^aeiouy]|qu)y$/i' => '\1ies',
41: '/(hive)$/i' => '\1s',
42: '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
43: '/sis$/i' => 'ses',
44: '/([ti])um$/i' => '\1a',
45: '/(p)erson$/i' => '\1eople',
46: '/(m)an$/i' => '\1en',
47: '/(c)hild$/i' => '\1hildren',
48: '/(buffal|tomat)o$/i' => '\1\2oes',
49: '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
50: '/us$/i' => 'uses',
51: '/(alias)$/i' => '\1es',
52: '/(ax|cris|test)is$/i' => '\1es',
53: '/s$/' => 's',
54: '/^$/' => '',
55: '/$/' => 's',
56: ),
57: 'uninflected' => array(
58: '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', 'people', 'cookie'
59: ),
60: 'irregular' => array(
61: 'atlas' => 'atlases',
62: 'beef' => 'beefs',
63: 'brother' => 'brothers',
64: 'cafe' => 'cafes',
65: 'child' => 'children',
66: 'cookie' => 'cookies',
67: 'corpus' => 'corpuses',
68: 'cow' => 'cows',
69: 'ganglion' => 'ganglions',
70: 'genie' => 'genies',
71: 'genus' => 'genera',
72: 'graffito' => 'graffiti',
73: 'hoof' => 'hoofs',
74: 'loaf' => 'loaves',
75: 'man' => 'men',
76: 'money' => 'monies',
77: 'mongoose' => 'mongooses',
78: 'move' => 'moves',
79: 'mythos' => 'mythoi',
80: 'niche' => 'niches',
81: 'numen' => 'numina',
82: 'occiput' => 'occiputs',
83: 'octopus' => 'octopuses',
84: 'opus' => 'opuses',
85: 'ox' => 'oxen',
86: 'penis' => 'penises',
87: 'person' => 'people',
88: 'sex' => 'sexes',
89: 'soliloquy' => 'soliloquies',
90: 'testis' => 'testes',
91: 'trilby' => 'trilbys',
92: 'turf' => 'turfs'
93: )
94: );
95:
96: 97: 98: 99: 100:
101: protected static $_singular = array(
102: 'rules' => array(
103: '/(s)tatuses$/i' => '\1\2tatus',
104: '/^(.*)(menu)s$/i' => '\1\2',
105: '/(quiz)zes$/i' => '\\1',
106: '/(matr)ices$/i' => '\1ix',
107: '/(vert|ind)ices$/i' => '\1ex',
108: '/^(ox)en/i' => '\1',
109: '/(alias)(es)*$/i' => '\1',
110: '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
111: '/([ftw]ax)es/i' => '\1',
112: '/(cris|ax|test)es$/i' => '\1is',
113: '/(shoe|slave)s$/i' => '\1',
114: '/(o)es$/i' => '\1',
115: '/ouses$/' => 'ouse',
116: '/([^a])uses$/' => '\1us',
117: '/([m|l])ice$/i' => '\1ouse',
118: '/(x|ch|ss|sh)es$/i' => '\1',
119: '/(m)ovies$/i' => '\1\2ovie',
120: '/(s)eries$/i' => '\1\2eries',
121: '/([^aeiouy]|qu)ies$/i' => '\1y',
122: '/([lr])ves$/i' => '\1f',
123: '/(tive)s$/i' => '\1',
124: '/(hive)s$/i' => '\1',
125: '/(drive)s$/i' => '\1',
126: '/([^fo])ves$/i' => '\1fe',
127: '/(^analy)ses$/i' => '\1sis',
128: '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
129: '/([ti])a$/i' => '\1um',
130: '/(p)eople$/i' => '\1\2erson',
131: '/(m)en$/i' => '\1an',
132: '/(c)hildren$/i' => '\1\2hild',
133: '/(n)ews$/i' => '\1\2ews',
134: '/eaus$/' => 'eau',
135: '/^(.*us)$/' => '\\1',
136: '/s$/i' => ''
137: ),
138: 'uninflected' => array(
139: '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss'
140: ),
141: 'irregular' => array(
142: 'foes' => 'foe',
143: 'waves' => 'wave',
144: 'curves' => 'curve'
145: )
146: );
147:
148: 149: 150: 151: 152:
153: protected static $_uninflected = array(
154: 'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus',
155: 'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps',
156: 'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder',
157: 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
158: 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings',
159: 'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', '.*?media',
160: 'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
161: 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
162: 'proceedings', 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
163: 'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
164: 'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
165: 'Yengeese'
166: );
167:
168: 169: 170: 171: 172:
173: protected static $_transliteration = array(
174: '/ä|æ|ǽ/' => 'ae',
175: '/ö|œ/' => 'oe',
176: '/ü/' => 'ue',
177: '/Ä/' => 'Ae',
178: '/Ü/' => 'Ue',
179: '/Ö/' => 'Oe',
180: '/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A',
181: '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' => 'a',
182: '/Ç|Ć|Ĉ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/' => 'y',
214: '/Ŵ/' => 'W',
215: '/ŵ/' => 'w',
216: '/Ź|Ż|Ž/' => 'Z',
217: '/ź|ż|ž/' => 'z',
218: '/Æ|Ǽ/' => 'AE',
219: '/ß/' => 'ss',
220: '/IJ/' => 'IJ',
221: '/ij/' => 'ij',
222: '/Œ/' => 'OE',
223: '/ƒ/' => 'f'
224: );
225:
226: 227: 228: 229: 230:
231: protected static $_cache = array();
232:
233: 234: 235: 236: 237:
238: protected static $_initialState = array();
239:
240: 241: 242: 243: 244: 245: 246: 247:
248: protected static function _cache($type, $key, $value = false) {
249: $key = '_' . $key;
250: $type = '_' . $type;
251: if ($value !== false) {
252: self::$_cache[$type][$key] = $value;
253: return $value;
254: }
255: if (!isset(self::$_cache[$type][$key])) {
256: return false;
257: }
258: return self::$_cache[$type][$key];
259: }
260:
261: 262: 263: 264: 265: 266:
267: public static function reset() {
268: if (empty(self::$_initialState)) {
269: self::$_initialState = get_class_vars('Inflector');
270: return;
271: }
272: foreach (self::$_initialState as $key => $val) {
273: if ($key != '_initialState') {
274: self::${$key} = $val;
275: }
276: }
277: }
278:
279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299:
300: public static function rules($type, $rules, $reset = false) {
301: $var = '_' . $type;
302:
303: switch ($type) {
304: case 'transliteration':
305: if ($reset) {
306: self::$_transliteration = $rules;
307: } else {
308: self::$_transliteration = $rules + self::$_transliteration;
309: }
310: break;
311:
312: default:
313: foreach ($rules as $rule => $pattern) {
314: if (is_array($pattern)) {
315: if ($reset) {
316: self::${$var}[$rule] = $pattern;
317: } else {
318: if ($rule === 'uninflected') {
319: self::${$var}[$rule] = array_merge($pattern, self::${$var}[$rule]);
320: } else {
321: self::${$var}[$rule] = $pattern + self::${$var}[$rule];
322: }
323: }
324: unset($rules[$rule], self::${$var}['cache' . ucfirst($rule)]);
325: if (isset(self::${$var}['merged'][$rule])) {
326: unset(self::${$var}['merged'][$rule]);
327: }
328: if ($type === 'plural') {
329: self::$_cache['pluralize'] = self::$_cache['tableize'] = array();
330: } elseif ($type === 'singular') {
331: self::$_cache['singularize'] = array();
332: }
333: }
334: }
335: self::${$var}['rules'] = $rules + self::${$var}['rules'];
336: break;
337: }
338: }
339:
340: 341: 342: 343: 344: 345: 346:
347: public static function pluralize($word) {
348: if (isset(self::$_cache['pluralize'][$word])) {
349: return self::$_cache['pluralize'][$word];
350: }
351:
352: if (!isset(self::$_plural['merged']['irregular'])) {
353: self::$_plural['merged']['irregular'] = self::$_plural['irregular'];
354: }
355:
356: if (!isset(self::$_plural['merged']['uninflected'])) {
357: self::$_plural['merged']['uninflected'] = array_merge(self::$_plural['uninflected'], self::$_uninflected);
358: }
359:
360: if (!isset(self::$_plural['cacheUninflected']) || !isset(self::$_plural['cacheIrregular'])) {
361: self::$_plural['cacheUninflected'] = '(?:' . implode('|', self::$_plural['merged']['uninflected']) . ')';
362: self::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_plural['merged']['irregular'])) . ')';
363: }
364:
365: if (preg_match('/(.*)\\b(' . self::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) {
366: self::$_cache['pluralize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1);
367: return self::$_cache['pluralize'][$word];
368: }
369:
370: if (preg_match('/^(' . self::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
371: self::$_cache['pluralize'][$word] = $word;
372: return $word;
373: }
374:
375: foreach (self::$_plural['rules'] as $rule => $replacement) {
376: if (preg_match($rule, $word)) {
377: self::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
378: return self::$_cache['pluralize'][$word];
379: }
380: }
381: }
382:
383: 384: 385: 386: 387: 388: 389:
390: public static function singularize($word) {
391: if (isset(self::$_cache['singularize'][$word])) {
392: return self::$_cache['singularize'][$word];
393: }
394:
395: if (!isset(self::$_singular['merged']['uninflected'])) {
396: self::$_singular['merged']['uninflected'] = array_merge(
397: self::$_singular['uninflected'],
398: self::$_uninflected
399: );
400: }
401:
402: if (!isset(self::$_singular['merged']['irregular'])) {
403: self::$_singular['merged']['irregular'] = array_merge(
404: self::$_singular['irregular'],
405: array_flip(self::$_plural['irregular'])
406: );
407: }
408:
409: if (!isset(self::$_singular['cacheUninflected']) || !isset(self::$_singular['cacheIrregular'])) {
410: self::$_singular['cacheUninflected'] = '(?:' . join('|', self::$_singular['merged']['uninflected']) . ')';
411: self::$_singular['cacheIrregular'] = '(?:' . join('|', array_keys(self::$_singular['merged']['irregular'])) . ')';
412: }
413:
414: if (preg_match('/(.*)\\b(' . self::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
415: self::$_cache['singularize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1);
416: return self::$_cache['singularize'][$word];
417: }
418:
419: if (preg_match('/^(' . self::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
420: self::$_cache['singularize'][$word] = $word;
421: return $word;
422: }
423:
424: foreach (self::$_singular['rules'] as $rule => $replacement) {
425: if (preg_match($rule, $word)) {
426: self::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
427: return self::$_cache['singularize'][$word];
428: }
429: }
430: self::$_cache['singularize'][$word] = $word;
431: return $word;
432: }
433:
434: 435: 436: 437: 438: 439: 440:
441: public static function camelize($lowerCaseAndUnderscoredWord) {
442: if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
443: $result = str_replace(' ', '', Inflector::humanize($lowerCaseAndUnderscoredWord));
444: self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
445: }
446: return $result;
447: }
448:
449: 450: 451: 452: 453: 454: 455:
456: public static function underscore($camelCasedWord) {
457: if (!($result = self::_cache(__FUNCTION__, $camelCasedWord))) {
458: $result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
459: self::_cache(__FUNCTION__, $camelCasedWord, $result);
460: }
461: return $result;
462: }
463:
464: 465: 466: 467: 468: 469: 470: 471:
472: public static function humanize($lowerCaseAndUnderscoredWord) {
473: if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
474: $result = ucwords(str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
475: self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
476: }
477: return $result;
478: }
479:
480: 481: 482: 483: 484: 485: 486:
487: public static function tableize($className) {
488: if (!($result = self::_cache(__FUNCTION__, $className))) {
489: $result = Inflector::pluralize(Inflector::underscore($className));
490: self::_cache(__FUNCTION__, $className, $result);
491: }
492: return $result;
493: }
494:
495: 496: 497: 498: 499: 500: 501:
502: public static function classify($tableName) {
503: if (!($result = self::_cache(__FUNCTION__, $tableName))) {
504: $result = Inflector::camelize(Inflector::singularize($tableName));
505: self::_cache(__FUNCTION__, $tableName, $result);
506: }
507: return $result;
508: }
509:
510: 511: 512: 513: 514: 515: 516:
517: public static function variable($string) {
518: if (!($result = self::_cache(__FUNCTION__, $string))) {
519: $camelized = Inflector::camelize(Inflector::underscore($string));
520: $replace = strtolower(substr($camelized, 0, 1));
521: $result = preg_replace('/\\w/', $replace, $camelized, 1);
522: self::_cache(__FUNCTION__, $string, $result);
523: }
524: return $result;
525: }
526:
527: 528: 529: 530: 531: 532: 533: 534: 535:
536: public static function slug($string, $replacement = '_') {
537: $quotedReplacement = preg_quote($replacement, '/');
538:
539: $merge = array(
540: '/[^\s\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
541: '/\\s+/' => $replacement,
542: sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
543: );
544:
545: $map = self::$_transliteration + $merge;
546: return preg_replace(array_keys($map), array_values($map), $string);
547: }
548:
549: }
550:
551:
552: Inflector::reset();
553: