Passed
Pull Request — master (#39)
by Alexander
01:11
created

Inflector   A

Complexity

Total Complexity 37

Size/Duplication

Total Lines 594
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 37
eloc 268
c 3
b 1
f 0
dl 0
loc 594
ccs 84
cts 84
cp 1
rs 9.44

22 Methods

Rating   Name   Duplication   Size   Complexity  
A withPluralizeRules() 0 5 1
A getSpecialRules() 0 3 1
A withTransliterationMap() 0 5 1
A withTransliterator() 0 5 1
A withSpecialRules() 0 5 1
A getPluralizeRules() 0 3 1
A withoutIntl() 0 5 1
A withSingularizeRules() 0 5 1
A getSingularizeRules() 0 3 1
A toSentence() 0 5 2
A toCamelCase() 0 5 1
A toSlug() 0 8 3
A toWords() 0 7 1
A pascalCaseToId() 0 12 3
A tableToClass() 0 3 1
A toPascalCase() 0 3 1
A classToTable() 0 3 1
A toHumanReadable() 0 5 2
A toTransliterated() 0 12 3
A toPlural() 0 12 4
A toSingular() 0 13 4
A useIntl() 0 3 2
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 toTransliterated()
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 toTransliterated()
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 toTransliterated()
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 toTransliterated()} 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 toTransliterated()} 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 toTransliterated()} when intl is available.
352
     * Defaults to {@see TRANSLITERATE_LOOSE}.
353
     * @return self
