Inflector.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. * @since CakePHP(tm) v 0.2.9
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Utility;
  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. * @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|([lre])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. 'brief' => 'briefs',
  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. 'potato' => 'potatoes',
  93. 'hero' => 'heroes',
  94. 'tooth' => 'teeth',
  95. 'goose' => 'geese',
  96. 'foot' => 'feet'
  97. )
  98. );
  99. /**
  100. * Singular inflector rules
  101. *
  102. * @var array
  103. */
  104. protected static $_singular = array(
  105. 'rules' => array(
  106. '/(s)tatuses$/i' => '\1\2tatus',
  107. '/^(.*)(menu)s$/i' => '\1\2',
  108. '/(quiz)zes$/i' => '\\1',
  109. '/(matr)ices$/i' => '\1ix',
  110. '/(vert|ind)ices$/i' => '\1ex',
  111. '/^(ox)en/i' => '\1',
  112. '/(alias)(es)*$/i' => '\1',
  113. '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
  114. '/([ftw]ax)es/i' => '\1',
  115. '/(cris|ax|test)es$/i' => '\1is',
  116. '/(shoe|slave)s$/i' => '\1',
  117. '/(o)es$/i' => '\1',
  118. '/ouses$/' => 'ouse',
  119. '/([^a])uses$/' => '\1us',
  120. '/([m|l])ice$/i' => '\1ouse',
  121. '/(x|ch|ss|sh)es$/i' => '\1',
  122. '/(m)ovies$/i' => '\1\2ovie',
  123. '/(s)eries$/i' => '\1\2eries',
  124. '/([^aeiouy]|qu)ies$/i' => '\1y',
  125. '/(tive)s$/i' => '\1',
  126. '/(hive)s$/i' => '\1',
  127. '/(drive)s$/i' => '\1',
  128. '/([lre])ves$/i' => '\1f',
  129. '/([^fo])ves$/i' => '\1fe',
  130. '/(^analy)ses$/i' => '\1sis',
  131. '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
  132. '/([ti])a$/i' => '\1um',
  133. '/(p)eople$/i' => '\1\2erson',
  134. '/(m)en$/i' => '\1an',
  135. '/(c)hildren$/i' => '\1\2hild',
  136. '/(n)ews$/i' => '\1\2ews',
  137. '/eaus$/' => 'eau',
  138. '/^(.*us)$/' => '\\1',
  139. '/s$/i' => ''
  140. ),
  141. 'uninflected' => array(
  142. '.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss'
  143. ),
  144. 'irregular' => array(
  145. 'foes' => 'foe',
  146. 'waves' => 'wave',
  147. 'curves' => 'curve'
  148. )
  149. );
  150. /**
  151. * Words that should not be inflected
  152. *
  153. * @var array
  154. */
  155. protected static $_uninflected = array(
  156. 'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus',
  157. 'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps',
  158. 'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder',
  159. 'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
  160. 'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings',
  161. 'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', '.*?media',
  162. 'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
  163. 'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
  164. 'proceedings', 'rabies', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
  165. 'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
  166. 'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
  167. 'Yengeese'
  168. );
  169. /**
  170. * Default map of accented and special characters to ASCII characters
  171. *
  172. * @var array
  173. */
  174. protected static $_transliteration = array(
  175. 'ä' => 'ae',
  176. 'æ' => 'ae',
  177. 'ǽ' => 'ae',
  178. 'ö' => 'oe',
  179. 'œ' => 'oe',
  180. 'ü' => 'ue',
  181. 'Ä' => 'Ae',
  182. 'Ü' => 'Ue',
  183. 'Ö' => 'Oe',
  184. 'À' => 'A',
  185. 'Á' => 'A',
  186. 'Â' => 'A',
  187. 'Ã' => 'A',
  188. 'Å' => 'A',
  189. 'Ǻ' => 'A',
  190. 'Ā' => 'A',
  191. 'Ă' => 'A',
  192. 'Ą' => 'A',
  193. 'Ǎ' => 'A',
  194. 'à' => 'a',
  195. 'á' => 'a',
  196. 'â' => 'a',
  197. 'ã' => 'a',
  198. 'å' => 'a',
  199. 'ǻ' => 'a',
  200. 'ā' => 'a',
  201. 'ă' => 'a',
  202. 'ą' => 'a',
  203. 'ǎ' => 'a',
  204. 'ª' => 'a',
  205. 'Ç' => 'C',
  206. 'Ć' => 'C',
  207. 'Ĉ' => 'C',
  208. 'Ċ' => 'C',
  209. 'Č' => 'C',
  210. 'ç' => 'c',
  211. 'ć' => 'c',
  212. 'ĉ' => 'c',
  213. 'ċ' => 'c',
  214. 'č' => 'c',
  215. 'Ð' => 'D',
  216. 'Ď' => 'D',
  217. 'Đ' => 'D',
  218. 'ð' => 'd',
  219. 'ď' => 'd',
  220. 'đ' => 'd',
  221. 'È' => 'E',
  222. 'É' => 'E',
  223. 'Ê' => 'E',
  224. 'Ë' => 'E',
  225. 'Ē' => 'E',
  226. 'Ĕ' => 'E',
  227. 'Ė' => 'E',
  228. 'Ę' => 'E',
  229. 'Ě' => 'E',
  230. 'è' => 'e',
  231. 'é' => 'e',
  232. 'ê' => 'e',
  233. 'ë' => 'e',
  234. 'ē' => 'e',
  235. 'ĕ' => 'e',
  236. 'ė' => 'e',
  237. 'ę' => 'e',
  238. 'ě' => 'e',
  239. 'Ĝ' => 'G',
  240. 'Ğ' => 'G',
  241. 'Ġ' => 'G',
  242. 'Ģ' => 'G',
  243. 'ĝ' => 'g',
  244. 'ğ' => 'g',
  245. 'ġ' => 'g',
  246. 'ģ' => 'g',
  247. 'Ĥ' => 'H',
  248. 'Ħ' => 'H',
  249. 'ĥ' => 'h',
  250. 'ħ' => 'h',
  251. 'Ì' => 'I',
  252. 'Í' => 'I',
  253. 'Î' => 'I',
  254. 'Ï' => 'I',
  255. 'Ĩ' => 'I',
  256. 'Ī' => 'I',
  257. 'Ĭ' => 'I',
  258. 'Ǐ' => 'I',
  259. 'Į' => 'I',
  260. 'İ' => 'I',
  261. 'ì' => 'i',
  262. 'í' => 'i',
  263. 'î' => 'i',
  264. 'ï' => 'i',
  265. 'ĩ' => 'i',
  266. 'ī' => 'i',
  267. 'ĭ' => 'i',
  268. 'ǐ' => 'i',
  269. 'į' => 'i',
  270. 'ı' => 'i',
  271. 'Ĵ' => 'J',
  272. 'ĵ' => 'j',
  273. 'Ķ' => 'K',
  274. 'ķ' => 'k',
  275. 'Ĺ' => 'L',
  276. 'Ļ' => 'L',
  277. 'Ľ' => 'L',
  278. 'Ŀ' => 'L',
  279. 'Ł' => 'L',
  280. 'ĺ' => 'l',
  281. 'ļ' => 'l',
  282. 'ľ' => 'l',
  283. 'ŀ' => 'l',
  284. 'ł' => 'l',
  285. 'Ñ' => 'N',
  286. 'Ń' => 'N',
  287. 'Ņ' => 'N',
  288. 'Ň' => 'N',
  289. 'ñ' => 'n',
  290. 'ń' => 'n',
  291. 'ņ' => 'n',
  292. 'ň' => 'n',
  293. 'ʼn' => 'n',
  294. 'Ò' => 'O',
  295. 'Ó' => 'O',
  296. 'Ô' => 'O',
  297. 'Õ' => 'O',
  298. 'Ō' => 'O',
  299. 'Ŏ' => 'O',
  300. 'Ǒ' => 'O',
  301. 'Ő' => 'O',
  302. 'Ơ' => 'O',
  303. 'Ø' => 'O',
  304. 'Ǿ' => 'O',
  305. 'ò' => 'o',
  306. 'ó' => 'o',
  307. 'ô' => 'o',
  308. 'õ' => 'o',
  309. 'ō' => 'o',
  310. 'ŏ' => 'o',
  311. 'ǒ' => 'o',
  312. 'ő' => 'o',
  313. 'ơ' => 'o',
  314. 'ø' => 'o',
  315. 'ǿ' => 'o',
  316. 'º' => 'o',
  317. 'Ŕ' => 'R',
  318. 'Ŗ' => 'R',
  319. 'Ř' => 'R',
  320. 'ŕ' => 'r',
  321. 'ŗ' => 'r',
  322. 'ř' => 'r',
  323. 'Ś' => 'S',
  324. 'Ŝ' => 'S',
  325. 'Ş' => 'S',
  326. 'Ș' => 'S',
  327. 'Š' => 'S',
  328. 'ś' => 's',
  329. 'ŝ' => 's',
  330. 'ş' => 's',
  331. 'ș' => 's',
  332. 'š' => 's',
  333. 'ſ' => 's',
  334. 'Ţ' => 'T',
  335. 'Ț' => 'T',
  336. 'Ť' => 'T',
  337. 'Ŧ' => 'T',
  338. 'ţ' => 't',
  339. 'ț' => 't',
  340. 'ť' => 't',
  341. 'ŧ' => 't',
  342. 'Ù' => 'U',
  343. 'Ú' => 'U',
  344. 'Û' => 'U',
  345. 'Ũ' => 'U',
  346. 'Ū' => 'U',
  347. 'Ŭ' => 'U',
  348. 'Ů' => 'U',
  349. 'Ű' => 'U',
  350. 'Ų' => 'U',
  351. 'Ư' => 'U',
  352. 'Ǔ' => 'U',
  353. 'Ǖ' => 'U',
  354. 'Ǘ' => 'U',
  355. 'Ǚ' => 'U',
  356. 'Ǜ' => 'U',
  357. 'ù' => 'u',
  358. 'ú' => 'u',
  359. 'û' => 'u',
  360. 'ũ' => 'u',
  361. 'ū' => 'u',
  362. 'ŭ' => 'u',
  363. 'ů' => 'u',
  364. 'ű' => 'u',
  365. 'ų' => 'u',
  366. 'ư' => 'u',
  367. 'ǔ' => 'u',
  368. 'ǖ' => 'u',
  369. 'ǘ' => 'u',
  370. 'ǚ' => 'u',
  371. 'ǜ' => 'u',
  372. 'Ý' => 'Y',
  373. 'Ÿ' => 'Y',
  374. 'Ŷ' => 'Y',
  375. 'ý' => 'y',
  376. 'ÿ' => 'y',
  377. 'ŷ' => 'y',
  378. 'Ŵ' => 'W',
  379. 'ŵ' => 'w',
  380. 'Ź' => 'Z',
  381. 'Ż' => 'Z',
  382. 'Ž' => 'Z',
  383. 'ź' => 'z',
  384. 'ż' => 'z',
  385. 'ž' => 'z',
  386. 'Æ' => 'AE',
  387. 'Ǽ' => 'AE',
  388. 'ß' => 'ss',
  389. 'IJ' => 'IJ',
  390. 'ij' => 'ij',
  391. 'Œ' => 'OE',
  392. 'ƒ' => 'f',
  393. );
  394. /**
  395. * Method cache array.
  396. *
  397. * @var array
  398. */
  399. protected static $_cache = array();
  400. /**
  401. * The initial state of Inflector so reset() works.
  402. *
  403. * @var array
  404. */
  405. protected static $_initialState = array();
  406. /**
  407. * Cache inflected values, and return if already available
  408. *
  409. * @param string $type Inflection type
  410. * @param string $key Original value
  411. * @param string $value Inflected value
  412. * @return string Inflected value, from cache
  413. */
  414. protected static function _cache($type, $key, $value = false) {
  415. $key = '_' . $key;
  416. $type = '_' . $type;
  417. if ($value !== false) {
  418. static::$_cache[$type][$key] = $value;
  419. return $value;
  420. }
  421. if (!isset(static::$_cache[$type][$key])) {
  422. return false;
  423. }
  424. return static::$_cache[$type][$key];
  425. }
  426. /**
  427. * Clears Inflectors inflected value caches. And resets the inflection
  428. * rules to the initial values.
  429. *
  430. * @return void
  431. */
  432. public static function reset() {
  433. if (empty(static::$_initialState)) {
  434. static::$_initialState = get_class_vars(__CLASS__);
  435. return;
  436. }
  437. foreach (static::$_initialState as $key => $val) {
  438. if ($key !== '_initialState') {
  439. static::${$key} = $val;
  440. }
  441. }
  442. }
  443. /**
  444. * Adds custom inflection $rules, of either 'plural', 'singular' or 'transliteration' $type.
  445. *
  446. * ### Usage:
  447. *
  448. * {{{
  449. * Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
  450. * Inflector::rules('plural', array(
  451. * 'rules' => array('/^(inflect)ors$/i' => '\1ables'),
  452. * 'uninflected' => array('dontinflectme'),
  453. * 'irregular' => array('red' => 'redlings')
  454. * ));
  455. * Inflector::rules('transliteration', array('/å/' => 'aa'));
  456. * }}}
  457. *
  458. * @param string $type The type of inflection, either 'plural', 'singular' or 'transliteration'
  459. * @param array $rules Array of rules to be added.
  460. * @param boolean $reset If true, will unset default inflections for all
  461. * new rules that are being defined in $rules.
  462. * @return void
  463. */
  464. public static function rules($type, $rules, $reset = false) {
  465. $var = '_' . $type;
  466. switch ($type) {
  467. case 'transliteration':
  468. if ($reset) {
  469. static::$_transliteration = $rules;
  470. } else {
  471. static::$_transliteration = $rules + static::$_transliteration;
  472. }
  473. break;
  474. default:
  475. foreach ($rules as $rule => $pattern) {
  476. if (is_array($pattern)) {
  477. if ($reset) {
  478. static::${$var}[$rule] = $pattern;
  479. } else {
  480. if ($rule === 'uninflected') {
  481. static::${$var}[$rule] = array_merge($pattern, static::${$var}[$rule]);
  482. } else {
  483. static::${$var}[$rule] = $pattern + static::${$var}[$rule];
  484. }
  485. }
  486. unset($rules[$rule], static::${$var}['cache' . ucfirst($rule)]);
  487. if (isset(static::${$var}['merged'][$rule])) {
  488. unset(static::${$var}['merged'][$rule]);
  489. }
  490. if ($type === 'plural') {
  491. static::$_cache['pluralize'] = static::$_cache['tableize'] = array();
  492. } elseif ($type === 'singular') {
  493. static::$_cache['singularize'] = array();
  494. }
  495. }
  496. }
  497. static::${$var}['rules'] = $rules + static::${$var}['rules'];
  498. }
  499. }
  500. /**
  501. * Return $word in plural form.
  502. *
  503. * @param string $word Word in singular
  504. * @return string Word in plural
  505. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::pluralize
  506. */
  507. public static function pluralize($word) {
  508. if (isset(static::$_cache['pluralize'][$word])) {
  509. return static::$_cache['pluralize'][$word];
  510. }
  511. if (!isset(static::$_plural['merged']['irregular'])) {
  512. static::$_plural['merged']['irregular'] = static::$_plural['irregular'];
  513. }
  514. if (!isset(static::$_plural['merged']['uninflected'])) {
  515. static::$_plural['merged']['uninflected'] = array_merge(static::$_plural['uninflected'], static::$_uninflected);
  516. }
  517. if (!isset(static::$_plural['cacheUninflected']) || !isset(static::$_plural['cacheIrregular'])) {
  518. static::$_plural['cacheUninflected'] = '(?:' . implode('|', static::$_plural['merged']['uninflected']) . ')';
  519. static::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(static::$_plural['merged']['irregular'])) . ')';
  520. }
  521. if (preg_match('/(.*)\\b(' . static::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) {
  522. static::$_cache['pluralize'][$word] = $regs[1] . substr($word, 0, 1) . substr(static::$_plural['merged']['irregular'][strtolower($regs[2])], 1);
  523. return static::$_cache['pluralize'][$word];
  524. }
  525. if (preg_match('/^(' . static::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
  526. static::$_cache['pluralize'][$word] = $word;
  527. return $word;
  528. }
  529. foreach (static::$_plural['rules'] as $rule => $replacement) {
  530. if (preg_match($rule, $word)) {
  531. static::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
  532. return static::$_cache['pluralize'][$word];
  533. }
  534. }
  535. }
  536. /**
  537. * Return $word in singular form.
  538. *
  539. * @param string $word Word in plural
  540. * @return string Word in singular
  541. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::singularize
  542. */
  543. public static function singularize($word) {
  544. if (isset(static::$_cache['singularize'][$word])) {
  545. return static::$_cache['singularize'][$word];
  546. }
  547. if (!isset(static::$_singular['merged']['uninflected'])) {
  548. static::$_singular['merged']['uninflected'] = array_merge(
  549. static::$_singular['uninflected'],
  550. static::$_uninflected
  551. );
  552. }
  553. if (!isset(static::$_singular['merged']['irregular'])) {
  554. static::$_singular['merged']['irregular'] = array_merge(
  555. static::$_singular['irregular'],
  556. array_flip(static::$_plural['irregular'])
  557. );
  558. }
  559. if (!isset(static::$_singular['cacheUninflected']) || !isset(static::$_singular['cacheIrregular'])) {
  560. static::$_singular['cacheUninflected'] = '(?:' . implode('|', static::$_singular['merged']['uninflected']) . ')';
  561. static::$_singular['cacheIrregular'] = '(?:' . implode('|', array_keys(static::$_singular['merged']['irregular'])) . ')';
  562. }
  563. if (preg_match('/(.*)\\b(' . static::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
  564. static::$_cache['singularize'][$word] = $regs[1] . substr($word, 0, 1) . substr(static::$_singular['merged']['irregular'][strtolower($regs[2])], 1);
  565. return static::$_cache['singularize'][$word];
  566. }
  567. if (preg_match('/^(' . static::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
  568. static::$_cache['singularize'][$word] = $word;
  569. return $word;
  570. }
  571. foreach (static::$_singular['rules'] as $rule => $replacement) {
  572. if (preg_match($rule, $word)) {
  573. static::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
  574. return static::$_cache['singularize'][$word];
  575. }
  576. }
  577. static::$_cache['singularize'][$word] = $word;
  578. return $word;
  579. }
  580. /**
  581. * Returns the given lower_case_and_underscored_word as a CamelCased word.
  582. *
  583. * @param string $lowerCaseAndUnderscoredWord Word to camelize
  584. * @return string Camelized word. LikeThis.
  585. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::camelize
  586. */
  587. public static function camelize($lowerCaseAndUnderscoredWord) {
  588. if (!($result = static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
  589. $result = str_replace(' ', '', Inflector::humanize($lowerCaseAndUnderscoredWord));
  590. static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
  591. }
  592. return $result;
  593. }
  594. /**
  595. * Returns the given camelCasedWord as an underscored_word.
  596. *
  597. * @param string $camelCasedWord Camel-cased word to be "underscorized"
  598. * @return string Underscore-syntaxed version of the $camelCasedWord
  599. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::underscore
  600. */
  601. public static function underscore($camelCasedWord) {
  602. if (!($result = static::_cache(__FUNCTION__, $camelCasedWord))) {
  603. $result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
  604. static::_cache(__FUNCTION__, $camelCasedWord, $result);
  605. }
  606. return $result;
  607. }
  608. /**
  609. * Returns the given underscored_word_group as a Human Readable Word Group.
  610. * (Underscores are replaced by spaces and capitalized following words.)
  611. *
  612. * @param string $lowerCaseAndUnderscoredWord String to be made more readable
  613. * @return string Human-readable string
  614. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::humanize
  615. */
  616. public static function humanize($lowerCaseAndUnderscoredWord) {
  617. if (!($result = static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
  618. $result = ucwords(str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
  619. static::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
  620. }
  621. return $result;
  622. }
  623. /**
  624. * Returns corresponding table name for given model $className. ("people" for the model class "Person").
  625. *
  626. * @param string $className Name of class to get database table name for
  627. * @return string Name of the database table for given class
  628. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::tableize
  629. */
  630. public static function tableize($className) {
  631. if (!($result = static::_cache(__FUNCTION__, $className))) {
  632. $result = Inflector::pluralize(Inflector::underscore($className));
  633. static::_cache(__FUNCTION__, $className, $result);
  634. }
  635. return $result;
  636. }
  637. /**
  638. * Returns Cake model class name ("Person" for the database table "people".) for given database table.
  639. *
  640. * @param string $tableName Name of database table to get class name for
  641. * @return string Class name
  642. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::classify
  643. */
  644. public static function classify($tableName) {
  645. if (!($result = static::_cache(__FUNCTION__, $tableName))) {
  646. $result = Inflector::camelize(Inflector::singularize($tableName));
  647. static::_cache(__FUNCTION__, $tableName, $result);
  648. }
  649. return $result;
  650. }
  651. /**
  652. * Returns camelBacked version of an underscored string.
  653. *
  654. * @param string $string
  655. * @return string in variable form
  656. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::variable
  657. */
  658. public static function variable($string) {
  659. if (!($result = static::_cache(__FUNCTION__, $string))) {
  660. $camelized = Inflector::camelize(Inflector::underscore($string));
  661. $replace = strtolower(substr($camelized, 0, 1));
  662. $result = preg_replace('/\\w/', $replace, $camelized, 1);
  663. static::_cache(__FUNCTION__, $string, $result);
  664. }
  665. return $result;
  666. }
  667. /**
  668. * Returns a string with all spaces converted to underscores (by default), accented
  669. * characters converted to non-accented characters, and non word characters removed.
  670. *
  671. * @param string $string the string you want to slug
  672. * @param string $replacement will replace keys in map
  673. * @return string
  674. * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::slug
  675. */
  676. public static function slug($string, $replacement = '_') {
  677. $quotedReplacement = preg_quote($replacement, '/');
  678. $map = array(
  679. '/[^\s\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
  680. '/\\s+/' => $replacement,
  681. sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
  682. );
  683. $string = str_replace(array_keys(static::$_transliteration), array_values(static::$_transliteration), $string);
  684. return preg_replace(array_keys($map), array_values($map), $string);
  685. }
  686. }
  687. // Store the initial state
  688. Inflector::reset();