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