BaseInflector.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. 'software' => 'software',
  220. 'hardware' => 'hardware',
  221. ];
  222. /**
  223. * @var array fallback map for transliteration used by [[transliterate()]] when intl isn't available.
  224. */
  225. public static $transliteration = [
  226. 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE', 'Ç' => 'C',
  227. 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I', 'Ï' => 'I',
  228. 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ő' => 'O',
  229. 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U', 'Ý' => 'Y', 'Þ' => 'TH',
  230. 'ß' => 'ss',
  231. 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c',
  232. 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i',
  233. 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o',
  234. 'ø' => 'o', 'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th',
  235. 'ÿ' => 'y',
  236. ];
  237. /**
  238. * Shortcut for `Any-Latin; NFKD` transliteration rule.
  239. *
  240. * The rule is strict, letters will be transliterated with
  241. * the closest sound-representation chars. The result may contain any UTF-8 chars. For example:
  242. * `获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?` will be transliterated to
  243. * `huò qǔ dào dochira Ukraí̈nsʹka: g̀,ê, Srpska: đ, n̂, d̂! ¿Español?`.
  244. *
  245. * Used in [[transliterate()]].
  246. * For detailed information see [unicode normalization forms](http://unicode.org/reports/tr15/#Normalization_Forms_Table)
  247. * @see http://unicode.org/reports/tr15/#Normalization_Forms_Table
  248. * @see transliterate()
  249. * @since 2.0.7
  250. */
  251. const TRANSLITERATE_STRICT = 'Any-Latin; NFKD';
  252. /**
  253. * Shortcut for `Any-Latin; Latin-ASCII` transliteration rule.
  254. *
  255. * The rule is medium, letters will be
  256. * transliterated to characters of Latin-1 (ISO 8859-1) ASCII table. For example:
  257. * `获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?` will be transliterated to
  258. * `huo qu dao dochira Ukrainsʹka: g,e, Srpska: d, n, d! ¿Espanol?`.
  259. *
  260. * Used in [[transliterate()]].
  261. * For detailed information see [unicode normalization forms](http://unicode.org/reports/tr15/#Normalization_Forms_Table)
  262. * @see http://unicode.org/reports/tr15/#Normalization_Forms_Table
  263. * @see transliterate()
  264. * @since 2.0.7
  265. */
  266. const TRANSLITERATE_MEDIUM = 'Any-Latin; Latin-ASCII';
  267. /**
  268. * Shortcut for `Any-Latin; Latin-ASCII; [\u0080-\uffff] remove` transliteration rule.
  269. *
  270. * The rule is loose,
  271. * letters will be transliterated with the characters of Basic Latin Unicode Block.
  272. * For example:
  273. * `获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español?` will be transliterated to
  274. * `huo qu dao dochira Ukrainska: g,e, Srpska: d, n, d! Espanol?`.
  275. *
  276. * Used in [[transliterate()]].
  277. * For detailed information see [unicode normalization forms](http://unicode.org/reports/tr15/#Normalization_Forms_Table)
  278. * @see http://unicode.org/reports/tr15/#Normalization_Forms_Table
  279. * @see transliterate()
  280. * @since 2.0.7
  281. */
  282. const TRANSLITERATE_LOOSE = 'Any-Latin; Latin-ASCII; [\u0080-\uffff] remove';
  283. /**
  284. * @var mixed Either a [[\Transliterator]], or a string from which a [[\Transliterator]] can be built
  285. * for transliteration. Used by [[transliterate()]] when intl is available. Defaults to [[TRANSLITERATE_LOOSE]]
  286. * @see https://secure.php.net/manual/en/transliterator.transliterate.php
  287. */
  288. public static $transliterator = self::TRANSLITERATE_LOOSE;
  289. /**
  290. * Converts a word to its plural form.
  291. * Note that this is for English only!
  292. * For example, 'apple' will become 'apples', and 'child' will become 'children'.
  293. * @param string $word the word to be pluralized
  294. * @return string the pluralized word
  295. */
  296. public static function pluralize($word)
  297. {
  298. if (isset(static::$specials[$word])) {
  299. return static::$specials[$word];
  300. }
  301. foreach (static::$plurals as $rule => $replacement) {
  302. if (preg_match($rule, $word)) {
  303. return preg_replace($rule, $replacement, $word);
  304. }
  305. }
  306. return $word;
  307. }
  308. /**
  309. * Returns the singular of the $word.
  310. * @param string $word the english word to singularize
  311. * @return string Singular noun.
  312. */
  313. public static function singularize($word)
  314. {
  315. $result = array_search($word, static::$specials, true);
  316. if ($result !== false) {
  317. return $result;
  318. }
  319. foreach (static::$singulars as $rule => $replacement) {
  320. if (preg_match($rule, $word)) {
  321. return preg_replace($rule, $replacement, $word);
  322. }
  323. }
  324. return $word;
  325. }
  326. /**
  327. * Converts an underscored or CamelCase word into a English
  328. * sentence.
  329. * @param string $words
  330. * @param bool $ucAll whether to set all words to uppercase
  331. * @return string
  332. */
  333. public static function titleize($words, $ucAll = false)
  334. {
  335. $words = static::humanize(static::underscore($words), $ucAll);
  336. return $ucAll ? StringHelper::mb_ucwords($words, self::encoding()) : StringHelper::mb_ucfirst($words, self::encoding());
  337. }
  338. /**
  339. * Returns given word as CamelCased.
  340. *
  341. * Converts a word like "send_email" to "SendEmail". It
  342. * will remove non alphanumeric character from the word, so
  343. * "who's online" will be converted to "WhoSOnline".
  344. * @param string $word the word to CamelCase
  345. * @return string
  346. * @see variablize()
  347. */
  348. public static function camelize($word)
  349. {
  350. return str_replace(' ', '', StringHelper::mb_ucwords(preg_replace('/[^\pL\pN]+/u', ' ', $word), self::encoding()));
  351. }
  352. /**
  353. * Converts a CamelCase name into space-separated words.
  354. * For example, 'PostTag' will be converted to 'Post Tag'.
  355. * @param string $name the string to be converted
  356. * @param bool $ucwords whether to capitalize the first letter in each word
  357. * @return string the resulting words
  358. */
  359. public static function camel2words($name, $ucwords = true)
  360. {
  361. $label = mb_strtolower(trim(str_replace([
  362. '-',
  363. '_',
  364. '.',
  365. ], ' ', preg_replace('/(?<!\p{Lu})(\p{Lu})|(\p{Lu})(?=\p{Ll})/u', ' \0', $name))), self::encoding());
  366. return $ucwords ? StringHelper::mb_ucwords($label, self::encoding()) : $label;
  367. }
  368. /**
  369. * Converts a CamelCase name into an ID in lowercase.
  370. * Words in the ID may be concatenated using the specified character (defaults to '-').
  371. * For example, 'PostTag' will be converted to 'post-tag'.
  372. * @param string $name the string to be converted
  373. * @param string $separator the character used to concatenate the words in the ID
  374. * @param bool|string $strict whether to insert a separator between two consecutive uppercase chars, defaults to false
  375. * @return string the resulting ID
  376. */
  377. public static function camel2id($name, $separator = '-', $strict = false)
  378. {
  379. $regex = $strict ? '/\p{Lu}/u' : '/(?<!\p{Lu})\p{Lu}/u';
  380. if ($separator === '_') {
  381. return mb_strtolower(trim(preg_replace($regex, '_\0', $name), '_'), self::encoding());
  382. }
  383. return mb_strtolower(trim(str_replace('_', $separator, preg_replace($regex, $separator . '\0', $name)), $separator), self::encoding());
  384. }
  385. /**
  386. * Converts an ID into a CamelCase name.
  387. * Words in the ID separated by `$separator` (defaults to '-') will be concatenated into a CamelCase name.
  388. * For example, 'post-tag' is converted to 'PostTag'.
  389. * @param string $id the ID to be converted
  390. * @param string $separator the character used to separate the words in the ID
  391. * @return string the resulting CamelCase name
  392. */
  393. public static function id2camel($id, $separator = '-')
  394. {
  395. return str_replace(' ', '', StringHelper::mb_ucwords(str_replace($separator, ' ', $id), self::encoding()));
  396. }
  397. /**
  398. * Converts any "CamelCased" into an "underscored_word".
  399. * @param string $words the word(s) to underscore
  400. * @return string
  401. */
  402. public static function underscore($words)
  403. {
  404. return mb_strtolower(preg_replace('/(?<=\\pL)(\\p{Lu})/u', '_\\1', $words), self::encoding());
  405. }
  406. /**
  407. * Returns a human-readable string from $word.
  408. * @param string $word the string to humanize
  409. * @param bool $ucAll whether to set all words to uppercase or not
  410. * @return string
  411. */
  412. public static function humanize($word, $ucAll = false)
  413. {
  414. $word = str_replace('_', ' ', preg_replace('/_id$/', '', $word));
  415. $encoding = self::encoding();
  416. return $ucAll ? StringHelper::mb_ucwords($word, $encoding) : StringHelper::mb_ucfirst($word, $encoding);
  417. }
  418. /**
  419. * Same as camelize but first char is in lowercase.
  420. *
  421. * Converts a word like "send_email" to "sendEmail". It
  422. * will remove non alphanumeric character from the word, so
  423. * "who's online" will be converted to "whoSOnline".
  424. * @param string $word to lowerCamelCase
  425. * @return string
  426. */
  427. public static function variablize($word)
  428. {
  429. $word = static::camelize($word);
  430. return mb_strtolower(mb_substr($word, 0, 1, self::encoding())) . mb_substr($word, 1, null, self::encoding());
  431. }
  432. /**
  433. * Converts a class name to its table name (pluralized) naming conventions.
  434. *
  435. * For example, converts "Person" to "people".
  436. * @param string $className the class name for getting related table_name
  437. * @return string
  438. */
  439. public static function tableize($className)
  440. {
  441. return static::pluralize(static::underscore($className));
  442. }
  443. /**
  444. * Returns a string with all spaces converted to given replacement,
  445. * non word characters removed and the rest of characters transliterated.
  446. *
  447. * If intl extension isn't available uses fallback that converts latin characters only
  448. * and removes the rest. You may customize characters map via $transliteration property
  449. * of the helper.
  450. *
  451. * @param string $string An arbitrary string to convert
  452. * @param string $replacement The replacement to use for spaces
  453. * @param bool $lowercase whether to return the string in lowercase or not. Defaults to `true`.
  454. * @return string The converted string.
  455. */
  456. public static function slug($string, $replacement = '-', $lowercase = true)
  457. {
  458. if ((string)$replacement !== '') {
  459. $parts = explode($replacement, static::transliterate($string));
  460. } else {
  461. $parts = [static::transliterate($string)];
  462. }
  463. $replaced = array_map(function ($element) use ($replacement) {
  464. $element = preg_replace('/[^a-zA-Z0-9=\s—–-]+/u', '', $element);
  465. return preg_replace('/[=\s—–-]+/u', $replacement, $element);
  466. }, $parts);
  467. $string = trim(implode($replacement, $replaced), $replacement);
  468. if ((string)$replacement !== '') {
  469. $string = preg_replace('#' . preg_quote($replacement) . '+#', $replacement, $string);
  470. }
  471. return $lowercase ? strtolower($string) : $string;
  472. }
  473. /**
  474. * Returns transliterated version of a string.
  475. *
  476. * If intl extension isn't available uses fallback that converts latin characters only
  477. * and removes the rest. You may customize characters map via $transliteration property
  478. * of the helper.
  479. *
  480. * @param string $string input string
  481. * @param string|\Transliterator $transliterator either a [[\Transliterator]] or a string
  482. * from which a [[\Transliterator]] can be built.
  483. * @return string
  484. * @since 2.0.7 this method is public.
  485. */
  486. public static function transliterate($string, $transliterator = null)
  487. {
  488. if (static::hasIntl()) {
  489. if ($transliterator === null) {
  490. $transliterator = static::$transliterator;
  491. }
  492. return transliterator_transliterate($transliterator, $string);
  493. }
  494. return strtr($string, static::$transliteration);
  495. }
  496. /**
  497. * @return bool if intl extension is loaded
  498. */
  499. protected static function hasIntl()
  500. {
  501. return extension_loaded('intl');
  502. }
  503. /**
  504. * Converts a table name to its class name.
  505. *
  506. * For example, converts "people" to "Person".
  507. * @param string $tableName
  508. * @return string
  509. */
  510. public static function classify($tableName)
  511. {
  512. return static::camelize(static::singularize($tableName));
  513. }
  514. /**
  515. * Converts number to its ordinal English form. For example, converts 13 to 13th, 2 to 2nd ...
  516. * @param int $number the number to get its ordinal value
  517. * @return string
  518. */
  519. public static function ordinalize($number)
  520. {
  521. if (in_array($number % 100, range(11, 13))) {
  522. return $number . 'th';
  523. }
  524. switch ($number % 10) {
  525. case 1:
  526. return $number . 'st';
  527. case 2:
  528. return $number . 'nd';
  529. case 3:
  530. return $number . 'rd';
  531. default:
  532. return $number . 'th';
  533. }
  534. }
  535. /**
  536. * Converts a list of words into a sentence.
  537. *
  538. * Special treatment is done for the last few words. For example,
  539. *
  540. * ```php
  541. * $words = ['Spain', 'France'];
  542. * echo Inflector::sentence($words);
  543. * // output: Spain and France
  544. *
  545. * $words = ['Spain', 'France', 'Italy'];
  546. * echo Inflector::sentence($words);
  547. * // output: Spain, France and Italy
  548. *
  549. * $words = ['Spain', 'France', 'Italy'];
  550. * echo Inflector::sentence($words, ' & ');
  551. * // output: Spain, France & Italy
  552. * ```
  553. *
  554. * @param array $words the words to be converted into an string
  555. * @param string $twoWordsConnector the string connecting words when there are only two
  556. * @param string $lastWordConnector the string connecting the last two words. If this is null, it will
  557. * take the value of `$twoWordsConnector`.
  558. * @param string $connector the string connecting words other than those connected by
  559. * $lastWordConnector and $twoWordsConnector
  560. * @return string the generated sentence
  561. * @since 2.0.1
  562. */
  563. public static function sentence(array $words, $twoWordsConnector = null, $lastWordConnector = null, $connector = ', ')
  564. {
  565. if ($twoWordsConnector === null) {
  566. $twoWordsConnector = Yii::t('yii', ' and ');
  567. }
  568. if ($lastWordConnector === null) {
  569. $lastWordConnector = $twoWordsConnector;
  570. }
  571. switch (count($words)) {
  572. case 0:
  573. return '';
  574. case 1:
  575. return reset($words);
  576. case 2:
  577. return implode($twoWordsConnector, $words);
  578. default:
  579. return implode($connector, array_slice($words, 0, -1)) . $lastWordConnector . end($words);
  580. }
  581. }
  582. /**
  583. * @return string
  584. */
  585. private static function encoding()
  586. {
  587. return isset(Yii::$app) ? Yii::$app->charset : 'UTF-8';
  588. }
  589. }