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://www.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. // Add a space before any uppercase letter preceded by a lowercase letter (xY => x Y)
  362. // and any uppercase letter preceded by an uppercase letter and followed by a lowercase letter (XYz => X Yz)
  363. $label = preg_replace('/(?<=\p{Ll})\p{Lu}|(?<=[\p{L}\d])\p{Lu}(?=\p{Ll})|(\d+)/u', ' \0', $name);
  364. $label = mb_strtolower(trim(str_replace(['-', '_', '.'], ' ', $label)), self::encoding());
  365. return $ucwords ? StringHelper::mb_ucwords($label, self::encoding()) : $label;
  366. }
  367. /**
  368. * Converts a CamelCase name into an ID in lowercase.
  369. * Words in the ID may be concatenated using the specified character (defaults to '-').
  370. * For example, 'PostTag' will be converted to 'post-tag'.
  371. * @param string $name the string to be converted
  372. * @param string $separator the character used to concatenate the words in the ID
  373. * @param bool|string $strict whether to insert a separator between two consecutive uppercase chars, defaults to false
  374. * @return string the resulting ID
  375. */
  376. public static function camel2id($name, $separator = '-', $strict = false)
  377. {
  378. $regex = $strict ? '/\p{Lu}/u' : '/(?<!\p{Lu})\p{Lu}/u';
  379. if ($separator === '_') {
  380. return mb_strtolower(trim(preg_replace($regex, '_\0', $name), '_'), self::encoding());
  381. }
  382. return mb_strtolower(trim(str_replace('_', $separator, preg_replace($regex, $separator . '\0', $name)), $separator), self::encoding());
  383. }
  384. /**
  385. * Converts an ID into a CamelCase name.
  386. * Words in the ID separated by `$separator` (defaults to '-') will be concatenated into a CamelCase name.
  387. * For example, 'post-tag' is converted to 'PostTag'.
  388. * @param string $id the ID to be converted
  389. * @param string $separator the character used to separate the words in the ID
  390. * @return string the resulting CamelCase name
  391. */
  392. public static function id2camel($id, $separator = '-')
  393. {
  394. return str_replace(' ', '', StringHelper::mb_ucwords(str_replace($separator, ' ', $id), self::encoding()));
  395. }
  396. /**
  397. * Converts any "CamelCased" into an "underscored_word".
  398. * @param string $words the word(s) to underscore
  399. * @return string
  400. */
  401. public static function underscore($words)
  402. {
  403. return mb_strtolower(preg_replace('/(?<=\\pL)(\\p{Lu})/u', '_\\1', $words), self::encoding());
  404. }
  405. /**
  406. * Returns a human-readable string from $word.
  407. * @param string $word the string to humanize
  408. * @param bool $ucAll whether to set all words to uppercase or not
  409. * @return string
  410. */
  411. public static function humanize($word, $ucAll = false)
  412. {
  413. $word = str_replace('_', ' ', preg_replace('/_id$/', '', $word));
  414. $encoding = self::encoding();
  415. return $ucAll ? StringHelper::mb_ucwords($word, $encoding) : StringHelper::mb_ucfirst($word, $encoding);
  416. }
  417. /**
  418. * Same as camelize but first char is in lowercase.
  419. *
  420. * Converts a word like "send_email" to "sendEmail". It
  421. * will remove non alphanumeric character from the word, so
  422. * "who's online" will be converted to "whoSOnline".
  423. * @param string $word to lowerCamelCase
  424. * @return string
  425. */
  426. public static function variablize($word)
  427. {
  428. $word = static::camelize($word);
  429. return mb_strtolower(mb_substr($word, 0, 1, self::encoding())) . mb_substr($word, 1, null, self::encoding());
  430. }
  431. /**
  432. * Converts a class name to its table name (pluralized) naming conventions.
  433. *
  434. * For example, converts "Person" to "people".
  435. * @param string $className the class name for getting related table_name
  436. * @return string
  437. */
  438. public static function tableize($className)
  439. {
  440. return static::pluralize(static::underscore($className));
  441. }
  442. /**
  443. * Returns a string with all spaces converted to given replacement,
  444. * non word characters removed and the rest of characters transliterated.
  445. *
  446. * If intl extension isn't available uses fallback that converts latin characters only
  447. * and removes the rest. You may customize characters map via $transliteration property
  448. * of the helper.
  449. *
  450. * @param string $string An arbitrary string to convert
  451. * @param string $replacement The replacement to use for spaces
  452. * @param bool $lowercase whether to return the string in lowercase or not. Defaults to `true`.
  453. * @return string The converted string.
  454. */
  455. public static function slug($string, $replacement = '-', $lowercase = true)
  456. {
  457. if ((string)$replacement !== '') {
  458. $parts = explode($replacement, static::transliterate($string));
  459. } else {
  460. $parts = [static::transliterate($string)];
  461. }
  462. $replaced = array_map(function ($element) use ($replacement) {
  463. $element = preg_replace('/[^a-zA-Z0-9=\s—–-]+/u', '', $element);
  464. return preg_replace('/[=\s—–-]+/u', $replacement, $element);
  465. }, $parts);
  466. $string = trim(implode($replacement, $replaced), $replacement);
  467. if ((string)$replacement !== '') {
  468. $string = preg_replace('#' . preg_quote($replacement) . '+#', $replacement, $string);
  469. }
  470. return $lowercase ? strtolower($string) : $string;
  471. }
  472. /**
  473. * Returns transliterated version of a string.
  474. *
  475. * If intl extension isn't available uses fallback that converts latin characters only
  476. * and removes the rest. You may customize characters map via $transliteration property
  477. * of the helper.
  478. *
  479. * @param string $string input string
  480. * @param string|\Transliterator $transliterator either a [[\Transliterator]] or a string
  481. * from which a [[\Transliterator]] can be built.
  482. * @return string
  483. * @since 2.0.7 this method is public.
  484. */
  485. public static function transliterate($string, $transliterator = null)
  486. {
  487. if (static::hasIntl()) {
  488. if ($transliterator === null) {
  489. $transliterator = static::$transliterator;
  490. }
  491. return transliterator_transliterate($transliterator, $string);
  492. }
  493. return strtr($string, static::$transliteration);
  494. }
  495. /**
  496. * @return bool if intl extension is loaded
  497. */
  498. protected static function hasIntl()
  499. {
  500. return extension_loaded('intl');
  501. }
  502. /**
  503. * Converts a table name to its class name.
  504. *
  505. * For example, converts "people" to "Person".
  506. * @param string $tableName
  507. * @return string
  508. */
  509. public static function classify($tableName)
  510. {
  511. return static::camelize(static::singularize($tableName));
  512. }
  513. /**
  514. * Converts number to its ordinal English form. For example, converts 13 to 13th, 2 to 2nd ...
  515. * @param int $number the number to get its ordinal value
  516. * @return string
  517. */
  518. public static function ordinalize($number)
  519. {
  520. if (in_array($number % 100, range(11, 13))) {
  521. return $number . 'th';
  522. }
  523. switch ($number % 10) {
  524. case 1:
  525. return $number . 'st';
  526. case 2:
  527. return $number . 'nd';
  528. case 3:
  529. return $number . 'rd';
  530. default:
  531. return $number . 'th';
  532. }
  533. }
  534. /**
  535. * Converts a list of words into a sentence.
  536. *
  537. * Special treatment is done for the last few words. For example,
  538. *
  539. * ```php
  540. * $words = ['Spain', 'France'];
  541. * echo Inflector::sentence($words);
  542. * // output: Spain and France
  543. *
  544. * $words = ['Spain', 'France', 'Italy'];
  545. * echo Inflector::sentence($words);
  546. * // output: Spain, France and Italy
  547. *
  548. * $words = ['Spain', 'France', 'Italy'];
  549. * echo Inflector::sentence($words, ' & ');
  550. * // output: Spain, France & Italy
  551. * ```
  552. *
  553. * @param array $words the words to be converted into an string
  554. * @param string $twoWordsConnector the string connecting words when there are only two
  555. * @param string $lastWordConnector the string connecting the last two words. If this is null, it will
  556. * take the value of `$twoWordsConnector`.
  557. * @param string $connector the string connecting words other than those connected by
  558. * $lastWordConnector and $twoWordsConnector
  559. * @return string the generated sentence
  560. * @since 2.0.1
  561. */
  562. public static function sentence(array $words, $twoWordsConnector = null, $lastWordConnector = null, $connector = ', ')
  563. {
  564. if ($twoWordsConnector === null) {
  565. $twoWordsConnector = Yii::t('yii', ' and ');
  566. }
  567. if ($lastWordConnector === null) {
  568. $lastWordConnector = $twoWordsConnector;
  569. }
  570. switch (count($words)) {
  571. case 0:
  572. return '';
  573. case 1:
  574. return reset($words);
  575. case 2:
  576. return implode($twoWordsConnector, $words);
  577. default:
  578. return implode($connector, array_slice($words, 0, -1)) . $lastWordConnector . end($words);
  579. }
  580. }
  581. /**
  582. * @return string
  583. */
  584. private static function encoding()
  585. {
  586. return isset(Yii::$app) ? Yii::$app->charset : 'UTF-8';
  587. }
  588. }