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