354
     *@see https://secure.php.net/manual/en/transliterator.transliterate.php
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 toTransliterated()} 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 toTransliterated()}.
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 toPlural(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 toSingular(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 PascalCase 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 toSentence(string $input, bool $uppercaseAll = false): string
435
    {
436 1
        $input = $this->toHumanReadable($this->pascalCaseToId($input, '_'), $uppercaseAll);
437
438 1
        return $uppercaseAll ? StringHelper::uppercaseFirstCharacterInEachWord($input) : StringHelper::uppercaseFirstCharacter($input);
439
    }
440
441
    /**
442
     * Converts a string into space-separated words.
443
     * For example, 'PostTag' will be converted to 'Post Tag'.
444
     * @param string $input The string to be converted.
445
     * @return string The resulting words.
446
     */
447 1
    public function toWords(string $input): string
448
    {
449 1
        return mb_strtolower(trim(str_replace([
450 1
            '-',
451
            '_',
452
            '.',
453 1
        ], ' ', preg_replace('/(?<!\p{Lu})(\p{Lu})|(\p{Lu})(?=\p{Ll})/u', ' \0', $input))));
454
    }
455
456
    /**
457
     * Converts a PascalCase name into an ID in lowercase.
458
     * Words in the ID may be concatenated using the specified character (defaults to '-').
459
     * For example, 'PostTag' will be converted to 'post-tag'.
460
     * @param string $input The string to be converted.
461
     * @param string $separator The character used to concatenate the words in the ID.
462
     * @param bool $strict Whether to insert a separator between two consecutive uppercase chars, defaults to false.
463
     * @return string The resulting ID.
464
     */
465 29
    public function pascalCaseToId(string $input, string $separator = '-', bool $strict = false): string
466
    {
467 29
        $regex = $strict
468 11
            ? '/(?<=\p{L})(\p{Lu})/u'
469 29
            : '/(?<=\p{L})(?<!\p{Lu})(\p{Lu})/u';
470 29
        $result = preg_replace($regex, addslashes($separator) . '\1', $input);
471
472 29
        if ($separator !== '_') {
473 14
            $result = str_replace('_', $separator, $result);
474
        }
475
476 29
        return mb_strtolower(trim($result, $separator));
477
    }
478
479
    /**
480
     * Returns given word as PascalCased.
481
     *
482
     * Converts a word like "send_email" to "SendEmail". It
483
     * will remove non alphanumeric character from the word, so
484
     * "who's online" will be converted to "WhoSOnline".
485
     * @param string $input The word to PascalCase.
486
     * @return string PascalCased string.
487
     * @see toCamelCase()
488
     */
489 3
    public function toPascalCase(string $input): string
490
    {
491 3
        return str_replace(' ', '', StringHelper::uppercaseFirstCharacterInEachWord(preg_replace('/[^\pL\pN]+/u', ' ', $input)));
492
    }
493
494
    /**
495
     * Returns a human-readable string.
496
     * @param string $input The string to humanize.
497
     * @param bool $uppercaseWords Whether to set all words to uppercase or not.
498
     * @return string
499
     */
500 2
    public function toHumanReadable(string $input, bool $uppercaseWords = false): string
501
    {
502 2
        $input = str_replace('_', ' ', preg_replace('/_id$/', '', $input));
503
504 2
        return $uppercaseWords ? StringHelper::uppercaseFirstCharacterInEachWord($input) : StringHelper::uppercaseFirstCharacter($input);
505
    }
506
507
    /**
508
     * Returns given word as camelCased.
509
     *
510
     * Converts a word like "send_email" to "sendEmail". It
511
     * will remove non alphanumeric character from the word, so
512
     * "who's online" will be converted to "whoSOnline".
513
     * @param string $input The word to convert.
514
     * @return string
515
     */
516 1
    public function toCamelCase(string $input): string
517
    {
518 1
        $input = $this->toPascalCase($input);
519
520 1
        return mb_strtolower(mb_substr($input, 0, 1)) . mb_substr($input, 1, null);
521
    }
522
523
    /**
524
     * Converts a class name to its table name (pluralized) naming conventions.
525
     *
526
     * For example, converts "Person" to "people".
527
     * @param string $className the class name for getting related table_name.
528
     * @return string
529
     */
530 1
    public function classToTable(string $className): string
531
    {
532 1
        return $this->toPlural($this->pascalCaseToId($className, '_'));
533
    }
534
535
    /**
536
     * Converts a table name to its class name.
537
     *
538
     * For example, converts "people" to "Person".
539
     * @param string $tableName
540
     * @return string
541
     */
542 1
    public function tableToClass(string $tableName): string
543
    {
544 1
        return $this->toPascalCase($this->toSingular($tableName));
545
    }
546
547
    /**
548
     * Returns a string with all spaces converted to given replacement,
549
     * non word characters removed and the rest of characters transliterated.
550
     *
551
     * If intl extension isn't available uses fallback that converts latin characters only
552
     * and removes the rest. You may customize characters map via $transliteration property
553
     * of the helper.
554
     *
555
     * @param string $input An arbitrary string to convert.
556
     * @param string $replacement The replacement to use for spaces.
557
     * @param bool $lowercase whether to return the string in lowercase or not. Defaults to `true`.
558
     * @return string The converted string.
559
     */
560 19
    public function toSlug(string $input, string $replacement = '-', bool $lowercase = true): string
561
    {
562
        // replace all non words character
563 19
        $input = preg_replace('/[^a-zA-Z0-9]++/u', $replacement, $this->toTransliterated($input));
564
        // remove first and last replacements
565 19
        $input = preg_replace('/^(?:' . preg_quote($replacement, '/') . ')++|(?:' . preg_quote($replacement, '/') . ')++$/u' . ($lowercase ? 'i' : ''), '', $input);
566
567 19
        return $lowercase ? strtolower($input) : $input;
568
    }
569
570
    /**
571
     * Returns transliterated version of a string.
572
     *
573
     * If intl extension isn't available uses fallback that converts latin characters only
574
     * and removes the rest. You may customize characters map via $transliteration property
575
     * of the helper.
576
     *
577
     * @noinspection PhpComposerExtensionStubsInspection
578
     *
579
     * @param string $input Input string.
580
     * @param string|\Transliterator|null $transliterator either a {@see \Transliterator} or a string
581
     * from which a {@see \Transliterator} can be built. If null, value set with {@see withTransliterator()}
582
     * or {@see TRANSLITERATE_LOOSE} is used.
583
     * @return string
584
     */
585 24
    public function toTransliterated(string $input, $transliterator = null): string
586
    {
587 24
        if ($this->useIntl()) {
588 22
            if ($transliterator === null) {
589 19
                $transliterator = $this->transliterator;
590
            }
591
592
            /* @noinspection PhpComposerExtensionStubsInspection */
593 22
            return transliterator_transliterate($transliterator, $input);
594
        }
595
596 19
        return strtr($input, $this->transliterationMap);
597
    }
598
599
    /**
600
     * @return bool If intl extension should be used.
601
     */
602 24
    private function useIntl(): bool
603
    {
604 24
        return $this->withoutIntl === false && \extension_loaded('intl');
605
    }
606
}
607