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