Inflector.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  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 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/3.0/en/core-libraries/inflector.html
  23. */
  24. class Inflector
  25. {
  26. /**
  27. * Plural inflector rules
  28. *
  29. * @var array
  30. */
  31. protected static $_plural = [
  32. '/(s)tatus$/i' => '\1tatuses',
  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. '/(chef)$/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. /**
  57. * Singular inflector rules
  58. *
  59. * @var array
  60. */
  61. protected static $_singular = [
  62. '/(s)tatuses$/i' => '\1\2tatus',
  63. '/^(.*)(menu)s$/i' => '\1\2',
  64. '/(quiz)zes$/i' => '\\1',
  65. '/(matr)ices$/i' => '\1ix',
  66. '/(vert|ind)ices$/i' => '\1ex',
  67. '/^(ox)en/i' => '\1',
  68. '/(alias)(es)*$/i' => '\1',
  69. '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
  70. '/([ftw]ax)es/i' => '\1',
  71. '/(cris|ax|test)es$/i' => '\1is',
  72. '/(shoe)s$/i' => '\1',
  73. '/(o)es$/i' => '\1',
  74. '/ouses$/' => 'ouse',
  75. '/([^a])uses$/' => '\1us',
  76. '/([m|l])ice$/i' => '\1ouse',
  77. '/(x|ch|ss|sh)es$/i' => '\1',
  78. '/(m)ovies$/i' => '\1\2ovie',
  79. '/(s)eries$/i' => '\1\2eries',
  80. '/([^aeiouy]|qu)ies$/i' => '\1y',
  81. '/(tive)s$/i' => '\1',
  82. '/(hive)s$/i' => '\1',
  83. '/(drive)s$/i' => '\1',
  84. '/([le])ves$/i' => '\1f',
  85. '/([^rfoa])ves$/i' => '\1fe',
  86. '/(^analy)ses$/i' => '\1sis',
  87. '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
  88. '/([ti])a$/i' => '\1um',
  89. '/(p)eople$/i' => '\1\2erson',
  90. '/(m)en$/i' => '\1an',
  91. '/(c)hildren$/i' => '\1\2hild',
  92. '/(n)ews$/i' => '\1\2ews',
  93. '/eaus$/' => 'eau',
  94. '/^(.*us)$/' => '\\1',
  95. '/s$/i' => ''
  96. ];
  97. /**
  98. * Irregular rules
  99. *
  100. * @var array
  101. */
  102. protected static $_irregular = [
  103. 'atlas' => 'atlases',
  104. 'beef' => 'beefs',
  105. 'brief' => 'briefs',
  106. 'brother' => 'brothers',
  107. 'cafe' => 'cafes',
  108. 'child' => 'children',
  109. 'cookie' => 'cookies',
  110. 'corpus' => 'corpuses',
  111. 'cow' => 'cows',
  112. 'criterion' => 'criteria',
  113. 'ganglion' => 'ganglions',
  114. 'genie' => 'genies',
  115. 'genus' => 'genera',
  116. 'graffito' => 'graffiti',
  117. 'hoof' => 'hoofs',
  118. 'loaf' => 'loaves',
  119. 'man' => 'men',
  120. 'money' => 'monies',
  121. 'mongoose' => 'mongooses',
  122. 'move' => 'moves',
  123. 'mythos' => 'mythoi',
  124. 'niche' => 'niches',
  125. 'numen' => 'numina',
  126. 'occiput' => 'occiputs',
  127. 'octopus' => 'octopuses',
  128. 'opus' => 'opuses',
  129. 'ox' => 'oxen',
  130. 'penis' => 'penises',
  131. 'person' => 'people',
  132. 'sex' => 'sexes',
  133. 'soliloquy' => 'soliloquies',
  134. 'testis' => 'testes',
  135. 'trilby' => 'trilbys',
  136. 'turf' => 'turfs',
  137. 'potato' => 'potatoes',
  138. 'hero' => 'heroes',
  139. 'tooth' => 'teeth',
  140. 'goose' => 'geese',
  141. 'foot' => 'feet',
  142. 'foe' => 'foes',
  143. 'sieve' => 'sieves'
  144. ];
  145. /**
  146. * Words that should not be inflected
  147. *
  148. * @var array
  149. */
  150. protected static $_uninflected = [
  151. '.*[nrlm]ese', '.*data', '.*deer', '.*fish', '.*measles', '.*ois',
  152. '.*pox', '.*sheep', 'people', 'feedback', 'stadia', '.*?media',
  153. 'chassis', 'clippers', 'debris', 'diabetes', 'equipment', 'gallows',
  154. 'graffiti', 'headquarters', 'information', 'innings', 'news', 'nexus',
  155. 'proceedings', 'research', 'sea[- ]bass', 'series', 'species', 'weather'
  156. ];
  157. /**
  158. * Default map of accented and special characters to ASCII characters
  159. *
  160. * @var array
  161. */
  162. protected static $_transliteration = [
  163. 'ä' => 'ae',
  164. 'æ' => 'ae',
  165. 'ǽ' => 'ae',
  166. 'ö' => 'oe',
  167. 'œ' => 'oe',
  168. 'ü' => 'ue',
  169. 'Ä' => 'Ae',
  170. 'Ü' => 'Ue',
  171. 'Ö' => 'Oe',
  172. 'À' => 'A',
  173. 'Á' => 'A',
  174. 'Â' => 'A',
  175. 'Ã' => 'A',
  176. 'Å' => 'A',
  177. 'Ǻ' => 'A',
  178. 'Ā' => 'A',
  179. 'Ă' => 'A',
  180. 'Ą' => 'A',
  181. 'Ǎ' => 'A',
  182. 'à' => 'a',
  183. 'á' => 'a',
  184. 'â' => 'a',
  185. 'ã' => 'a',
  186. 'å' => 'a',
  187. 'ǻ' => 'a',
  188. 'ā' => 'a',
  189. 'ă' => 'a',
  190. 'ą' => 'a',
  191. 'ǎ' => 'a',
  192. 'ª' => 'a',
  193. 'Ç' => 'C',
  194. 'Ć' => 'C',
  195. 'Ĉ' => 'C',
  196. 'Ċ' => 'C',
  197. 'Č' => 'C',
  198. 'ç' => 'c',
  199. 'ć' => 'c',
  200. 'ĉ' => 'c',
  201. 'ċ' => 'c',
  202. 'č' => 'c',
  203. 'Ð' => 'D',
  204. 'Ď' => 'D',
  205. 'Đ' => 'D',
  206. 'ð' => 'd',
  207. 'ď' => 'd',
  208. 'đ' => 'd',
  209. 'È' => 'E',
  210. 'É' => 'E',
  211. 'Ê' => 'E',
  212. 'Ë' => 'E',
  213. 'Ē' => 'E',
  214. 'Ĕ' => 'E',
  215. 'Ė' => 'E',
  216. 'Ę' => 'E',
  217. 'Ě' => 'E',
  218. 'è' => 'e',
  219. 'é' => 'e',
  220. 'ê' => 'e',
  221. 'ë' => 'e',
  222. 'ē' => 'e',
  223. 'ĕ' => 'e',
  224. 'ė' => 'e',
  225. 'ę' => 'e',
  226. 'ě' => 'e',
  227. 'Ĝ' => 'G',
  228. 'Ğ' => 'G',
  229. 'Ġ' => 'G',
  230. 'Ģ' => 'G',
  231. 'Ґ' => 'G',
  232. 'ĝ' => 'g',
  233. 'ğ' => 'g',
  234. 'ġ' => 'g',
  235. 'ģ' => 'g',
  236. 'ґ' => 'g',
  237. 'Ĥ' => 'H',
  238. 'Ħ' => 'H',
  239. 'ĥ' => 'h',
  240. 'ħ' => 'h',
  241. 'І' => 'I',
  242. 'Ì' => 'I',
  243. 'Í' => 'I',
  244. 'Î' => 'I',
  245. 'Ї' => 'Yi',
  246. 'Ï' => 'I',
  247. 'Ĩ' => 'I',
  248. 'Ī' => 'I',
  249. 'Ĭ' => 'I',
  250. 'Ǐ' => 'I',
  251. 'Į' => 'I',
  252. 'İ' => 'I',
  253. 'і' => 'i',
  254. 'ì' => 'i',
  255. 'í' => 'i',
  256. 'î' => 'i',
  257. 'ï' => 'i',
  258. 'ї' => 'yi',
  259. 'ĩ' => 'i',
  260. 'ī' => 'i',
  261. 'ĭ' => 'i',
  262. 'ǐ' => 'i',
  263. 'į' => 'i',
  264. 'ı' => 'i',
  265. 'Ĵ' => 'J',
  266. 'ĵ' => 'j',
  267. 'Ķ' => 'K',
  268. 'ķ' => 'k',
  269. 'Ĺ' => 'L',
  270. 'Ļ' => 'L',
  271. 'Ľ' => 'L',
  272. 'Ŀ' => 'L',
  273. 'Ł' => 'L',
  274. 'ĺ' => 'l',
  275. 'ļ' => 'l',
  276. 'ľ' => 'l',
  277. 'ŀ' => 'l',
  278. 'ł' => 'l',
  279. 'Ñ' => 'N',
  280. 'Ń' => 'N',
  281. 'Ņ' => 'N',
  282. 'Ň' => 'N',
  283. 'ñ' => 'n',
  284. 'ń' => 'n',
  285. 'ņ' => 'n',
  286. 'ň' => 'n',
  287. 'ʼn' => 'n',
  288. 'Ò' => 'O',
  289. 'Ó' => 'O',
  290. 'Ô' => 'O',
  291. 'Õ' => 'O',
  292. 'Ō' => 'O',
  293. 'Ŏ' => 'O',
  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. 'Ŕ' => 'R',
  312. 'Ŗ' => 'R',
  313. 'Ř' => 'R',
  314. 'ŕ' => 'r',
  315. 'ŗ' => 'r',
  316. 'ř' => 'r',
  317. 'Ś' => 'S',
  318. 'Ŝ' => 'S',
  319. 'Ş' => 'S',
  320. 'Ș' => 'S',
  321. 'Š' => 'S',
  322. 'ẞ' => 'SS',
  323. 'ś' => 's',
  324. 'ŝ' => 's',
  325. 'ş' => 's',
  326. 'ș' => 's',
  327. 'š' => 's',
  328. 'ſ' => 's',
  329. 'Ţ' => 'T',
  330. 'Ț' => 'T',
  331. 'Ť' => 'T',
  332. 'Ŧ' => 'T',
  333. 'ţ' => 't',
  334. 'ț' => 't',
  335. 'ť' => 't',
  336. 'ŧ' => 't',
  337. 'Ù' => 'U',
  338. 'Ú' => 'U',
  339. 'Û' => 'U',
  340. 'Ũ' => 'U',
  341. 'Ū' => 'U',
  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. 'Ý' => 'Y',
  368. 'Ÿ' => 'Y',
  369. 'Ŷ' => 'Y',
  370. 'ý' => 'y',
  371. 'ÿ' => 'y',
  372. 'ŷ' => 'y',
  373. 'Ŵ' => 'W',
  374. 'ŵ' => 'w',
  375. 'Ź' => 'Z',
  376. 'Ż' => 'Z',
  377. 'Ž' => 'Z',
  378. 'ź' => 'z',
  379. 'ż' => 'z',
  380. 'ž' => 'z',
  381. 'Æ' => 'AE',
  382. 'Ǽ' => 'AE',
  383. 'ß' => 'ss',
  384. 'IJ' => 'IJ',
  385. 'ij' => 'ij',
  386. 'Œ' => 'OE',
  387. 'ƒ' => 'f',
  388. 'Þ' => 'TH',
  389. 'þ' => 'th',
  390. 'Є' => 'Ye',
  391. 'є' => 'ye',
  392. ];
  393. /**
  394. * Method cache array.
  395. *
  396. * @var array
  397. */
  398. protected static $_cache = [];
  399. /**
  400. * The initial state of Inflector so reset() works.
  401. *
  402. * @var array
  403. */
  404. protected static $_initialState = [];
  405. /**
  406. * Cache inflected values, and return if already available
  407. *
  408. * @param string $type Inflection type
  409. * @param string $key Original value
  410. * @param string|bool $value Inflected value
  411. * @return string|bool Inflected value on cache hit or false on cache miss.
  412. */
  413. protected static function _cache($type, $key, $value = false)
  414. {
  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. {
  434. if (empty(static::$_initialState)) {
  435. static::$_initialState = get_class_vars(__CLASS__);
  436. return;
  437. }
  438. foreach (static::$_initialState as $key => $val) {
  439. if ($key !== '_initialState') {
  440. static::${$key} = $val;
  441. }
  442. }
  443. }
  444. /**
  445. * Adds custom inflection $rules, of either 'plural', 'singular',
  446. * 'uninflected', 'irregular' or 'transliteration' $type.
  447. *
  448. * ### Usage:
  449. *
  450. * ```
  451. * Inflector::rules('plural', ['/^(inflect)or$/i' => '\1ables']);
  452. * Inflector::rules('irregular', ['red' => 'redlings']);
  453. * Inflector::rules('uninflected', ['dontinflectme']);
  454. * Inflector::rules('transliteration', ['/å/' => 'aa']);
  455. * ```
  456. *
  457. * @param string $type The type of inflection, either 'plural', 'singular',
  458. * 'uninflected' or 'transliteration'.
  459. * @param array $rules Array of rules to be added.
  460. * @param bool $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. {
  466. $var = '_' . $type;
  467. if ($reset) {
  468. static::${$var} = $rules;
  469. } elseif ($type === 'uninflected') {
  470. static::$_uninflected = array_merge(
  471. $rules,
  472. static::$_uninflected
  473. );
  474. } else {
  475. static::${$var} = $rules + static::${$var};
  476. }
  477. static::$_cache = [];
  478. }
  479. /**
  480. * Return $word in plural form.
  481. *
  482. * @param string $word Word in singular
  483. * @return string Word in plural
  484. * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-plural-singular-forms
  485. */
  486. public static function pluralize($word)
  487. {
  488. if (isset(static::$_cache['pluralize'][$word])) {
  489. return static::$_cache['pluralize'][$word];
  490. }
  491. if (!isset(static::$_cache['irregular']['pluralize'])) {
  492. static::$_cache['irregular']['pluralize'] = '(?:' . implode('|', array_keys(static::$_irregular)) . ')';
  493. }
  494. if (preg_match('/(.*?(?:\\b|_))(' . static::$_cache['irregular']['pluralize'] . ')$/i', $word, $regs)) {
  495. static::$_cache['pluralize'][$word] = $regs[1] . substr($regs[2], 0, 1) .
  496. substr(static::$_irregular[strtolower($regs[2])], 1);
  497. return static::$_cache['pluralize'][$word];
  498. }
  499. if (!isset(static::$_cache['uninflected'])) {
  500. static::$_cache['uninflected'] = '(?:' . implode('|', static::$_uninflected) . ')';
  501. }
  502. if (preg_match('/^(' . static::$_cache['uninflected'] . ')$/i', $word, $regs)) {
  503. static::$_cache['pluralize'][$word] = $word;
  504. return $word;
  505. }
  506. foreach (static::$_plural as $rule => $replacement) {
  507. if (preg_match($rule, $word)) {
  508. static::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
  509. return static::$_cache['pluralize'][$word];
  510. }
  511. }
  512. }
  513. /**
  514. * Return $word in singular form.
  515. *
  516. * @param string $word Word in plural
  517. * @return string Word in singular
  518. * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-plural-singular-forms
  519. */
  520. public static function singularize($word)
  521. {
  522. if (isset(static::$_cache['singularize'][$word])) {
  523. return static::$_cache['singularize'][$word];
  524. }
  525. if (!isset(static::$_cache['irregular']['singular'])) {
  526. static::$_cache['irregular']['singular'] = '(?:' . implode('|', static::$_irregular) . ')';
  527. }
  528. if (preg_match('/(.*?(?:\\b|_))(' . static::$_cache['irregular']['singular'] . ')$/i', $word, $regs)) {
  529. static::$_cache['singularize'][$word] = $regs[1] . substr($regs[2], 0, 1) .
  530. substr(array_search(strtolower($regs[2]), static::$_irregular), 1);
  531. return static::$_cache['singularize'][$word];
  532. }
  533. if (!isset(static::$_cache['uninflected'])) {
  534. static::$_cache['uninflected'] = '(?:' . implode('|', static::$_uninflected) . ')';
  535. }
  536. if (preg_match('/^(' . static::$_cache['uninflected'] . ')$/i', $word, $regs)) {
  537. static::$_cache['pluralize'][$word] = $word;
  538. return $word;
  539. }
  540. foreach (static::$_singular as $rule => $replacement) {
  541. if (preg_match($rule, $word)) {
  542. static::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
  543. return static::$_cache['singularize'][$word];
  544. }
  545. }
  546. static::$_cache['singularize'][$word] = $word;
  547. return $word;
  548. }
  549. /**
  550. * Returns the input lower_case_delimited_string as a CamelCasedString.
  551. *
  552. * @param string $string String to camelize
  553. * @param string $delimiter the delimiter in the input string
  554. * @return string CamelizedStringLikeThis.
  555. * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-camelcase-and-under-scored-forms
  556. */
  557. public static function camelize($string, $delimiter = '_')
  558. {
  559. $cacheKey = __FUNCTION__ . $delimiter;
  560. $result = static::_cache($cacheKey, $string);
  561. if ($result === false) {
  562. $result = str_replace(' ', '', static::humanize($string, $delimiter));
  563. static::_cache($cacheKey, $string, $result);
  564. }
  565. return $result;
  566. }
  567. /**
  568. * Returns the input CamelCasedString as an underscored_string.
  569. *
  570. * Also replaces dashes with underscores
  571. *
  572. * @param string $string CamelCasedString to be "underscorized"
  573. * @return string underscore_version of the input string
  574. * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-camelcase-and-under-scored-forms
  575. */
  576. public static function underscore($string)
  577. {
  578. return static::delimit(str_replace('-', '_', $string), '_');
  579. }
  580. /**
  581. * Returns the input CamelCasedString as an dashed-string.
  582. *
  583. * Also replaces underscores with dashes
  584. *
  585. * @param string $string The string to dasherize.
  586. * @return string Dashed version of the input string
  587. */
  588. public static function dasherize($string)
  589. {
  590. return static::delimit(str_replace('_', '-', $string), '-');
  591. }
  592. /**
  593. * Returns the input lower_case_delimited_string as 'A Human Readable String'.
  594. * (Underscores are replaced by spaces and capitalized following words.)
  595. *
  596. * @param string $string String to be humanized
  597. * @param string $delimiter the character to replace with a space
  598. * @return string Human-readable string
  599. * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-human-readable-forms
  600. */
  601. public static function humanize($string, $delimiter = '_')
  602. {
  603. $cacheKey = __FUNCTION__ . $delimiter;
  604. $result = static::_cache($cacheKey, $string);
  605. if ($result === false) {
  606. $result = explode(' ', str_replace($delimiter, ' ', $string));
  607. foreach ($result as &$word) {
  608. $word = mb_strtoupper(mb_substr($word, 0, 1)) . mb_substr($word, 1);
  609. }
  610. $result = implode(' ', $result);
  611. static::_cache($cacheKey, $string, $result);
  612. }
  613. return $result;
  614. }
  615. /**
  616. * Expects a CamelCasedInputString, and produces a lower_case_delimited_string
  617. *
  618. * @param string $string String to delimit
  619. * @param string $delimiter the character to use as a delimiter
  620. * @return string delimited string
  621. */
  622. public static function delimit($string, $delimiter = '_')
  623. {
  624. $cacheKey = __FUNCTION__ . $delimiter;
  625. $result = static::_cache($cacheKey, $string);
  626. if ($result === false) {
  627. $result = mb_strtolower(preg_replace('/(?<=\\w)([A-Z])/', $delimiter . '\\1', $string));
  628. static::_cache($cacheKey, $string, $result);
  629. }
  630. return $result;
  631. }
  632. /**
  633. * Returns corresponding table name for given model $className. ("people" for the model class "Person").
  634. *
  635. * @param string $className Name of class to get database table name for
  636. * @return string Name of the database table for given class
  637. * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-table-and-class-name-forms
  638. */
  639. public static function tableize($className)
  640. {
  641. $result = static::_cache(__FUNCTION__, $className);
  642. if ($result === false) {
  643. $result = static::pluralize(static::underscore($className));
  644. static::_cache(__FUNCTION__, $className, $result);
  645. }
  646. return $result;
  647. }
  648. /**
  649. * Returns Cake model class name ("Person" for the database table "people".) for given database table.
  650. *
  651. * @param string $tableName Name of database table to get class name for
  652. * @return string Class name
  653. * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-table-and-class-name-forms
  654. */
  655. public static function classify($tableName)
  656. {
  657. $result = static::_cache(__FUNCTION__, $tableName);
  658. if ($result === false) {
  659. $result = static::camelize(static::singularize($tableName));
  660. static::_cache(__FUNCTION__, $tableName, $result);
  661. }
  662. return $result;
  663. }
  664. /**
  665. * Returns camelBacked version of an underscored string.
  666. *
  667. * @param string $string String to convert.
  668. * @return string in variable form
  669. * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-variable-names
  670. */
  671. public static function variable($string)
  672. {
  673. $result = static::_cache(__FUNCTION__, $string);
  674. if ($result === false) {
  675. $camelized = static::camelize(static::underscore($string));
  676. $replace = strtolower(substr($camelized, 0, 1));
  677. $result = $replace . substr($camelized, 1);
  678. static::_cache(__FUNCTION__, $string, $result);
  679. }
  680. return $result;
  681. }
  682. /**
  683. * Returns a string with all spaces converted to dashes (by default), accented
  684. * characters converted to non-accented characters, and non word characters removed.
  685. *
  686. * @deprecated 3.2.7 Use Text::slug() instead.
  687. * @param string $string the string you want to slug
  688. * @param string $replacement will replace keys in map
  689. * @return string
  690. * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-url-safe-strings
  691. */
  692. public static function slug($string, $replacement = '-')
  693. {
  694. $quotedReplacement = preg_quote($replacement, '/');
  695. $map = [
  696. '/[^\s\p{Zs}\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
  697. '/[\s\p{Zs}]+/mu' => $replacement,
  698. sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
  699. ];
  700. $string = str_replace(
  701. array_keys(static::$_transliteration),
  702. array_values(static::$_transliteration),
  703. $string
  704. );
  705. return preg_replace(array_keys($map), array_values($map), $string);
  706. }
  707. }