Inflector.php 17 KB

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