BaseInflector.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  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 <amigo.cobos@gmail.com>
  15. * @author Alexander Makarov <sam@rmcreative.ru>
  16. * @since 2.0
  17. */
  18. class BaseInflector
  19. {
  20. /**
  21. * @var array the rules for converting a word into its plural form.
  22. * The keys are the regular expressions and the values are the corresponding replacements.
  23. */
  24. public static $plurals = [
  25. '/([nrlm]ese|deer|fish|sheep|measles|ois|pox|media)$/i' => '\1',
  26. '/^(sea[- ]bass)$/i' => '\1',
  27. '/(m)ove$/i' => '\1oves',
  28. '/(f)oot$/i' => '\1eet',
  29. '/(h)uman$/i' => '\1umans',
  30. '/(s)tatus$/i' => '\1tatuses',
  31. '/(s)taff$/i' => '\1taff',
  32. '/(t)ooth$/i' => '\1eeth',
  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|([lr])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|potat|ech|her|vet)o$/i' => '\1oes',
  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. '/(currenc)y$/' => '\1ies',
  52. '/s$/' => 's',
  53. '/^$/' => '',
  54. '/$/' => 's',
  55. ];
  56. /**
  57. * @var array the rules for converting a word into its singular form.
  58. * The keys are the regular expressions and the values are the corresponding replacements.
  59. */
  60. public static $singulars = [
  61. '/([nrlm]ese|deer|fish|sheep|measles|ois|pox|media|ss)$/i' => '\1',
  62. '/^(sea[- ]bass)$/i' => '\1',
  63. '/(s)tatuses$/i' => '\1tatus',
  64. '/(f)eet$/i' => '\1oot',
  65. '/(t)eeth$/i' => '\1ooth',
  66. '/^(.*)(menu)s$/i' => '\1\2',
  67. '/(quiz)zes$/i' => '\\1',
  68. '/(matr)ices$/i' => '\1ix',
  69. '/(vert|ind)ices$/i' => '\1ex',
  70. '/^(ox)en/i' => '\1',
  71. '/(alias)(es)*$/i' => '\1',
  72. '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
  73. '/([ftw]ax)es/i' => '\1',
  74. '/(cris|ax|test)es$/i' => '\1is',
  75. '/(shoe|slave)s$/i' => '\1',
  76. '/(o)es$/i' => '\1',
  77. '/ouses$/' => 'ouse',
  78. '/([^a])uses$/' => '\1us',
  79. '/([m|l])ice$/i' => '\1ouse',
  80. '/(x|ch|ss|sh)es$/i' => '\1',
  81. '/(m)ovies$/i' => '\1\2ovie',
  82. '/(s)eries$/i' => '\1\2eries',
  83. '/([^aeiouy]|qu)ies$/i' => '\1y',
  84. '/([lr])ves$/i' => '\1f',
  85. '/(tive)s$/i' => '\1',
  86. '/(hive)s$/i' => '\1',
  87. '/(drive)s$/i' => '\1',
  88. '/([^fo])ves$/i' => '\1fe',
  89. '/(^analy)ses$/i' => '\1sis',
  90. '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
  91. '/([ti])a$/i' => '\1um',
  92. '/(p)eople$/i' => '\1\2erson',
  93. '/(m)en$/i' => '\1an',
  94. '/(c)hildren$/i' => '\1\2hild',
  95. '/(n)ews$/i' => '\1\2ews',
  96. '/(n)etherlands$/i' => '\1\2etherlands',
  97. '/eaus$/' => 'eau',
  98. '/(currenc)ies$/' => '\1y',
  99. '/^(.*us)$/' => '\\1',
  100. '/s$/i' => '',
  101. ];
  102. /**
  103. * @var array the special rules for converting a word between its plural form and singular form.
  104. * The keys are the special words in singular form, and the values are the corresponding plural form.
  105. */
  106. public static $specials = [
  107. 'atlas' => 'atlases',
  108. 'beef' => 'beefs',
  109. 'brother' => 'brothers',
  110. 'cafe' => 'cafes',
  111. 'child' => 'children',
  112. 'cookie' => 'cookies',
  113. 'corpus' => 'corpuses',
  114. 'cow' => 'cows',
  115. 'curve' => 'curves',
  116. 'foe' => 'foes',
  117. 'ganglion' => 'ganglions',
  118. 'genie' => 'genies',
  119. 'genus' => 'genera',
  120. 'graffito' => 'graffiti',
  121. 'hoof' => 'hoofs',
  122. 'loaf' => 'loaves',
  123. 'man' => 'men',
  124. 'money' => 'monies',
  125. 'mongoose' => 'mongooses',
  126. 'move' => 'moves',
  127. 'mythos' => 'mythoi',
  128. 'niche' => 'niches',
  129. 'numen' => 'numina',
  130. 'occiput' => 'occiputs',
  131. 'octopus' => 'octopuses',
  132. 'opus' => 'opuses',
  133. 'ox' => 'oxen',
  134. 'pasta' => 'pasta',
  135. 'penis' => 'penises',
  136. 'sex' => 'sexes',
  137. 'soliloquy' => 'soliloquies',
  138. 'testis' => 'testes',
  139. 'trilby' => 'trilbys',
  140. 'turf' => 'turfs',
  141. 'wave' => 'waves',
  142. 'Amoyese' => 'Amoyese',
  143. 'bison' => 'bison',
  144. 'Borghese' => 'Borghese',
  145. 'bream' => 'bream',
  146. 'breeches' => 'breeches',
  147. 'britches' => 'britches',
  148. 'buffalo' => 'buffalo',
  149. 'cantus' => 'cantus',
  150. 'carp' => 'carp',
  151. 'chassis' => 'chassis',
  152. 'clippers' => 'clippers',
  153. 'cod' => 'cod',
  154. 'coitus' => 'coitus',
  155. 'Congoese' => 'Congoese',
  156. 'contretemps' => 'contretemps',
  157. 'corps' => 'corps',
  158. 'debris' => 'debris',
  159. 'diabetes' => 'diabetes',
  160. 'djinn' => 'djinn',
  161. 'eland' => 'eland',
  162. 'elk' => 'elk',
  163. 'equipment' => 'equipment',
  164. 'Faroese' => 'Faroese',
  165. 'flounder' => 'flounder',
  166. 'Foochowese' => 'Foochowese',
  167. 'gallows' => 'gallows',
  168. 'Genevese' => 'Genevese',
  169. 'Genoese' => 'Genoese',
  170. 'Gilbertese' => 'Gilbertese',
  171. 'graffiti' => 'graffiti',
  172. 'headquarters' => 'headquarters',
  173. 'herpes' => 'herpes',
  174. 'hijinks' => 'hijinks',
  175. 'Hottentotese' => 'Hottentotese',
  176. 'information' => 'information',
  177. 'innings' => 'innings',
  178. 'jackanapes' => 'jackanapes',
  179. 'Kiplingese' => 'Kiplingese',
  180. 'Kongoese' => 'Kongoese',
  181. 'Lucchese' => 'Lucchese',
  182. 'mackerel' => 'mackerel',
  183. 'Maltese' => 'Maltese',
  184. 'mews' => 'mews',
  185. 'moose' => 'moose',
  186. 'mumps' => 'mumps',
  187. 'Nankingese' => 'Nankingese',
  188. 'news' => 'news',
  189. 'nexus' => 'nexus',
  190. 'Niasese' => 'Niasese',
  191. 'Pekingese' => 'Pekingese',
  192. 'Piedmontese' => 'Piedmontese',
  193. 'pincers' => 'pincers',
  194. 'Pistoiese' => 'Pistoiese',
  195. 'pliers' => 'pliers',
  196. 'Portuguese' => 'Portuguese',
  197. 'proceedings' => 'proceedings',
  198. 'rabies' => 'rabies',
  199. 'rice' => 'rice',
  200. 'rhinoceros' => 'rhinoceros',
  201. 'salmon' => 'salmon',
  202. 'Sarawakese' => 'Sarawakese',
  203. 'scissors' => 'scissors',
  204. 'series' => 'series',
  205. 'Shavese' => 'Shavese',
  206. 'shears' => 'shears',
  207. 'siemens' => 'siemens',
  208. 'species' => 'species',
  209. 'swine' => 'swine',
  210. 'testes' => 'testes',
  211. 'trousers' => 'trousers',
  212. 'trout' => 'trout',
  213. 'tuna' => 'tuna',
  214. 'Vermontese' => 'Vermontese',
  215. 'Wenchowese' => 'Wenchowese',
  216. 'whiting' => 'whiting',
  217. 'wildebeest' => 'wildebeest',
  218. 'Yengeese' => 'Yengeese',
  219. ];
  220. /**
  221. * @var array fallback map for transliteration used by [[transliterate()]] when intl isn't available.
  222. */
  223. public static $transliteration = [
  224. 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE', 'Ç' => 'C',
  225. 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I',
  226. 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ő' => 'O',
  227. 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U', 'Ý' => 'Y', 'Þ' => 'TH',
  228. 'ß' => 'ss',
  229. 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c',
  230. 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
  231. 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o',
  232. 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th',
  233. 'ÿ' => 'y',
  234. ];
  235. /**
  236. * Shortcut for `Any-Latin; NFKD` transliteration rule.
  237. *
  238. * The rule is strict, letters will be transliterated with
  239. * the closest sound-representation chars. The result may contain any UTF-8 chars. For example:
  240. * `获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?` will be transliterated to
  241. * `huò qǔ dào dochira Ukraí̈nsʹka: g̀,ê, Srpska: đ, n̂, d̂! ¿Español?`.
  242. *
  243. * Used in [[transliterate()]].
  244. * For detailed information see [unicode normalization forms](http://unicode.org/reports/tr15/#Normalization_Forms_Table)
  245. * @see http://unicode.org/reports/tr15/#Normalization_Forms_Table
  246. * @see transliterate()
  247. * @since 2.0.7
  248. */
  249. const TRANSLITERATE_STRICT = 'Any-Latin; NFKD';
  250. /**
  251. * Shortcut for `Any-Latin; Latin-ASCII` transliteration rule.
  252. *
  253. * The rule is medium, letters will be
  254. * transliterated to characters of Latin-1 (ISO 8859-1) ASCII table. For example:
  255. * `获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?` will be transliterated to
  256. * `huo qu dao dochira Ukrainsʹka: g,e, Srpska: d, n, d! ¿Espanol?`.
  257. *
  258. * Used in [[transliterate()]].
  259. * For detailed information see [unicode normalization forms](http://unicode.org/reports/tr15/#Normalization_Forms_Table)
  260. * @see http://unicode.org/reports/tr15/#Normalization_Forms_Table
  261. * @see transliterate()
  262. * @since 2.0.7
  263. */
  264. const TRANSLITERATE_MEDIUM = 'Any-Latin; Latin-ASCII';
  265. /**
  266. * Shortcut for `Any-Latin; Latin-ASCII; [\u0080-\uffff] remove` transliteration rule.
  267. *
  268. * The rule is loose,
  269. * letters will be transliterated with the characters of Basic Latin Unicode Block.
  270. * For example:
  271. * `获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?` will be transliterated to
  272. * `huo qu dao dochira Ukrainska: g,e, Srpska: d, n, d! Espanol?`.
  273. *
  274. * Used in [[transliterate()]].
  275. * For detailed information see [unicode normalization forms](http://unicode.org/reports/tr15/#Normalization_Forms_Table)
  276. * @see http://unicode.org/reports/tr15/#Normalization_Forms_Table
  277. * @see transliterate()
  278. * @since 2.0.7
  279. */
  280. const TRANSLITERATE_LOOSE = 'Any-Latin; Latin-ASCII; [\u0080-\uffff] remove';
  281. /**
  282. * @var mixed Either a [[\Transliterator]], or a string from which a [[\Transliterator]] can be built
  283. * for transliteration. Used by [[transliterate()]] when intl is available. Defaults to [[TRANSLITERATE_LOOSE]]
  284. * @see https://secure.php.net/manual/en/transliterator.transliterate.php
  285. */
  286. public static $transliterator = self::TRANSLITERATE_LOOSE;
  287. /**
  288. * Converts a word to its plural form.
  289. * Note that this is for English only!
  290. * For example, 'apple' will become 'apples', and 'child' will become 'children'.
  291. * @param string $word the word to be pluralized
  292. * @return string the pluralized word
  293. */
  294. public static function pluralize($word)
  295. {
  296. if (isset(static::$specials[$word])) {
  297. return static::$specials[$word];
  298. }
  299. foreach (static::$plurals as $rule => $replacement) {
  300. if (preg_match($rule, $word)) {
  301. return preg_replace($rule, $replacement, $word);
  302. }
  303. }
  304. return $word;
  305. }
  306. /**
  307. * Returns the singular of the $word.
  308. * @param string $word the english word to singularize
  309. * @return string Singular noun.
  310. */
  311. public static function singularize($word)
  312. {
  313. $result = array_search($word, static::$specials, true);
  314. if ($result !== false) {
  315. return $result;
  316. }
  317. foreach (static::$singulars as $rule => $replacement) {
  318. if (preg_match($rule, $word)) {
  319. return preg_replace($rule, $replacement, $word);
  320. }
  321. }
  322. return $word;
  323. }
  324. /**
  325. * Converts an underscored or CamelCase word into a English
  326. * sentence.
  327. * @param string $words
  328. * @param bool $ucAll whether to set all words to uppercase
  329. * @return string
  330. */
  331. public static function titleize($words, $ucAll = false)
  332. {
  333. $words = static::humanize(static::underscore($words), $ucAll);
  334. return $ucAll ? StringHelper::mb_ucwords($words, self::encoding()) : StringHelper::mb_ucfirst($words, self::encoding());
  335. }
  336. /**
  337. * Returns given word as CamelCased.
  338. *
  339. * Converts a word like "send_email" to "SendEmail". It
  340. * will remove non alphanumeric character from the word, so
  341. * "who's online" will be converted to "WhoSOnline".
  342. * @param string $word the word to CamelCase
  343. * @return string
  344. * @see variablize()
  345. */
  346. public static function camelize($word)
  347. {
  348. return str_replace(' ', '', StringHelper::mb_ucwords(preg_replace('/[^\pL\pN]+/u', ' ', $word), self::encoding()));
  349. }
  350. /**
  351. * Converts a CamelCase name into space-separated words.
  352. * For example, 'PostTag' will be converted to 'Post Tag'.
  353. * @param string $name the string to be converted
  354. * @param bool $ucwords whether to capitalize the first letter in each word
  355. * @return string the resulting words
  356. */
  357. public static function camel2words($name, $ucwords = true)
  358. {
  359. $label = mb_strtolower(trim(str_replace([
  360. '-',
  361. '_',
  362. '.',
  363. ], ' ', preg_replace('/(?<!\p{Lu})(\p{Lu})|(\p{Lu})(?=\p{Ll})/u', ' \0', $name))), self::encoding());
  364. return $ucwords ? StringHelper::mb_ucwords($label, self::encoding()) : $label;
  365. }
  366. /**
  367. * Converts a CamelCase name into an ID in lowercase.
  368. * Words in the ID may be concatenated using the specified character (defaults to '-').
  369. * For example, 'PostTag' will be converted to 'post-tag'.
  370. * @param string $name the string to be converted
  371. * @param string $separator the character used to concatenate the words in the ID
  372. * @param bool|string $strict whether to insert a separator between two consecutive uppercase chars, defaults to false
  373. * @return string the resulting ID
  374. */
  375. public static function camel2id($name, $separator = '-', $strict = false)
  376. {
  377. $regex = $strict ? '/\p{Lu}/u' : '/(?<!\p{Lu})\p{Lu}/u';
  378. if ($separator === '_') {
  379. return mb_strtolower(trim(preg_replace($regex, '_\0', $name), '_'), self::encoding());
  380. }
  381. return mb_strtolower(trim(str_replace('_', $separator, preg_replace($regex, $separator . '\0', $name)), $separator), self::encoding());
  382. }
  383. /**
  384. * Converts an ID into a CamelCase name.
  385. * Words in the ID separated by `$separator` (defaults to '-') will be concatenated into a CamelCase name.
  386. * For example, 'post-tag' is converted to 'PostTag'.
  387. * @param string $id the ID to be converted
  388. * @param string $separator the character used to separate the words in the ID
  389. * @return string the resulting CamelCase name
  390. */
  391. public static function id2camel($id, $separator = '-')
  392. {
  393. return str_replace(' ', '', StringHelper::mb_ucwords(str_replace($separator, ' ', $id), self::encoding()));
  394. }
  395. /**
  396. * Converts any "CamelCased" into an "underscored_word".
  397. * @param string $words the word(s) to underscore
  398. * @return string
  399. */
  400. public static function underscore($words)
  401. {
  402. return mb_strtolower(preg_replace('/(?<=\\pL)(\\p{Lu})/u', '_\\1', $words), self::encoding());
  403. }
  404. /**
  405. * Returns a human-readable string from $word.
  406. * @param string $word the string to humanize
  407. * @param bool $ucAll whether to set all words to uppercase or not
  408. * @return string
  409. */
  410. public static function humanize($word, $ucAll = false)
  411. {
  412. $word = str_replace('_', ' ', preg_replace('/_id$/', '', $word));
  413. $encoding = self::encoding();
  414. return $ucAll ? StringHelper::mb_ucwords($word, $encoding) : StringHelper::mb_ucfirst($word, $encoding);
  415. }
  416. /**
  417. * Same as camelize but first char is in lowercase.
  418. *
  419. * Converts a word like "send_email" to "sendEmail". It
  420. * will remove non alphanumeric character from the word, so
  421. * "who's online" will be converted to "whoSOnline".
  422. * @param string $word to lowerCamelCase
  423. * @return string
  424. */
  425. public static function variablize($word)
  426. {
  427. $word = static::camelize($word);
  428. return mb_strtolower(mb_substr($word, 0, 1, self::encoding())) . mb_substr($word, 1, null, self::encoding());
  429. }
  430. /**
  431. * Converts a class name to its table name (pluralized) naming conventions.
  432. *
  433. * For example, converts "Person" to "people".
  434. * @param string $className the class name for getting related table_name
  435. * @return string
  436. */
  437. public static function tableize($className)
  438. {
  439. return static::pluralize(static::underscore($className));
  440. }
  441. /**
  442. * Returns a string with all spaces converted to given replacement,
  443. * non word characters removed and the rest of characters transliterated.
  444. *
  445. * If intl extension isn't available uses fallback that converts latin characters only
  446. * and removes the rest. You may customize characters map via $transliteration property
  447. * of the helper.
  448. *
  449. * @param string $string An arbitrary string to convert
  450. * @param string $replacement The replacement to use for spaces
  451. * @param bool $lowercase whether to return the string in lowercase or not. Defaults to `true`.
  452. * @return string The converted string.
  453. */
  454. public static function slug($string, $replacement = '-', $lowercase = true)
  455. {
  456. $parts = explode($replacement, static::transliterate($string));
  457. $replaced = array_map(function ($element) use ($replacement) {
  458. $element = preg_replace('/[^a-zA-Z0-9=\s—–-]+/u', '', $element);
  459. return preg_replace('/[=\s—–-]+/u', $replacement, $element);
  460. }, $parts);
  461. $string = trim(implode($replacement, $replaced), $replacement);
  462. return $lowercase ? strtolower($string) : $string;
  463. }
  464. /**
  465. * Returns transliterated version of a string.
  466. *
  467. * If intl extension isn't available uses fallback that converts latin characters only
  468. * and removes the rest. You may customize characters map via $transliteration property
  469. * of the helper.
  470. *
  471. * @param string $string input string
  472. * @param string|\Transliterator $transliterator either a [[\Transliterator]] or a string
  473. * from which a [[\Transliterator]] can be built.
  474. * @return string
  475. * @since 2.0.7 this method is public.
  476. */
  477. public static function transliterate($string, $transliterator = null)
  478. {
  479. if (static::hasIntl()) {
  480. if ($transliterator === null) {
  481. $transliterator = static::$transliterator;
  482. }
  483. return transliterator_transliterate($transliterator, $string);
  484. }
  485. return strtr($string, static::$transliteration);
  486. }
  487. /**
  488. * @return bool if intl extension is loaded
  489. */
  490. protected static function hasIntl()
  491. {
  492. return extension_loaded('intl');
  493. }
  494. /**
  495. * Converts a table name to its class name.
  496. *
  497. * For example, converts "people" to "Person".
  498. * @param string $tableName
  499. * @return string
  500. */
  501. public static function classify($tableName)
  502. {
  503. return static::camelize(static::singularize($tableName));
  504. }
  505. /**
  506. * Converts number to its ordinal English form. For example, converts 13 to 13th, 2 to 2nd ...
  507. * @param int $number the number to get its ordinal value
  508. * @return string
  509. */
  510. public static function ordinalize($number)
  511. {
  512. if (in_array($number % 100, range(11, 13))) {
  513. return $number . 'th';
  514. }
  515. switch ($number % 10) {
  516. case 1:
  517. return $number . 'st';
  518. case 2:
  519. return $number . 'nd';
  520. case 3:
  521. return $number . 'rd';
  522. default:
  523. return $number . 'th';
  524. }
  525. }
  526. /**
  527. * Converts a list of words into a sentence.
  528. *
  529. * Special treatment is done for the last few words. For example,
  530. *
  531. * ```php
  532. * $words = ['Spain', 'France'];
  533. * echo Inflector::sentence($words);
  534. * // output: Spain and France
  535. *
  536. * $words = ['Spain', 'France', 'Italy'];
  537. * echo Inflector::sentence($words);
  538. * // output: Spain, France and Italy
  539. *
  540. * $words = ['Spain', 'France', 'Italy'];
  541. * echo Inflector::sentence($words, ' & ');
  542. * // output: Spain, France & Italy
  543. * ```
  544. *
  545. * @param array $words the words to be converted into an string
  546. * @param string $twoWordsConnector the string connecting words when there are only two
  547. * @param string $lastWordConnector the string connecting the last two words. If this is null, it will
  548. * take the value of `$twoWordsConnector`.
  549. * @param string $connector the string connecting words other than those connected by
  550. * $lastWordConnector and $twoWordsConnector
  551. * @return string the generated sentence
  552. * @since 2.0.1
  553. */
  554. public static function sentence(array $words, $twoWordsConnector = null, $lastWordConnector = null, $connector = ', ')
  555. {
  556. if ($twoWordsConnector === null) {
  557. $twoWordsConnector = Yii::t('yii', ' and ');
  558. }
  559. if ($lastWordConnector === null) {
  560. $lastWordConnector = $twoWordsConnector;
  561. }
  562. switch (count($words)) {
  563. case 0:
  564. return '';
  565. case 1:
  566. return reset($words);
  567. case 2:
  568. return implode($twoWordsConnector, $words);
  569. default:
  570. return implode($connector, array_slice($words, 0, -1)) . $lastWordConnector . end($words);
  571. }
  572. }
  573. /**
  574. * @return string
  575. */
  576. private static function encoding()
  577. {
  578. return isset(Yii::$app) ? Yii::$app->charset : 'UTF-8';
  579. }
  580. }