Inflector.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 0.2.9
  13. * @license https://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 https://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. 'cache' => 'caches',
  145. ];
  146. /**
  147. * Words that should not be inflected
  148. *
  149. * @var array
  150. */
  151. protected static $_uninflected = [
  152. '.*[nrlm]ese', '.*data', '.*deer', '.*fish', '.*measles', '.*ois',
  153. '.*pox', '.*sheep', 'people', 'feedback', 'stadia', '.*?media',
  154. 'chassis', 'clippers', 'debris', 'diabetes', 'equipment', 'gallows',
  155. 'graffiti', 'headquarters', 'information', 'innings', 'news', 'nexus',
  156. 'pokemon', 'proceedings', 'research', 'sea[- ]bass', 'series', 'species', 'weather'
  157. ];
  158. /**
  159. * Default map of accented and special characters to ASCII characters
  160. *
  161. * @var array
  162. */
  163. protected static $_transliteration = [
  164. 'ä' => 'ae',
  165. 'æ' => 'ae',
  166. 'ǽ' => 'ae',
  167. 'ö' => 'oe',
  168. 'œ' => 'oe',
  169. 'ü' => 'ue',
  170. 'Ä' => 'Ae',
  171. 'Ü' => 'Ue',
  172. 'Ö' => 'Oe',
  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. 'ª' => 'a',
  194. 'Ç' => 'C',
  195. 'Ć' => 'C',
  196. 'Ĉ' => 'C',
  197. 'Ċ' => 'C',
  198. 'Č' => 'C',
  199. 'ç' => 'c',
  200. 'ć' => 'c',
  201. 'ĉ' => 'c',
  202. 'ċ' => 'c',
  203. 'č' => 'c',
  204. 'Ð' => 'D',
  205. 'Ď' => 'D',
  206. 'Đ' => 'D',
  207. 'ð' => 'd',
  208. 'ď' => 'd',
  209. 'đ' => 'd',
  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. 'ě' => 'e',
  228. 'Ĝ' => 'G',
  229. 'Ğ' => 'G',
  230. 'Ġ' => 'G',
  231. 'Ģ' => 'G',
  232. 'Ґ' => 'G',
  233. 'ĝ' => 'g',
  234. 'ğ' => 'g',
  235. 'ġ' => 'g',
  236. 'ģ' => 'g',
  237. 'ґ' => 'g',
  238. 'Ĥ' => 'H',
  239. 'Ħ' => 'H',
  240. 'ĥ' => 'h',
  241. 'ħ' => 'h',
  242. 'І' => 'I',
  243. 'Ì' => 'I',
  244. 'Í' => 'I',
  245. 'Î' => 'I',
  246. 'Ї' => 'Yi',
  247. 'Ï' => 'I',
  248. 'Ĩ' => 'I',
  249. 'Ī' => 'I',
  250. 'Ĭ' => 'I',
  251. 'Ǐ' => 'I',
  252. 'Į' => 'I',
  253. 'İ' => 'I',
  254. 'і' => 'i',
  255. 'ì' => 'i',
  256. 'í' => 'i',
  257. 'î' => 'i',
  258. 'ï' => 'i',
  259. 'ї' => 'yi',
  260. 'ĩ' => 'i',
  261. 'ī' => 'i',
  262. 'ĭ' => 'i',
  263. 'ǐ' => 'i',
  264. 'į' => 'i',
  265. 'ı' => 'i',
  266. 'Ĵ' => 'J',
  267. 'ĵ' => 'j',
  268. 'Ķ' => 'K',
  269. 'ķ' => 'k',
  270. 'Ĺ' => 'L',
  271. 'Ļ' => 'L',
  272. 'Ľ' => 'L',
  273. 'Ŀ' => 'L',
  274. 'Ł' => 'L',
  275. 'ĺ' => 'l',
  276. 'ļ' => 'l',
  277. 'ľ' => 'l',
  278. 'ŀ' => 'l',
  279. 'ł' => 'l',
  280. 'Ñ' => 'N',
  281. 'Ń' => 'N',
  282. 'Ņ' => 'N',
  283. 'Ň' => 'N',
  284. 'ñ' => 'n',
  285. 'ń' => 'n',
  286. 'ņ' => 'n',
  287. 'ň' => 'n',
  288. 'ʼn' => 'n',
  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. 'º' => 'o',
  312. 'Ŕ' => 'R',
  313. 'Ŗ' => 'R',
  314. 'Ř' => 'R',
  315. 'ŕ' => 'r',
  316. 'ŗ' => 'r',
  317. 'ř' => 'r',
  318. 'Ś' => 'S',
  319. 'Ŝ' => 'S',
  320. 'Ş' => 'S',
  321. 'Ș' => 'S',
  322. 'Š' => 'S',
  323. 'ẞ' => 'SS',
  324. 'ś' => 's',
  325. 'ŝ' => 's',
  326. 'ş' => 's',
  327. 'ș' => 's',
  328. 'š' => 's',
  329. 'ſ' => 's',
  330. 'Ţ' => 'T',
  331. 'Ț' => 'T',
  332. 'Ť' => 'T',
  333. 'Ŧ' => 'T',
  334. 'ţ' => 't',
  335. 'ț' => 't',
  336. 'ť' => 't',
  337. 'ŧ' => 't',
  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. 'ǜ' => 'u',
  368. 'Ý' => 'Y',
  369. 'Ÿ' => 'Y',
  370. 'Ŷ' => 'Y',
  371. 'ý' => 'y',
  372. 'ÿ' => 'y',
  373. 'ŷ' => 'y',
  374. 'Ŵ' => 'W',
  375. 'ŵ' => 'w',
  376. 'Ź' => 'Z',
  377. 'Ż' => 'Z',
  378. 'Ž' => 'Z',
  379. 'ź' => 'z',
  380. 'ż' => 'z',
  381. 'ž' => 'z',
  382. 'Æ' => 'AE',
  383. 'Ǽ' => 'AE',
  384. 'ß' => 'ss',
  385. 'IJ' => 'IJ',
  386. 'ij' => 'ij',
  387. 'Œ' => 'OE',
  388. 'ƒ' => 'f',
  389. 'Þ' => 'TH',
  390. 'þ' => 'th',
  391. 'Є' => 'Ye',
  392. 'є' => 'ye',
  393. ];
  394. /**
  395. * Method cache array.
  396. *
  397. * @var array
  398. */
  399. protected static $_cache = [];
  400. /**
  401. * The initial state of Inflector so reset() works.
  402. *
  403. * @var array
  404. */
  405. protected static $_initialState = [];
  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|bool $value Inflected value
  412. * @return string|false Inflected value on cache hit or false on cache miss.
  413. */
  414. protected static function _cache($type, $key, $value = false)
  415. {
  416. $key = '_' . $key;
  417. $type = '_' . $type;
  418. if ($value !== false) {
  419. static::$_cache[$type][$key] = $value;
  420. return $value;
  421. }
  422. if (!isset(static::$_cache[$type][$key])) {
  423. return false;
  424. }
  425. return static::$_cache[$type][$key];
  426. }
  427. /**
  428. * Clears Inflectors inflected value caches. And resets the inflection
  429. * rules to the initial values.
  430. *
  431. * @return void
  432. */
  433. public static function reset()
  434. {
  435. if (empty(static::$_initialState)) {
  436. static::$_initialState = get_class_vars(__CLASS__);
  437. return;
  438. }
  439. foreach (static::$_initialState as $key => $val) {
  440. if ($key !== '_initialState') {
  441. static::${$key} = $val;
  442. }
  443. }
  444. }
  445. /**
  446. * Adds custom inflection $rules, of either 'plural', 'singular',
  447. * 'uninflected', 'irregular' or 'transliteration' $type.
  448. *
  449. * ### Usage:
  450. *
  451. * ```
  452. * Inflector::rules('plural', ['/^(inflect)or$/i' => '\1ables']);
  453. * Inflector::rules('irregular', ['red' => 'redlings']);
  454. * Inflector::rules('uninflected', ['dontinflectme']);
  455. * Inflector::rules('transliteration', ['/å/' => 'aa']);
  456. * ```
  457. *
  458. * @param string $type The type of inflection, either 'plural', 'singular',
  459. * 'uninflected' or 'transliteration'.
  460. * @param array $rules Array of rules to be added.
  461. * @param bool $reset If true, will unset default inflections for all
  462. * new rules that are being defined in $rules.
  463. * @return void
  464. */
  465. public static function rules($type, $rules, $reset = false)
  466. {
  467. $var = '_' . $type;
  468. if ($reset) {
  469. static::${$var} = $rules;
  470. } elseif ($type === 'uninflected') {
  471. static::$_uninflected = array_merge(
  472. $rules,
  473. static::$_uninflected
  474. );
  475. } else {
  476. static::${$var} = $rules + static::${$var};
  477. }
  478. static::$_cache = [];
  479. }
  480. /**
  481. * Return $word in plural form.
  482. *
  483. * @param string $word Word in singular
  484. * @return string Word in plural
  485. * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-plural-singular-forms
  486. */
  487. public static function pluralize($word)
  488. {
  489. if (isset(static::$_cache['pluralize'][$word])) {
  490. return static::$_cache['pluralize'][$word];
  491. }
  492. if (!isset(static::$_cache['irregular']['pluralize'])) {
  493. static::$_cache['irregular']['pluralize'] = '(?:' . implode('|', array_keys(static::$_irregular)) . ')';
  494. }
  495. if (preg_match('/(.*?(?:\\b|_))(' . static::$_cache['irregular']['pluralize'] . ')$/i', $word, $regs)) {
  496. static::$_cache['pluralize'][$word] = $regs[1] . substr($regs[2], 0, 1) .
  497. substr(static::$_irregular[strtolower($regs[2])], 1);
  498. return static::$_cache['pluralize'][$word];
  499. }
  500. if (!isset(static::$_cache['uninflected'])) {
  501. static::$_cache['uninflected'] = '(?:' . implode('|', static::$_uninflected) . ')';
  502. }
  503. if (preg_match('/^(' . static::$_cache['uninflected'] . ')$/i', $word, $regs)) {
  504. static::$_cache['pluralize'][$word] = $word;
  505. return $word;
  506. }
  507. foreach (static::$_plural as $rule => $replacement) {
  508. if (preg_match($rule, $word)) {
  509. static::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
  510. return static::$_cache['pluralize'][$word];
  511. }
  512. }
  513. }
  514. /**
  515. * Return $word in singular form.
  516. *
  517. * @param string $word Word in plural
  518. * @return string Word in singular
  519. * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-plural-singular-forms
  520. */
  521. public static function singularize($word)
  522. {
  523. if (isset(static::$_cache['singularize'][$word])) {
  524. return static::$_cache['singularize'][$word];
  525. }
  526. if (!isset(static::$_cache['irregular']['singular'])) {
  527. static::$_cache['irregular']['singular'] = '(?:' . implode('|', static::$_irregular) . ')';
  528. }
  529. if (preg_match('/(.*?(?:\\b|_))(' . static::$_cache['irregular']['singular'] . ')$/i', $word, $regs)) {
  530. static::$_cache['singularize'][$word] = $regs[1] . substr($regs[2], 0, 1) .
  531. substr(array_search(strtolower($regs[2]), static::$_irregular), 1);
  532. return static::$_cache['singularize'][$word];
  533. }
  534. if (!isset(static::$_cache['uninflected'])) {
  535. static::$_cache['uninflected'] = '(?:' . implode('|', static::$_uninflected) . ')';
  536. }
  537. if (preg_match('/^(' . static::$_cache['uninflected'] . ')$/i', $word, $regs)) {
  538. static::$_cache['pluralize'][$word] = $word;
  539. return $word;
  540. }
  541. foreach (static::$_singular as $rule => $replacement) {
  542. if (preg_match($rule, $word)) {
  543. static::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
  544. return static::$_cache['singularize'][$word];
  545. }
  546. }
  547. static::$_cache['singularize'][$word] = $word;
  548. return $word;
  549. }
  550. /**
  551. * Returns the input lower_case_delimited_string as a CamelCasedString.
  552. *
  553. * @param string $string String to camelize
  554. * @param string $delimiter the delimiter in the input string
  555. * @return string CamelizedStringLikeThis.
  556. * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-camelcase-and-under-scored-forms
  557. */
  558. public static function camelize($string, $delimiter = '_')
  559. {
  560. $cacheKey = __FUNCTION__ . $delimiter;
  561. $result = static::_cache($cacheKey, $string);
  562. if ($result === false) {
  563. $result = str_replace(' ', '', static::humanize($string, $delimiter));
  564. static::_cache($cacheKey, $string, $result);
  565. }
  566. return $result;
  567. }
  568. /**
  569. * Returns the input CamelCasedString as an underscored_string.
  570. *
  571. * Also replaces dashes with underscores
  572. *
  573. * @param string $string CamelCasedString to be "underscorized"
  574. * @return string underscore_version of the input string
  575. * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-camelcase-and-under-scored-forms
  576. */
  577. public static function underscore($string)
  578. {
  579. return static::delimit(str_replace('-', '_', $string), '_');
  580. }
  581. /**
  582. * Returns the input CamelCasedString as an dashed-string.
  583. *
  584. * Also replaces underscores with dashes
  585. *
  586. * @param string $string The string to dasherize.
  587. * @return string Dashed version of the input string
  588. */
  589. public static function dasherize($string)
  590. {
  591. return static::delimit(str_replace('_', '-', $string), '-');
  592. }
  593. /**
  594. * Returns the input lower_case_delimited_string as 'A Human Readable String'.
  595. * (Underscores are replaced by spaces and capitalized following words.)
  596. *
  597. * @param string $string String to be humanized
  598. * @param string $delimiter the character to replace with a space
  599. * @return string Human-readable string
  600. * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-human-readable-forms
  601. */
  602. public static function humanize($string, $delimiter = '_')
  603. {
  604. $cacheKey = __FUNCTION__ . $delimiter;
  605. $result = static::_cache($cacheKey, $string);
  606. if ($result === false) {
  607. $result = explode(' ', str_replace($delimiter, ' ', $string));
  608. foreach ($result as &$word) {
  609. $word = mb_strtoupper(mb_substr($word, 0, 1)) . mb_substr($word, 1);
  610. }
  611. $result = implode(' ', $result);
  612. static::_cache($cacheKey, $string, $result);
  613. }
  614. return $result;
  615. }
  616. /**
  617. * Expects a CamelCasedInputString, and produces a lower_case_delimited_string
  618. *
  619. * @param string $string String to delimit
  620. * @param string $delimiter the character to use as a delimiter
  621. * @return string delimited string
  622. */
  623. public static function delimit($string, $delimiter = '_')
  624. {
  625. $cacheKey = __FUNCTION__ . $delimiter;
  626. $result = static::_cache($cacheKey, $string);
  627. if ($result === false) {
  628. $result = mb_strtolower(preg_replace('/(?<=\\w)([A-Z])/', $delimiter . '\\1', $string));
  629. static::_cache($cacheKey, $string, $result);
  630. }
  631. return $result;
  632. }
  633. /**
  634. * Returns corresponding table name for given model $className. ("people" for the model class "Person").
  635. *
  636. * @param string $className Name of class to get database table name for
  637. * @return string Name of the database table for given class
  638. * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-table-and-class-name-forms
  639. */
  640. public static function tableize($className)
  641. {
  642. $result = static::_cache(__FUNCTION__, $className);
  643. if ($result === false) {
  644. $result = static::pluralize(static::underscore($className));
  645. static::_cache(__FUNCTION__, $className, $result);
  646. }
  647. return $result;
  648. }
  649. /**
  650. * Returns Cake model class name ("Person" for the database table "people".) for given database table.
  651. *
  652. * @param string $tableName Name of database table to get class name for
  653. * @return string Class name
  654. * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-table-and-class-name-forms
  655. */
  656. public static function classify($tableName)
  657. {
  658. $result = static::_cache(__FUNCTION__, $tableName);
  659. if ($result === false) {
  660. $result = static::camelize(static::singularize($tableName));
  661. static::_cache(__FUNCTION__, $tableName, $result);
  662. }
  663. return $result;
  664. }
  665. /**
  666. * Returns camelBacked version of an underscored string.
  667. *
  668. * @param string $string String to convert.
  669. * @return string in variable form
  670. * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-variable-names
  671. */
  672. public static function variable($string)
  673. {
  674. $result = static::_cache(__FUNCTION__, $string);
  675. if ($result === false) {
  676. $camelized = static::camelize(static::underscore($string));
  677. $replace = strtolower(substr($camelized, 0, 1));
  678. $result = $replace . substr($camelized, 1);
  679. static::_cache(__FUNCTION__, $string, $result);
  680. }
  681. return $result;
  682. }
  683. /**
  684. * Returns a string with all spaces converted to dashes (by default), accented
  685. * characters converted to non-accented characters, and non word characters removed.
  686. *
  687. * @deprecated 3.2.7 Use Text::slug() instead.
  688. * @param string $string the string you want to slug
  689. * @param string $replacement will replace keys in map
  690. * @return string
  691. * @link https://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-url-safe-strings
  692. */
  693. public static function slug($string, $replacement = '-')
  694. {
  695. deprecationWarning(
  696. 'Inflector::slug() is deprecated. ' .
  697. 'Use Text::slug() instead.'
  698. );
  699. $quotedReplacement = preg_quote($replacement, '/');
  700. $map = [
  701. '/[^\s\p{Zs}\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
  702. '/[\s\p{Zs}]+/mu' => $replacement,
  703. sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
  704. ];
  705. $string = str_replace(
  706. array_keys(static::$_transliteration),
  707. static::$_transliteration,
  708. $string
  709. );
  710. return preg_replace(array_keys($map), array_values($map), $string);
  711. }
  712. }