Base for a static organization website

Inflector.php 18KB

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