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: 'brief' => 'briefs',
65: 'brother' => 'brothers',
66: 'cafe' => 'cafes',
67: 'child' => 'children',
68: 'cookie' => 'cookies',
69: 'corpus' => 'corpuses',
70: 'cow' => 'cows',
71: 'ganglion' => 'ganglions',
72: 'genie' => 'genies',
73: 'genus' => 'genera',
74: 'graffito' => 'graffiti',
75: 'hoof' => 'hoofs',
76: 'loaf' => 'loaves',
77: 'man' => 'men',
78: 'money' => 'monies',
79: 'mongoose' => 'mongooses',
80: 'move' => 'moves',
81: 'mythos' => 'mythoi',
82: 'niche' => 'niches',
83: 'numen' => 'numina',
84: 'occiput' => 'occiputs',
85: 'octopus' => 'octopuses',
86: 'opus' => 'opuses',
87: 'ox' => 'oxen',
88: 'penis' => 'penises',
89: 'person' => 'people',
90: 'sex' => 'sexes',
91: 'soliloquy' => 'soliloquies',
92: 'testis' => 'testes',
93: 'trilby' => 'trilbys',
94: 'turf' => 'turfs',
95: 'potato' => 'potatoes',
96: 'hero' => 'heroes',
97: 'tooth' => 'teeth',
98: 'goose' => 'geese',
99: 'foot' => 'feet'
100: )
101: );
102:
103: 104: 105: 106: 107:
108: protected static $_singular = array(
109: 'rules' => array(
110: '/(s)tatuses$/i' => '\1\2tatus',
111: '/^(.*)(menu)s$/i' => '\1\2',
112: '/(quiz)zes$/i' => '\\1',
113: '/(matr)ices$/i' => '\1ix',
114: '/(vert|ind)ices$/i' => '\1ex',
115: '/^(ox)en/i' => '\1',
116: '/(alias)(es)*$/i' => '\1',
117: '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
118: '/([ftw]ax)es/i' => '\1',
119: '/(cris|ax|test)es$/i' => '\1is',
120: '/(shoe)s$/i' => '\1',
121: '/(o)es$/i' => '\1',
122: '/ouses$/' => 'ouse',
123: '/([^a])uses$/' => '\1us',
124: '/([m|l])ice$/i' => '\1ouse',
125: '/(x|ch|ss|sh)es$/i' => '\1',
126: '/(m)ovies$/i' => '\1\2ovie',
127: '/(s)eries$/i' => '\1\2eries',
128: '/([^aeiouy]|qu)ies$/i' => '\1y',
129: '/(tive)s$/i' => '\1',
130: '/(hive)s$/i' => '\1',
131: '/(drive)s$/i' => '\1',
132: '/([le])ves$/i' => '\1f',
133: '/([^rfoa])ves$/i' => '\1fe',
134: '/(^analy)ses$/i' => '\1sis',
135: '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
136: '/([ti])a$/i' => '\1um',
137: '/(p)eople$/i' => '\1\2erson',
138: '/(m)en$/i' => '\1an',
139: '/(c)hildren$/i' => '\1\2hild',
140: '/(n)ews$/i' => '\1\2ews',
141: '/eaus$/' => 'eau',
142: '/^(.*us)$/' => '\\1',
143: '/s$/i' => ''
144: ),
145: 'uninflected' => array(
146: '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss'
147: ),
148: 'irregular' => array(
149: 'foes' => 'foe',
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: 'metadata', '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: '/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => '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ư|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u',
217: '/Ý|Ÿ|Ŷ/' => 'Y',
218: '/ý|ÿ|ŷ/' => '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: }
342: }
343:
344: 345: 346: 347: 348: 349: 350:
351: public static function pluralize($word) {
352: if (isset(self::$_cache['pluralize'][$word])) {
353: return self::$_cache['pluralize'][$word];
354: }
355:
356: if (!isset(self::$_plural['merged']['irregular'])) {
357: self::$_plural['merged']['irregular'] = self::$_plural['irregular'];
358: }
359:
360: if (!isset(self::$_plural['merged']['uninflected'])) {
361: self::$_plural['merged']['uninflected'] = array_merge(self::$_plural['uninflected'], self::$_uninflected);
362: }
363:
364: if (!isset(self::$_plural['cacheUninflected']) || !isset(self::$_plural['cacheIrregular'])) {
365: self::$_plural['cacheUninflected'] = '(?:' . implode('|', self::$_plural['merged']['uninflected']) . ')';
366: self::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_plural['merged']['irregular'])) . ')';
367: }
368:
369: if (preg_match('/(.*)\\b(' . self::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) {
370: self::$_cache['pluralize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1);
371: return self::$_cache['pluralize'][$word];
372: }
373:
374: if (preg_match('/^(' . self::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
375: self::$_cache['pluralize'][$word] = $word;
376: return $word;
377: }
378:
379: foreach (self::$_plural['rules'] as $rule => $replacement) {
380: if (preg_match($rule, $word)) {
381: self::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
382: return self::$_cache['pluralize'][$word];
383: }
384: }
385: }
386:
387: 388: 389: 390: 391: 392: 393:
394: public static function singularize($word) {
395: if (isset(self::$_cache['singularize'][$word])) {
396: return self::$_cache['singularize'][$word];
397: }
398:
399: if (!isset(self::$_singular['merged']['uninflected'])) {
400: self::$_singular['merged']['uninflected'] = array_merge(
401: self::$_singular['uninflected'],
402: self::$_uninflected
403: );
404: }
405:
406: if (!isset(self::$_singular['merged']['irregular'])) {
407: self::$_singular['merged']['irregular'] = array_merge(
408: self::$_singular['irregular'],
409: array_flip(self::$_plural['irregular'])
410: );
411: }
412:
413: if (!isset(self::$_singular['cacheUninflected']) || !isset(self::$_singular['cacheIrregular'])) {
414: self::$_singular['cacheUninflected'] = '(?:' . implode('|', self::$_singular['merged']['uninflected']) . ')';
415: self::$_singular['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_singular['merged']['irregular'])) . ')';
416: }
417:
418: if (preg_match('/(.*)\\b(' . self::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
419: self::$_cache['singularize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1);
420: return self::$_cache['singularize'][$word];
421: }
422:
423: if (preg_match('/^(' . self::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
424: self::$_cache['singularize'][$word] = $word;
425: return $word;
426: }
427:
428: foreach (self::$_singular['rules'] as $rule => $replacement) {
429: if (preg_match($rule, $word)) {
430: self::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
431: return self::$_cache['singularize'][$word];
432: }
433: }
434: self::$_cache['singularize'][$word] = $word;
435: return $word;
436: }
437:
438: 439: 440: 441: 442: 443: 444:
445: public static function camelize($lowerCaseAndUnderscoredWord) {
446: if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
447: $result = str_replace(' ', '', Inflector::humanize($lowerCaseAndUnderscoredWord));
448: self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
449: }
450: return $result;
451: }
452:
453: 454: 455: 456: 457: 458: 459:
460: public static function underscore($camelCasedWord) {
461: if (!($result = self::_cache(__FUNCTION__, $camelCasedWord))) {
462: $result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
463: self::_cache(__FUNCTION__, $camelCasedWord, $result);
464: }
465: return $result;
466: }
467:
468: 469: 470: 471: 472: 473: 474: 475:
476: public static function humanize($lowerCaseAndUnderscoredWord) {
477: if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
478: $result = ucwords(str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
479: self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
480: }
481: return $result;
482: }
483:
484: 485: 486: 487: 488: 489: 490:
491: public static function tableize($className) {
492: if (!($result = self::_cache(__FUNCTION__, $className))) {
493: $result = Inflector::pluralize(Inflector::underscore($className));
494: self::_cache(__FUNCTION__, $className, $result);
495: }
496: return $result;
497: }
498:
499: 500: 501: 502: 503: 504: 505:
506: public static function classify($tableName) {
507: if (!($result = self::_cache(__FUNCTION__, $tableName))) {
508: $result = Inflector::camelize(Inflector::singularize($tableName));
509: self::_cache(__FUNCTION__, $tableName, $result);
510: }
511: return $result;
512: }
513:
514: 515: 516: 517: 518: 519: 520:
521: public static function variable($string) {
522: if (!($result = self::_cache(__FUNCTION__, $string))) {
523: $camelized = Inflector::camelize(Inflector::underscore($string));
524: $replace = strtolower(substr($camelized, 0, 1));
525: $result = preg_replace('/\\w/', $replace, $camelized, 1);
526: self::_cache(__FUNCTION__, $string, $result);
527: }
528: return $result;
529: }
530:
531: 532: 533: 534: 535: 536: 537: 538: 539:
540: public static function slug($string, $replacement = '_') {
541: $quotedReplacement = preg_quote($replacement, '/');
542:
543: $merge = array(
544: '/[^\s\p{Zs}\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
545: '/[\s\p{Zs}]+/mu' => $replacement,
546: sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
547: );
548:
549: $map = self::$_transliteration + $merge;
550: return preg_replace(array_keys($map), array_values($map), $string);
551: }
552:
553: }
554:
555:
556: Inflector::reset();
557: