Inflector::useIntl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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