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