Inflector.php 18 KB

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