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