Inflector.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @package Cake.Utility
  13. * @since CakePHP(tm) v 0.2.9
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. /**
  17. * Pluralize and singularize English words.
  18. *
  19. * Inflector pluralizes and singularizes English nouns.
  20. * Used by CakePHP's naming conventions throughout the framework.
  21. *
  22. * @package Cake.Utility
  23. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html
  24. */
  25. class Inflector {
  26. /**
  27. * Plural inflector rules
  28. *
  29. * @var array
  30. */
  31. protected static $_plural = array(
  32. 'rules' => array(
  33. '/(s)tatus$/i' => '\1\2tatuses',
  34. '/(quiz)$/i' => '\1zes',
  35. '/^(ox)$/i' => '\1\2en',
  36. '/([m|l])ouse$/i' => '\1ice',
  37. '/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
  38. '/(x|ch|ss|sh)$/i' => '\1es',
  39. '/([^aeiouy]|qu)y$/i' => '\1ies',
  40. '/(hive)$/i' => '\1s',
  41. '/(?:([^f])fe|([lre])f)$/i' => '\1\2ves',
  42. '/sis$/i' => 'ses',
  43. '/([ti])um$/i' => '\1a',
  44. '/(p)erson$/i' => '\1eople',
  45. '/(m)an$/i' => '\1en',
  46. '/(c)hild$/i' => '\1hildren',
  47. '/(buffal|tomat)o$/i' => '\1\2oes',
  48. '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
  49. '/us$/i' => 'uses',
  50. '/(alias)$/i' => '\1es',
  51. '/(ax|cris|test)is$/i' => '\1es',
  52. '/s$/' => 's',
  53. '/^$/' => '',
  54. '/$/' => 's',
  55. ),
  56. 'uninflected' => array(
  57. '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', 'people'
  58. ),
  59. 'irregular' => array(
  60. 'atlas' => 'atlases',
  61. 'beef' => 'beefs',
  62. 'brief' => 'briefs',
  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. 'potato' => 'potatoes',
  94. 'hero' => 'heroes',
  95. 'tooth' => 'teeth',
  96. 'goose' => 'geese',
  97. 'foot' => 'feet'
  98. )
  99. );
  100. /**
  101. * Singular inflector rules
  102. *
  103. * @var array
  104. */
  105. protected static $_singular = array(
  106. 'rules' => array(
  107. '/(s)tatuses$/i' => '\1\2tatus',
  108. '/^(.*)(menu)s$/i' => '\1\2',
  109. '/(quiz)zes$/i' => '\\1',
  110. '/(matr)ices$/i' => '\1ix',
  111. '/(vert|ind)ices$/i' => '\1ex',
  112. '/^(ox)en/i' => '\1',
  113. '/(alias)(es)*$/i' => '\1',
  114. '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
  115. '/([ftw]ax)es/i' => '\1',
  116. '/(cris|ax|test)es$/i' => '\1is',
  117. '/(shoe)s$/i' => '\1',
  118. '/(o)es$/i' => '\1',
  119. '/ouses$/' => 'ouse',
  120. '/([^a])uses$/' => '\1us',
  121. '/([m|l])ice$/i' => '\1ouse',
  122. '/(x|ch|ss|sh)es$/i' => '\1',
  123. '/(m)ovies$/i' => '\1\2ovie',
  124. '/(s)eries$/i' => '\1\2eries',
  125. '/([^aeiouy]|qu)ies$/i' => '\1y',
  126. '/(tive)s$/i' => '\1',
  127. '/(hive)s$/i' => '\1',
  128. '/(drive)s$/i' => '\1',
  129. '/([le])ves$/i' => '\1f',
  130. '/([^rfoa])ves$/i' => '\1fe',
  131. '/(^analy)ses$/i' => '\1sis',
  132. '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
  133. '/([ti])a$/i' => '\1um',
  134. '/(p)eople$/i' => '\1\2erson',
  135. '/(m)en$/i' => '\1an',
  136. '/(c)hildren$/i' => '\1\2hild',
  137. '/(n)ews$/i' => '\1\2ews',
  138. '/eaus$/' => 'eau',
  139. '/^(.*us)$/' => '\\1',
  140. '/s$/i' => ''
  141. ),
  142. 'uninflected' => array(
  143. '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss'
  144. ),
  145. 'irregular' => array(
  146. 'foes' => 'foe',
  147. )
  148. );
  149. /**
  150. * Words that should not be inflected
  151. *
  152. * @var array
  153. */
  154. protected static $_uninflected = array(
  155. 'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus',
  156. 'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps',
  157. 'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder',
  158. 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
  159. 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings',
  160. 'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', '.*?media',
  161. 'metadata', 'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
  162. 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
  163. 'proceedings', 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
  164. 'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
  165. 'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
  166. 'Yengeese'
  167. );
  168. /**
  169. * Default map of accented and special characters to ASCII characters
  170. *
  171. * @var array
  172. */
  173. protected static $_transliteration = array(
  174. '/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A',
  175. '/Æ|Ǽ/' => 'AE',
  176. '/Ä/' => 'Ae',
  177. '/Ç|Ć|Ĉ|Ċ|Č/' => 'C',
  178. '/Ð|Ď|Đ/' => 'D',
  179. '/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/' => 'E',
  180. '/Ĝ|Ğ|Ġ|Ģ|Ґ/' => 'G',
  181. '/Ĥ|Ħ/' => 'H',
  182. '/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ|І/' => 'I',
  183. '/IJ/' => 'IJ',
  184. '/Ĵ/' => 'J',
  185. '/Ķ/' => 'K',
  186. '/Ĺ|Ļ|Ľ|Ŀ|Ł/' => 'L',
  187. '/Ñ|Ń|Ņ|Ň/' => 'N',
  188. '/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ/' => 'O',
  189. '/Œ/' => 'OE',
  190. '/Ö/' => 'Oe',
  191. '/Ŕ|Ŗ|Ř/' => 'R',
  192. '/Ś|Ŝ|Ş|Ș|Š/' => 'S',
  193. '/ẞ/' => 'SS',
  194. '/Ţ|Ț|Ť|Ŧ/' => 'T',
  195. '/Þ/' => 'TH',
  196. '/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/' => 'U',
  197. '/Ü/' => 'Ue',
  198. '/Ŵ/' => 'W',
  199. '/Ý|Ÿ|Ŷ/' => 'Y',
  200. '/Є/' => 'Ye',
  201. '/Ї/' => 'Yi',
  202. '/Ź|Ż|Ž/' => 'Z',
  203. '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' => 'a',
  204. '/ä|æ|ǽ/' => 'ae',
  205. '/ç|ć|ĉ|ċ|č/' => 'c',
  206. '/ð|ď|đ/' => 'd',
  207. '/è|é|ê|ë|ē|ĕ|ė|ę|ě/' => 'e',
  208. '/ƒ/' => 'f',
  209. '/ĝ|ğ|ġ|ģ|ґ/' => 'g',
  210. '/ĥ|ħ/' => 'h',
  211. '/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı|і/' => 'i',
  212. '/ij/' => 'ij',
  213. '/ĵ/' => 'j',
  214. '/ķ/' => 'k',
  215. '/ĺ|ļ|ľ|ŀ|ł/' => 'l',
  216. '/ñ|ń|ņ|ň|ʼn/' => 'n',
  217. '/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º/' => 'o',
  218. '/ö|œ/' => 'oe',
  219. '/ŕ|ŗ|ř/' => 'r',
  220. '/ś|ŝ|ş|ș|š|ſ/' => 's',
  221. '/ß/' => 'ss',
  222. '/ţ|ț|ť|ŧ/' => 't',
  223. '/þ/' => 'th',
  224. '/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u',
  225. '/ü/' => 'ue',
  226. '/ŵ/' => 'w',
  227. '/ý|ÿ|ŷ/' => 'y',
  228. '/є/' => 'ye',
  229. '/ї/' => 'yi',
  230. '/ź|ż|ž/' => 'z',
  231. );
  232. /**
  233. * Method cache array.
  234. *
  235. * @var array
  236. */
  237. protected static $_cache = array();
  238. /**
  239. * The initial state of Inflector so reset() works.
  240. *
  241. * @var array
  242. */
  243. protected static $_initialState = array();
  244. /**
  245. * Cache inflected values, and return if already available
  246. *
  247. * @param string $type Inflection type
  248. * @param string $key Original value
  249. * @param string $value Inflected value
  250. * @return string Inflected value, from cache
  251. */
  252. protected static function _cache($type, $key, $value = false) {
  253. $key = '_' . $key;
  254. $type = '_' . $type;
  255. if ($value !== false) {
  256. self::$_cache[$type][$key] = $value;
  257. return $value;
  258. }
  259. if (!isset(self::$_cache[$type][$key])) {
  260. return false;
  261. }
  262. return self::$_cache[$type][$key];
  263. }
  264. /**
  265. * Clears Inflectors inflected value caches. And resets the inflection
  266. * rules to the initial values.
  267. *
  268. * @return void
  269. */
  270. public static function reset() {
  271. if (empty(self::$_initialState)) {
  272. self::$_initialState = get_class_vars('Inflector');
  273. return;
  274. }
  275. foreach (self::$_initialState as $key => $val) {
  276. if ($key !== '_initialState') {
  277. self::${$key} = $val;
  278. }
  279. }
  280. }
  281. /**
  282. * Adds custom inflection $rules, of either 'plural', 'singular' or 'transliteration' $type.
  283. *
  284. * ### Usage:
  285. *
  286. * {{{
  287. * Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
  288. * Inflector::rules('plural', array(
  289. * 'rules' => array('/^(inflect)ors$/i' => '\1ables'),
  290. * 'uninflected' => array('dontinflectme'),
  291. * 'irregular' => array('red' => 'redlings')
  292. * ));
  293. * Inflector::rules('transliteration', array('/å/' => 'aa'));
  294. * }}}
  295. *
  296. * @param string $type The type of inflection, either 'plural', 'singular' or 'transliteration'
  297. * @param array $rules Array of rules to be added.
  298. * @param boolean $reset If true, will unset default inflections for all
  299. * new rules that are being defined in $rules.
  300. * @return void
  301. */
  302. public static function rules($type, $rules, $reset = false) {
  303. $var = '_' . $type;
  304. switch ($type) {
  305. case 'transliteration':
  306. if ($reset) {
  307. self::$_transliteration = $rules;
  308. } else {
  309. self::$_transliteration = $rules + self::$_transliteration;
  310. }
  311. break;
  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. }
  337. }
  338. /**
  339. * Return $word in plural form.
  340. *
  341. * @param string $word Word in singular
  342. * @return string Word in plural
  343. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::pluralize
  344. */
  345. public static function pluralize($word) {
  346. if (isset(self::$_cache['pluralize'][$word])) {
  347. return self::$_cache['pluralize'][$word];
  348. }
  349. if (!isset(self::$_plural['merged']['irregular'])) {
  350. self::$_plural['merged']['irregular'] = self::$_plural['irregular'];
  351. }
  352. if (!isset(self::$_plural['merged']['uninflected'])) {
  353. self::$_plural['merged']['uninflected'] = array_merge(self::$_plural['uninflected'], self::$_uninflected);
  354. }
  355. if (!isset(self::$_plural['cacheUninflected']) || !isset(self::$_plural['cacheIrregular'])) {
  356. self::$_plural['cacheUninflected'] = '(?:' . implode('|', self::$_plural['merged']['uninflected']) . ')';
  357. self::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_plural['merged']['irregular'])) . ')';
  358. }
  359. if (preg_match('/(.*)\\b(' . self::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) {
  360. self::$_cache['pluralize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1);
  361. return self::$_cache['pluralize'][$word];
  362. }
  363. if (preg_match('/^(' . self::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
  364. self::$_cache['pluralize'][$word] = $word;
  365. return $word;
  366. }
  367. foreach (self::$_plural['rules'] as $rule => $replacement) {
  368. if (preg_match($rule, $word)) {
  369. self::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
  370. return self::$_cache['pluralize'][$word];
  371. }
  372. }
  373. }
  374. /**
  375. * Return $word in singular form.
  376. *
  377. * @param string $word Word in plural
  378. * @return string Word in singular
  379. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::singularize
  380. */
  381. public static function singularize($word) {
  382. if (isset(self::$_cache['singularize'][$word])) {
  383. return self::$_cache['singularize'][$word];
  384. }
  385. if (!isset(self::$_singular['merged']['uninflected'])) {
  386. self::$_singular['merged']['uninflected'] = array_merge(
  387. self::$_singular['uninflected'],
  388. self::$_uninflected
  389. );
  390. }
  391. if (!isset(self::$_singular['merged']['irregular'])) {
  392. self::$_singular['merged']['irregular'] = array_merge(
  393. self::$_singular['irregular'],
  394. array_flip(self::$_plural['irregular'])
  395. );
  396. }
  397. if (!isset(self::$_singular['cacheUninflected']) || !isset(self::$_singular['cacheIrregular'])) {
  398. self::$_singular['cacheUninflected'] = '(?:' . implode('|', self::$_singular['merged']['uninflected']) . ')';
  399. self::$_singular['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_singular['merged']['irregular'])) . ')';
  400. }
  401. if (preg_match('/(.*)\\b(' . self::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
  402. self::$_cache['singularize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1);
  403. return self::$_cache['singularize'][$word];
  404. }
  405. if (preg_match('/^(' . self::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
  406. self::$_cache['singularize'][$word] = $word;
  407. return $word;
  408. }
  409. foreach (self::$_singular['rules'] as $rule => $replacement) {
  410. if (preg_match($rule, $word)) {
  411. self::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
  412. return self::$_cache['singularize'][$word];
  413. }
  414. }
  415. self::$_cache['singularize'][$word] = $word;
  416. return $word;
  417. }
  418. /**
  419. * Returns the given lower_case_and_underscored_word as a CamelCased word.
  420. *
  421. * @param string $lowerCaseAndUnderscoredWord Word to camelize
  422. * @return string Camelized word. LikeThis.
  423. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::camelize
  424. */
  425. public static function camelize($lowerCaseAndUnderscoredWord) {
  426. if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
  427. $result = str_replace(' ', '', Inflector::humanize($lowerCaseAndUnderscoredWord));
  428. self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
  429. }
  430. return $result;
  431. }
  432. /**
  433. * Returns the given camelCasedWord as an underscored_word.
  434. *
  435. * @param string $camelCasedWord Camel-cased word to be "underscorized"
  436. * @return string Underscore-syntaxed version of the $camelCasedWord
  437. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::underscore
  438. */
  439. public static function underscore($camelCasedWord) {
  440. if (!($result = self::_cache(__FUNCTION__, $camelCasedWord))) {
  441. $result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
  442. self::_cache(__FUNCTION__, $camelCasedWord, $result);
  443. }
  444. return $result;
  445. }
  446. /**
  447. * Returns the given underscored_word_group as a Human Readable Word Group.
  448. * (Underscores are replaced by spaces and capitalized following words.)
  449. *
  450. * @param string $lowerCaseAndUnderscoredWord String to be made more readable
  451. * @return string Human-readable string
  452. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::humanize
  453. */
  454. public static function humanize($lowerCaseAndUnderscoredWord) {
  455. if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
  456. $result = ucwords(str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
  457. self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
  458. }
  459. return $result;
  460. }
  461. /**
  462. * Returns corresponding table name for given model $className. ("people" for the model class "Person").
  463. *
  464. * @param string $className Name of class to get database table name for
  465. * @return string Name of the database table for given class
  466. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::tableize
  467. */
  468. public static function tableize($className) {
  469. if (!($result = self::_cache(__FUNCTION__, $className))) {
  470. $result = Inflector::pluralize(Inflector::underscore($className));
  471. self::_cache(__FUNCTION__, $className, $result);
  472. }
  473. return $result;
  474. }
  475. /**
  476. * Returns Cake model class name ("Person" for the database table "people".) for given database table.
  477. *
  478. * @param string $tableName Name of database table to get class name for
  479. * @return string Class name
  480. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::classify
  481. */
  482. public static function classify($tableName) {
  483. if (!($result = self::_cache(__FUNCTION__, $tableName))) {
  484. $result = Inflector::camelize(Inflector::singularize($tableName));
  485. self::_cache(__FUNCTION__, $tableName, $result);
  486. }
  487. return $result;
  488. }
  489. /**
  490. * Returns camelBacked version of an underscored string.
  491. *
  492. * @param string $string
  493. * @return string in variable form
  494. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::variable
  495. */
  496. public static function variable($string) {
  497. if (!($result = self::_cache(__FUNCTION__, $string))) {
  498. $camelized = Inflector::camelize(Inflector::underscore($string));
  499. $replace = strtolower(substr($camelized, 0, 1));
  500. $result = preg_replace('/\\w/', $replace, $camelized, 1);
  501. self::_cache(__FUNCTION__, $string, $result);
  502. }
  503. return $result;
  504. }
  505. /**
  506. * Returns a string with all spaces converted to underscores (by default), accented
  507. * characters converted to non-accented characters, and non word characters removed.
  508. *
  509. * @param string $string the string you want to slug
  510. * @param string $replacement will replace keys in map
  511. * @return string
  512. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::slug
  513. */
  514. public static function slug($string, $replacement = '_') {
  515. $quotedReplacement = preg_quote($replacement, '/');
  516. $merge = array(
  517. '/[^\s\p{Zs}\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
  518. '/[\s\p{Zs}]+/mu' => $replacement,
  519. sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
  520. );
  521. $map = self::$_transliteration + $merge;
  522. return preg_replace(array_keys($map), array_values($map), $string);
  523. }
  524. }
  525. // Store the initial state
  526. Inflector::reset();