Inflector.php 17 KB

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