BaseInflector.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2008 Yii Software LLC
  4. * @link http://www.yiiframework.com/
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\helpers;
  8. use Yii;
  9. /**
  10. * BaseInflector provides concrete implementation for [[Inflector]].
  11. *
  12. * Do not use BaseInflector. Use [[Inflector]] instead.
  13. *
  14. * @author Antonio Ramirez <[email protected]>
  15. * @since 2.0
  16. */
  17. class BaseInflector
  18. {
  19. /**
  20. * @var array the rules for converting a word into its plural form.
  21. * The keys are the regular expressions and the values are the corresponding replacements.
  22. */
  23. public static $plurals = [
  24. '/([nrlm]ese|deer|fish|sheep|measles|ois|pox|media)$/i' => '\1',
  25. '/^(sea[- ]bass)$/i' => '\1',
  26. '/(m)ove$/i' => '\1oves',
  27. '/(f)oot$/i' => '\1eet',
  28. '/(h)uman$/i' => '\1umans',
  29. '/(s)tatus$/i' => '\1tatuses',
  30. '/(s)taff$/i' => '\1taff',
  31. '/(t)ooth$/i' => '\1eeth',
  32. '/(quiz)$/i' => '\1zes',
  33. '/^(ox)$/i' => '\1\2en',
  34. '/([m|l])ouse$/i' => '\1ice',
  35. '/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
  36. '/(x|ch|ss|sh)$/i' => '\1es',
  37. '/([^aeiouy]|qu)y$/i' => '\1ies',
  38. '/(hive)$/i' => '\1s',
  39. '/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
  40. '/sis$/i' => 'ses',
  41. '/([ti])um$/i' => '\1a',
  42. '/(p)erson$/i' => '\1eople',
  43. '/(m)an$/i' => '\1en',
  44. '/(c)hild$/i' => '\1hildren',
  45. '/(buffal|tomat|potat|ech|her|vet)o$/i' => '\1oes',
  46. '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
  47. '/us$/i' => 'uses',
  48. '/(alias)$/i' => '\1es',
  49. '/(ax|cris|test)is$/i' => '\1es',
  50. '/s$/' => 's',
  51. '/^$/' => '',
  52. '/$/' => 's',
  53. ];
  54. /**
  55. * @var array the rules for converting a word into its singular form.
  56. * The keys are the regular expressions and the values are the corresponding replacements.
  57. */
  58. public static $singulars = [
  59. '/([nrlm]ese|deer|fish|sheep|measles|ois|pox|media|ss)$/i' => '\1',
  60. '/^(sea[- ]bass)$/i' => '\1',
  61. '/(s)tatuses$/i' => '\1tatus',
  62. '/(f)eet$/i' => '\1oot',
  63. '/(t)eeth$/i' => '\1ooth',
  64. '/^(.*)(menu)s$/i' => '\1\2',
  65. '/(quiz)zes$/i' => '\\1',
  66. '/(matr)ices$/i' => '\1ix',
  67. '/(vert|ind)ices$/i' => '\1ex',
  68. '/^(ox)en/i' => '\1',
  69. '/(alias)(es)*$/i' => '\1',
  70. '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
  71. '/([ftw]ax)es/i' => '\1',
  72. '/(cris|ax|test)es$/i' => '\1is',
  73. '/(shoe|slave)s$/i' => '\1',
  74. '/(o)es$/i' => '\1',
  75. '/ouses$/' => 'ouse',
  76. '/([^a])uses$/' => '\1us',
  77. '/([m|l])ice$/i' => '\1ouse',
  78. '/(x|ch|ss|sh)es$/i' => '\1',
  79. '/(m)ovies$/i' => '\1\2ovie',
  80. '/(s)eries$/i' => '\1\2eries',
  81. '/([^aeiouy]|qu)ies$/i' => '\1y',
  82. '/([lr])ves$/i' => '\1f',
  83. '/(tive)s$/i' => '\1',
  84. '/(hive)s$/i' => '\1',
  85. '/(drive)s$/i' => '\1',
  86. '/([^fo])ves$/i' => '\1fe',
  87. '/(^analy)ses$/i' => '\1sis',
  88. '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
  89. '/([ti])a$/i' => '\1um',
  90. '/(p)eople$/i' => '\1\2erson',
  91. '/(m)en$/i' => '\1an',
  92. '/(c)hildren$/i' => '\1\2hild',
  93. '/(n)ews$/i' => '\1\2ews',
  94. '/eaus$/' => 'eau',
  95. '/^(.*us)$/' => '\\1',
  96. '/s$/i' => '',
  97. ];
  98. /**
  99. * @var array the special rules for converting a word between its plural form and singular form.
  100. * The keys are the special words in singular form, and the values are the corresponding plural form.
  101. */
  102. public static $specials = [
  103. 'atlas' => 'atlases',
  104. 'beef' => 'beefs',
  105. 'brother' => 'brothers',
  106. 'cafe' => 'cafes',
  107. 'child' => 'children',
  108. 'cookie' => 'cookies',
  109. 'corpus' => 'corpuses',
  110. 'cow' => 'cows',
  111. 'curve' => 'curves',
  112. 'foe' => 'foes',
  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. 'sex' => 'sexes',
  132. 'soliloquy' => 'soliloquies',
  133. 'testis' => 'testes',
  134. 'trilby' => 'trilbys',
  135. 'turf' => 'turfs',
  136. 'wave' => 'waves',
  137. 'Amoyese' => 'Amoyese',
  138. 'bison' => 'bison',
  139. 'Borghese' => 'Borghese',
  140. 'bream' => 'bream',
  141. 'breeches' => 'breeches',
  142. 'britches' => 'britches',
  143. 'buffalo' => 'buffalo',
  144. 'cantus' => 'cantus',
  145. 'carp' => 'carp',
  146. 'chassis' => 'chassis',
  147. 'clippers' => 'clippers',
  148. 'cod' => 'cod',
  149. 'coitus' => 'coitus',
  150. 'Congoese' => 'Congoese',
  151. 'contretemps' => 'contretemps',
  152. 'corps' => 'corps',
  153. 'debris' => 'debris',
  154. 'diabetes' => 'diabetes',
  155. 'djinn' => 'djinn',
  156. 'eland' => 'eland',
  157. 'elk' => 'elk',
  158. 'equipment' => 'equipment',
  159. 'Faroese' => 'Faroese',
  160. 'flounder' => 'flounder',
  161. 'Foochowese' => 'Foochowese',
  162. 'gallows' => 'gallows',
  163. 'Genevese' => 'Genevese',
  164. 'Genoese' => 'Genoese',
  165. 'Gilbertese' => 'Gilbertese',
  166. 'graffiti' => 'graffiti',
  167. 'headquarters' => 'headquarters',
  168. 'herpes' => 'herpes',
  169. 'hijinks' => 'hijinks',
  170. 'Hottentotese' => 'Hottentotese',
  171. 'information' => 'information',
  172. 'innings' => 'innings',
  173. 'jackanapes' => 'jackanapes',
  174. 'Kiplingese' => 'Kiplingese',
  175. 'Kongoese' => 'Kongoese',
  176. 'Lucchese' => 'Lucchese',
  177. 'mackerel' => 'mackerel',
  178. 'Maltese' => 'Maltese',
  179. 'mews' => 'mews',
  180. 'moose' => 'moose',
  181. 'mumps' => 'mumps',
  182. 'Nankingese' => 'Nankingese',
  183. 'news' => 'news',
  184. 'nexus' => 'nexus',
  185. 'Niasese' => 'Niasese',
  186. 'Pekingese' => 'Pekingese',
  187. 'Piedmontese' => 'Piedmontese',
  188. 'pincers' => 'pincers',
  189. 'Pistoiese' => 'Pistoiese',
  190. 'pliers' => 'pliers',
  191. 'Portuguese' => 'Portuguese',
  192. 'proceedings' => 'proceedings',
  193. 'rabies' => 'rabies',
  194. 'rice' => 'rice',
  195. 'rhinoceros' => 'rhinoceros',
  196. 'salmon' => 'salmon',
  197. 'Sarawakese' => 'Sarawakese',
  198. 'scissors' => 'scissors',
  199. 'series' => 'series',
  200. 'Shavese' => 'Shavese',
  201. 'shears' => 'shears',
  202. 'siemens' => 'siemens',
  203. 'species' => 'species',
  204. 'swine' => 'swine',
  205. 'testes' => 'testes',
  206. 'trousers' => 'trousers',
  207. 'trout' => 'trout',
  208. 'tuna' => 'tuna',
  209. 'Vermontese' => 'Vermontese',
  210. 'Wenchowese' => 'Wenchowese',
  211. 'whiting' => 'whiting',
  212. 'wildebeest' => 'wildebeest',
  213. 'Yengeese' => 'Yengeese',
  214. ];
  215. /**
  216. * @var array map of special chars and its translation. This is used by [[slug()]].
  217. */
  218. public static $transliteration = [
  219. // Latin
  220. 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE', 'Ç' => 'C',
  221. 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I',
  222. 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ő' => 'O',
  223. 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U', 'Ý' => 'Y', 'Þ' => 'TH',
  224. 'ß' => 'ss',
  225. 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c',
  226. 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
  227. 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o',
  228. 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th',
  229. 'ÿ' => 'y',
  230. // Latin symbols
  231. '©' => '(c)',
  232. // Greek
  233. 'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Θ' => '8',
  234. 'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O', 'Π' => 'P',
  235. 'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W',
  236. 'Ά' => 'A', 'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I',
  237. 'Ϋ' => 'Y',
  238. 'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => '8',
  239. 'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'π' => 'p',
  240. 'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w',
  241. 'ά' => 'a', 'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'ς' => 's',
  242. 'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i',
  243. // Turkish
  244. 'Ş' => 'S', 'İ' => 'I', 'Ç' => 'C', 'Ü' => 'U', 'Ö' => 'O', 'Ğ' => 'G',
  245. 'ş' => 's', 'ı' => 'i', 'ç' => 'c', 'ü' => 'u', 'ö' => 'o', 'ğ' => 'g',
  246. // Russian
  247. 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh',
  248. 'З' => 'Z', 'И' => 'I', 'Й' => 'J', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
  249. 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
  250. 'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sh', 'Ъ' => '', 'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'Yu',
  251. 'Я' => 'Ya',
  252. 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh',
  253. 'з' => 'z', 'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o',
  254. 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c',
  255. 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sh', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e', 'ю' => 'yu',
  256. 'я' => 'ya',
  257. // Ukrainian
  258. 'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G',
  259. 'є' => 'ye', 'і' => 'i', 'ї' => 'yi', 'ґ' => 'g',
  260. // Czech
  261. 'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T', 'Ů' => 'U',
  262. 'Ž' => 'Z',
  263. 'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u',
  264. 'ž' => 'z',
  265. // Polish
  266. 'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'e', 'Ł' => 'L', 'Ń' => 'N', 'Ó' => 'o', 'Ś' => 'S', 'Ź' => 'Z',
  267. 'Ż' => 'Z',
  268. 'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ó' => 'o', 'ś' => 's', 'ź' => 'z',
  269. 'ż' => 'z',
  270. // Latvian
  271. 'Ā' => 'A', 'Č' => 'C', 'Ē' => 'E', 'Ģ' => 'G', 'Ī' => 'i', 'Ķ' => 'k', 'Ļ' => 'L', 'Ņ' => 'N',
  272. 'Š' => 'S', 'Ū' => 'u', 'Ž' => 'Z',
  273. 'ā' => 'a', 'č' => 'c', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n',
  274. 'š' => 's', 'ū' => 'u', 'ž' => 'z'
  275. ];
  276. /**
  277. * Converts a word to its plural form.
  278. * Note that this is for English only!
  279. * For example, 'apple' will become 'apples', and 'child' will become 'children'.
  280. * @param string $word the word to be pluralized
  281. * @return string the pluralized word
  282. */
  283. public static function pluralize($word)
  284. {
  285. if (isset(static::$specials[$word])) {
  286. return static::$specials[$word];
  287. }
  288. foreach (static::$plurals as $rule => $replacement) {
  289. if (preg_match($rule, $word)) {
  290. return preg_replace($rule, $replacement, $word);
  291. }
  292. }
  293. return $word;
  294. }
  295. /**
  296. * Returns the singular of the $word
  297. * @param string $word the english word to singularize
  298. * @return string Singular noun.
  299. */
  300. public static function singularize($word)
  301. {
  302. $result = array_search($word, static::$specials, true);
  303. if ($result !== false) {
  304. return $result;
  305. }
  306. foreach (static::$singulars as $rule => $replacement) {
  307. if (preg_match($rule, $word)) {
  308. return preg_replace($rule, $replacement, $word);
  309. }
  310. }
  311. return $word;
  312. }
  313. /**
  314. * Converts an underscored or CamelCase word into a English
  315. * sentence.
  316. * @param string $words
  317. * @param bool $ucAll whether to set all words to uppercase
  318. * @return string
  319. */
  320. public static function titleize($words, $ucAll = false)
  321. {
  322. $words = static::humanize(static::underscore($words), $ucAll);
  323. return $ucAll ? ucwords($words) : ucfirst($words);
  324. }
  325. /**
  326. * Returns given word as CamelCased
  327. * Converts a word like "send_email" to "SendEmail". It
  328. * will remove non alphanumeric character from the word, so
  329. * "who's online" will be converted to "WhoSOnline"
  330. * @see variablize()
  331. * @param string $word the word to CamelCase
  332. * @return string
  333. */
  334. public static function camelize($word)
  335. {
  336. return str_replace(' ', '', ucwords(preg_replace('/[^A-Za-z0-9]+/', ' ', $word)));
  337. }
  338. /**
  339. * Converts a CamelCase name into space-separated words.
  340. * For example, 'PostTag' will be converted to 'Post Tag'.
  341. * @param string $name the string to be converted
  342. * @param boolean $ucwords whether to capitalize the first letter in each word
  343. * @return string the resulting words
  344. */
  345. public static function camel2words($name, $ucwords = true)
  346. {
  347. $label = trim(strtolower(str_replace([
  348. '-',
  349. '_',
  350. '.'
  351. ], ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $name))));
  352. return $ucwords ? ucwords($label) : $label;
  353. }
  354. /**
  355. * Converts a CamelCase name into an ID in lowercase.
  356. * Words in the ID may be concatenated using the specified character (defaults to '-').
  357. * For example, 'PostTag' will be converted to 'post-tag'.
  358. * @param string $name the string to be converted
  359. * @param string $separator the character used to concatenate the words in the ID
  360. * @return string the resulting ID
  361. */
  362. public static function camel2id($name, $separator = '-')
  363. {
  364. if ($separator === '_') {
  365. return trim(strtolower(preg_replace('/(?<![A-Z])[A-Z]/', '_\0', $name)), '_');
  366. } else {
  367. return trim(strtolower(str_replace('_', $separator, preg_replace('/(?<![A-Z])[A-Z]/', $separator . '\0', $name))), $separator);
  368. }
  369. }
  370. /**
  371. * Converts an ID into a CamelCase name.
  372. * Words in the ID separated by `$separator` (defaults to '-') will be concatenated into a CamelCase name.
  373. * For example, 'post-tag' is converted to 'PostTag'.
  374. * @param string $id the ID to be converted
  375. * @param string $separator the character used to separate the words in the ID
  376. * @return string the resulting CamelCase name
  377. */
  378. public static function id2camel($id, $separator = '-')
  379. {
  380. return str_replace(' ', '', ucwords(implode(' ', explode($separator, $id))));
  381. }
  382. /**
  383. * Converts any "CamelCased" into an "underscored_word".
  384. * @param string $words the word(s) to underscore
  385. * @return string
  386. */
  387. public static function underscore($words)
  388. {
  389. return strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $words));
  390. }
  391. /**
  392. * Returns a human-readable string from $word
  393. * @param string $word the string to humanize
  394. * @param bool $ucAll whether to set all words to uppercase or not
  395. * @return string
  396. */
  397. public static function humanize($word, $ucAll = false)
  398. {
  399. $word = str_replace('_', ' ', preg_replace('/_id$/', '', $word));
  400. return $ucAll ? ucwords($word) : ucfirst($word);
  401. }
  402. /**
  403. * Same as camelize but first char is in lowercase.
  404. * Converts a word like "send_email" to "sendEmail". It
  405. * will remove non alphanumeric character from the word, so
  406. * "who's online" will be converted to "whoSOnline"
  407. * @param string $word to lowerCamelCase
  408. * @return string
  409. */
  410. public static function variablize($word)
  411. {
  412. $word = static::camelize($word);
  413. return strtolower($word[0]) . substr($word, 1);
  414. }
  415. /**
  416. * Converts a class name to its table name (pluralized)
  417. * naming conventions. For example, converts "Person" to "people"
  418. * @param string $className the class name for getting related table_name
  419. * @return string
  420. */
  421. public static function tableize($className)
  422. {
  423. return static::pluralize(static::underscore($className));
  424. }
  425. /**
  426. * Returns a string with all spaces converted to given replacement and
  427. * non word characters removed. Maps special characters to ASCII using
  428. * [[$transliteration]] array.
  429. * @param string $string An arbitrary string to convert
  430. * @param string $replacement The replacement to use for spaces
  431. * @param bool $lowercase whether to return the string in lowercase or not. Defaults to `true`.
  432. * @return string The converted string.
  433. */
  434. public static function slug($string, $replacement = '-', $lowercase = true)
  435. {
  436. if (extension_loaded('intl') === true) {
  437. $options = 'Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove;';
  438. $string = transliterator_transliterate($options, $string);
  439. $string = preg_replace('/[-\s]+/', $replacement, $string);
  440. } else {
  441. $string = str_replace(array_keys(static::$transliteration), static::$transliteration, $string);
  442. $string = preg_replace('/[^\p{L}\p{Nd}]+/u', $replacement, $string);
  443. }
  444. $string = trim($string, $replacement);
  445. return $lowercase ? strtolower($string) : $string;
  446. }
  447. /**
  448. * Converts a table name to its class name. For example, converts "people" to "Person"
  449. * @param string $tableName
  450. * @return string
  451. */
  452. public static function classify($tableName)
  453. {
  454. return static::camelize(static::singularize($tableName));
  455. }
  456. /**
  457. * Converts number to its ordinal English form. For example, converts 13 to 13th, 2 to 2nd ...
  458. * @param int $number the number to get its ordinal value
  459. * @return string
  460. */
  461. public static function ordinalize($number)
  462. {
  463. if (in_array(($number % 100), range(11, 13))) {
  464. return $number . 'th';
  465. }
  466. switch ($number % 10) {
  467. case 1: return $number . 'st';
  468. case 2: return $number . 'nd';
  469. case 3: return $number . 'rd';
  470. default: return $number . 'th';
  471. }
  472. }
  473. }