Passed
Push — master ( becea6...705287 )
by Alexander
02:33
created

Inflector::toPascalCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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