Completed
Push — master ( 84dee3...084275 )
by Michal
36:03
created

Inflector   C

Complexity

Total Complexity 42

Size/Duplication

Total Lines 732
Duplicated Lines 5.33 %

Coupling/Cohesion

Components 3
Dependencies 0

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 42
c 4
b 0
f 0
lcom 3
cbo 0
dl 39
loc 732
rs 5.2657

14 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 12 4
A underscore() 0 4 1
A dasherize() 0 4 1
A _cache() 0 13 3
A rules() 0 17 3
C pluralize() 18 32 8
C singularize() 21 34 8
A humanize() 0 17 3
A tableize() 0 11 2
A classify() 0 11 2
A variable() 0 13 2
A slug() 0 17 1
A camelize() 0 13 2
A delimit() 0 13 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Inflector often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Inflector, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11
 * @link          http://cakephp.org CakePHP(tm) Project
12
 * @since         0.2.9
13
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
14
 */
15
namespace Cake\Utility;
16
17
/**
18
 * Pluralize and singularize English words.
19
 *
20
 * Inflector pluralizes and singularizes English nouns.
21
 * Used by CakePHP's naming conventions throughout the framework.
22
 *
23
 * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html
24
 */
25
class Inflector
26
{
27
28
    /**
29
     * Plural inflector rules
30
     *
31
     * @var array
32
     */
33
    protected static $_plural = [
34
        '/(s)tatus$/i' => '\1tatuses',
35
        '/(quiz)$/i' => '\1zes',
36
        '/^(ox)$/i' => '\1\2en',
37
        '/([m|l])ouse$/i' => '\1ice',
38
        '/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
39
        '/(x|ch|ss|sh)$/i' => '\1es',
40
        '/([^aeiouy]|qu)y$/i' => '\1ies',
41
        '/(hive)$/i' => '\1s',
42
        '/(?:([^f])fe|([lre])f)$/i' => '\1\2ves',
43
        '/sis$/i' => 'ses',
44
        '/([ti])um$/i' => '\1a',
45
        '/(p)erson$/i' => '\1eople',
46
        '/(?<!u)(m)an$/i' => '\1en',
47
        '/(c)hild$/i' => '\1hildren',
48
        '/(buffal|tomat)o$/i' => '\1\2oes',
49
        '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin)us$/i' => '\1i',
50
        '/us$/i' => 'uses',
51
        '/(alias)$/i' => '\1es',
52
        '/(ax|cris|test)is$/i' => '\1es',
53
        '/s$/' => 's',
54
        '/^$/' => '',
55
        '/$/' => 's',
56
    ];
57
58
    /**
59
     * Singular inflector rules
60
     *
61
     * @var array
62
     */
63
    protected static $_singular = [
64
        '/(s)tatuses$/i' => '\1\2tatus',
65
        '/^(.*)(menu)s$/i' => '\1\2',
66
        '/(quiz)zes$/i' => '\\1',
67
        '/(matr)ices$/i' => '\1ix',
68
        '/(vert|ind)ices$/i' => '\1ex',
69
        '/^(ox)en/i' => '\1',
70
        '/(alias)(es)*$/i' => '\1',
71
        '/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
72
        '/([ftw]ax)es/i' => '\1',
73
        '/(cris|ax|test)es$/i' => '\1is',
74
        '/(shoe)s$/i' => '\1',
75
        '/(o)es$/i' => '\1',
76
        '/ouses$/' => 'ouse',
77
        '/([^a])uses$/' => '\1us',
78
        '/([m|l])ice$/i' => '\1ouse',
79
        '/(x|ch|ss|sh)es$/i' => '\1',
80
        '/(m)ovies$/i' => '\1\2ovie',
81
        '/(s)eries$/i' => '\1\2eries',
82
        '/([^aeiouy]|qu)ies$/i' => '\1y',
83
        '/(tive)s$/i' => '\1',
84
        '/(hive)s$/i' => '\1',
85
        '/(drive)s$/i' => '\1',
86
        '/([le])ves$/i' => '\1f',
87
        '/([^rfoa])ves$/i' => '\1fe',
88
        '/(^analy)ses$/i' => '\1sis',
89
        '/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
90
        '/([ti])a$/i' => '\1um',
91
        '/(p)eople$/i' => '\1\2erson',
92
        '/(m)en$/i' => '\1an',
93
        '/(c)hildren$/i' => '\1\2hild',
94
        '/(n)ews$/i' => '\1\2ews',
95
        '/eaus$/' => 'eau',
96
        '/^(.*us)$/' => '\\1',
97
        '/s$/i' => ''
98
    ];
99
100
    /**
101
     * Irregular rules
102
     *
103
     * @var array
104
     */
105
    protected static $_irregular = [
106
        'atlas' => 'atlases',
107
        'beef' => 'beefs',
108
        'brief' => 'briefs',
109
        'brother' => 'brothers',
110
        'cafe' => 'cafes',
111
        'child' => 'children',
112
        'cookie' => 'cookies',
113
        'corpus' => 'corpuses',
114
        'cow' => 'cows',
115
        'criterion' => 'criteria',
116
        'ganglion' => 'ganglions',
117
        'genie' => 'genies',
118
        'genus' => 'genera',
119
        'graffito' => 'graffiti',
120
        'hoof' => 'hoofs',
121
        'loaf' => 'loaves',
122
        'man' => 'men',
123
        'money' => 'monies',
124
        'mongoose' => 'mongooses',
125
        'move' => 'moves',
126
        'mythos' => 'mythoi',
127
        'niche' => 'niches',
128
        'numen' => 'numina',
129
        'occiput' => 'occiputs',
130
        'octopus' => 'octopuses',
131
        'opus' => 'opuses',
132
        'ox' => 'oxen',
133
        'penis' => 'penises',
134
        'person' => 'people',
135
        'sex' => 'sexes',
136
        'soliloquy' => 'soliloquies',
137
        'testis' => 'testes',
138
        'trilby' => 'trilbys',
139
        'turf' => 'turfs',
140
        'potato' => 'potatoes',
141
        'hero' => 'heroes',
142
        'tooth' => 'teeth',
143
        'goose' => 'geese',
144
        'foot' => 'feet',
145
        'foe' => 'foes',
146
        'sieve' => 'sieves'
147
    ];
148
149
    /**
150
     * Words that should not be inflected
151
     *
152
     * @var array
153
     */
154
    protected static $_uninflected = [
155
        '.*[nrlm]ese', '.*data', '.*deer', '.*fish', '.*measles', '.*ois',
156
        '.*pox', '.*sheep', 'people', 'feedback', 'stadia', '.*?media',
157
        'chassis', 'clippers', 'debris', 'diabetes', 'equipment', 'gallows',
158
        'graffiti', 'headquarters', 'information', 'innings', 'news', 'nexus',
159
        'proceedings', 'research', 'sea[- ]bass', 'series', 'species', 'weather'
160
    ];
161
162
    /**
163
     * Default map of accented and special characters to ASCII characters
164
     *
165
     * @var array
166
     */
167
    protected static $_transliteration = [
168
        'ä' => 'ae',
169
        'æ' => 'ae',
170
        'ǽ' => 'ae',
171
        'ö' => 'oe',
172
        'œ' => 'oe',
173
        'ü' => 'ue',
174
        'Ä' => 'Ae',
175
        'Ü' => 'Ue',
176
        'Ö' => 'Oe',
177
        'À' => 'A',
178
        'Á' => 'A',
179
        'Â' => 'A',
180
        'Ã' => 'A',
181
        'Å' => 'A',
182
        'Ǻ' => 'A',
183
        'Ā' => 'A',
184
        'Ă' => 'A',
185
        'Ą' => 'A',
186
        'Ǎ' => 'A',
187
        'à' => 'a',
188
        'á' => 'a',
189
        'â' => 'a',
190
        'ã' => 'a',
191
        'å' => 'a',
192
        'ǻ' => 'a',
193
        'ā' => 'a',
194
        'ă' => 'a',
195
        'ą' => 'a',
196
        'ǎ' => 'a',
197
        'ª' => 'a',
198
        'Ç' => 'C',
199
        'Ć' => 'C',
200
        'Ĉ' => 'C',
201
        'Ċ' => 'C',
202
        'Č' => 'C',
203
        'ç' => 'c',
204
        'ć' => 'c',
205
        'ĉ' => 'c',
206
        'ċ' => 'c',
207
        'č' => 'c',
208
        'Ð' => 'D',
209
        'Ď' => 'D',
210
        'Đ' => 'D',
211
        'ð' => 'd',
212
        'ď' => 'd',
213
        'đ' => 'd',
214
        'È' => 'E',
215
        'É' => 'E',
216
        'Ê' => 'E',
217
        'Ë' => 'E',
218
        'Ē' => 'E',
219
        'Ĕ' => 'E',
220
        'Ė' => 'E',
221
        'Ę' => 'E',
222
        'Ě' => 'E',
223
        'è' => 'e',
224
        'é' => 'e',
225
        'ê' => 'e',
226
        'ë' => 'e',
227
        'ē' => 'e',
228
        'ĕ' => 'e',
229
        'ė' => 'e',
230
        'ę' => 'e',
231
        'ě' => 'e',
232
        'Ĝ' => 'G',
233
        'Ğ' => 'G',
234
        'Ġ' => 'G',
235
        'Ģ' => 'G',
236
        'Ґ' => 'G',
237
        'ĝ' => 'g',
238
        'ğ' => 'g',
239
        'ġ' => 'g',
240
        'ģ' => 'g',
241
        'ґ' => 'g',
242
        'Ĥ' => 'H',
243
        'Ħ' => 'H',
244
        'ĥ' => 'h',
245
        'ħ' => 'h',
246
        'І' => 'I',
247
        'Ì' => 'I',
248
        'Í' => 'I',
249
        'Î' => 'I',
250
        'Ї' => 'Yi',
251
        'Ï' => 'I',
252
        'Ĩ' => 'I',
253
        'Ī' => 'I',
254
        'Ĭ' => 'I',
255
        'Ǐ' => 'I',
256
        'Į' => 'I',
257
        'İ' => 'I',
258
        'і' => 'i',
259
        'ì' => 'i',
260
        'í' => 'i',
261
        'î' => 'i',
262
        'ï' => 'i',
263
        'ї' => 'yi',
264
        'ĩ' => 'i',
265
        'ī' => 'i',
266
        'ĭ' => 'i',
267
        'ǐ' => 'i',
268
        'į' => 'i',
269
        'ı' => 'i',
270
        'Ĵ' => 'J',
271
        'ĵ' => 'j',
272
        'Ķ' => 'K',
273
        'ķ' => 'k',
274
        'Ĺ' => 'L',
275
        'Ļ' => 'L',
276
        'Ľ' => 'L',
277
        'Ŀ' => 'L',
278
        'Ł' => 'L',
279
        'ĺ' => 'l',
280
        'ļ' => 'l',
281
        'ľ' => 'l',
282
        'ŀ' => 'l',
283
        'ł' => 'l',
284
        'Ñ' => 'N',
285
        'Ń' => 'N',
286
        'Ņ' => 'N',
287
        'Ň' => 'N',
288
        'ñ' => 'n',
289
        'ń' => 'n',
290
        'ņ' => 'n',
291
        'ň' => 'n',
292
        'ʼn' => 'n',
293
        'Ò' => 'O',
294
        'Ó' => 'O',
295
        'Ô' => 'O',
296
        'Õ' => 'O',
297
        'Ō' => 'O',
298
        'Ŏ' => 'O',
299
        'Ǒ' => 'O',
300
        'Ő' => 'O',
301
        'Ơ' => 'O',
302
        'Ø' => 'O',
303
        'Ǿ' => 'O',
304
        'ò' => 'o',
305
        'ó' => 'o',
306
        'ô' => 'o',
307
        'õ' => 'o',
308
        'ō' => 'o',
309
        'ŏ' => 'o',
310
        'ǒ' => 'o',
311
        'ő' => 'o',
312
        'ơ' => 'o',
313
        'ø' => 'o',
314
        'ǿ' => 'o',
315
        'º' => 'o',
316
        'Ŕ' => 'R',
317
        'Ŗ' => 'R',
318
        'Ř' => 'R',
319
        'ŕ' => 'r',
320
        'ŗ' => 'r',
321
        'ř' => 'r',
322
        'Ś' => 'S',
323
        'Ŝ' => 'S',
324
        'Ş' => 'S',
325
        'Ș' => 'S',
326
        'Š' => 'S',
327
        'ẞ' => 'SS',
328
        'ś' => 's',
329
        'ŝ' => 's',
330
        'ş' => 's',
331
        'ș' => 's',
332
        'š' => 's',
333
        'ſ' => 's',
334
        'Ţ' => 'T',
335
        'Ț' => 'T',
336
        'Ť' => 'T',
337
        'Ŧ' => 'T',
338
        'ţ' => 't',
339
        'ț' => 't',
340
        'ť' => 't',
341
        'ŧ' => 't',
342
        'Ù' => 'U',
343
        'Ú' => 'U',
344
        'Û' => 'U',
345
        'Ũ' => 'U',
346
        'Ū' => 'U',
347
        'Ŭ' => 'U',
348
        'Ů' => 'U',
349
        'Ű' => 'U',
350
        'Ų' => 'U',
351
        'Ư' => 'U',
352
        'Ǔ' => 'U',
353
        'Ǖ' => 'U',
354
        'Ǘ' => 'U',
355
        'Ǚ' => 'U',
356
        'Ǜ' => 'U',
357
        'ù' => 'u',
358
        'ú' => 'u',
359
        'û' => 'u',
360
        'ũ' => 'u',
361
        'ū' => 'u',
362
        'ŭ' => 'u',
363
        'ů' => 'u',
364
        'ű' => 'u',
365
        'ų' => 'u',
366
        'ư' => 'u',
367
        'ǔ' => 'u',
368
        'ǖ' => 'u',
369
        'ǘ' => 'u',
370
        'ǚ' => 'u',
371
        'ǜ' => 'u',
372
        'Ý' => 'Y',
373
        'Ÿ' => 'Y',
374
        'Ŷ' => 'Y',
375
        'ý' => 'y',
376
        'ÿ' => 'y',
377
        'ŷ' => 'y',
378
        'Ŵ' => 'W',
379
        'ŵ' => 'w',
380
        'Ź' => 'Z',
381
        'Ż' => 'Z',
382
        'Ž' => 'Z',
383
        'ź' => 'z',
384
        'ż' => 'z',
385
        'ž' => 'z',
386
        'Æ' => 'AE',
387
        'Ǽ' => 'AE',
388
        'ß' => 'ss',
389
        'IJ' => 'IJ',
390
        'ij' => 'ij',
391
        'Œ' => 'OE',
392
        'ƒ' => 'f',
393
        'Þ' => 'TH',
394
        'þ' => 'th',
395
        'Є' => 'Ye',
396
        'є' => 'ye',
397
    ];
398
399
    /**
400
     * Method cache array.
401
     *
402
     * @var array
403
     */
404
    protected static $_cache = [];
405
406
    /**
407
     * The initial state of Inflector so reset() works.
408
     *
409
     * @var array
410
     */
411
    protected static $_initialState = [];
412
413
    /**
414
     * Cache inflected values, and return if already available
415
     *
416
     * @param string $type Inflection type
417
     * @param string $key Original value
418
     * @param string|bool $value Inflected value
419
     * @return string|bool Inflected value on cache hit or false on cache miss.
420
     */
421
    protected static function _cache($type, $key, $value = false)
422
    {
423
        $key = '_' . $key;
424
        $type = '_' . $type;
425
        if ($value !== false) {
426
            static::$_cache[$type][$key] = $value;
427
            return $value;
428
        }
429
        if (!isset(static::$_cache[$type][$key])) {
430
            return false;
431
        }
432
        return static::$_cache[$type][$key];
433
    }
434
435
    /**
436
     * Clears Inflectors inflected value caches. And resets the inflection
437
     * rules to the initial values.
438
     *
439
     * @return void
440
     */
441
    public static function reset()
442
    {
443
        if (empty(static::$_initialState)) {
444
            static::$_initialState = get_class_vars(__CLASS__);
445
            return;
446
        }
447
        foreach (static::$_initialState as $key => $val) {
448
            if ($key !== '_initialState') {
449
                static::${$key} = $val;
450
            }
451
        }
452
    }
453
454
    /**
455
     * Adds custom inflection $rules, of either 'plural', 'singular',
456
     * 'uninflected', 'irregular' or 'transliteration' $type.
457
     *
458
     * ### Usage:
459
     *
460
     * ```
461
     * Inflector::rules('plural', ['/^(inflect)or$/i' => '\1ables']);
462
     * Inflector::rules('irregular', ['red' => 'redlings']);
463
     * Inflector::rules('uninflected', ['dontinflectme']);
464
     * Inflector::rules('transliteration', ['/å/' => 'aa']);
465
     * ```
466
     *
467
     * @param string $type The type of inflection, either 'plural', 'singular',
468
     *   'uninflected' or 'transliteration'.
469
     * @param array $rules Array of rules to be added.
470
     * @param bool $reset If true, will unset default inflections for all
471
     *        new rules that are being defined in $rules.
472
     * @return void
473
     */
474
    public static function rules($type, $rules, $reset = false)
475
    {
476
        $var = '_' . $type;
477
478
        if ($reset) {
479
            static::${$var} = $rules;
480
        } elseif ($type === 'uninflected') {
481
            static::$_uninflected = array_merge(
482
                $rules,
483
                static::$_uninflected
484
            );
485
        } else {
486
            static::${$var} = $rules + static::${$var};
487
        }
488
489
        static::$_cache = [];
490
    }
491
492
    /**
493
     * Return $word in plural form.
494
     *
495
     * @param string $word Word in singular
496
     * @return string Word in plural
497
     * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-plural-singular-forms
498
     */
499
    public static function pluralize($word)
500
    {
501
        if (isset(static::$_cache['pluralize'][$word])) {
502
            return static::$_cache['pluralize'][$word];
503
        }
504
505
        if (!isset(static::$_cache['irregular']['pluralize'])) {
506
            static::$_cache['irregular']['pluralize'] = '(?:' . implode('|', array_keys(static::$_irregular)) . ')';
507
        }
508
509 View Code Duplication
        if (preg_match('/(.*?(?:\\b|_))(' . static::$_cache['irregular']['pluralize'] . ')$/i', $word, $regs)) {
510
            static::$_cache['pluralize'][$word] = $regs[1] . substr($regs[2], 0, 1) .
511
                substr(static::$_irregular[strtolower($regs[2])], 1);
512
            return static::$_cache['pluralize'][$word];
513
        }
514
515 View Code Duplication
        if (!isset(static::$_cache['uninflected'])) {
516
            static::$_cache['uninflected'] = '(?:' . implode('|', static::$_uninflected) . ')';
517
        }
518
519 View Code Duplication
        if (preg_match('/^(' . static::$_cache['uninflected'] . ')$/i', $word, $regs)) {
520
            static::$_cache['pluralize'][$word] = $word;
521
            return $word;
522
        }
523
524 View Code Duplication
        foreach (static::$_plural as $rule => $replacement) {
525
            if (preg_match($rule, $word)) {
526
                static::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
527
                return static::$_cache['pluralize'][$word];
528
            }
529
        }
530
    }
531
532
    /**
533
     * Return $word in singular form.
534
     *
535
     * @param string $word Word in plural
536
     * @return string Word in singular
537
     * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-plural-singular-forms
538
     */
539
    public static function singularize($word)
540
    {
541
        if (isset(static::$_cache['singularize'][$word])) {
542
            return static::$_cache['singularize'][$word];
543
        }
544
545 View Code Duplication
        if (!isset(static::$_cache['irregular']['singular'])) {
546
            static::$_cache['irregular']['singular'] = '(?:' . implode('|', static::$_irregular) . ')';
547
        }
548
549 View Code Duplication
        if (preg_match('/(.*?(?:\\b|_))(' . static::$_cache['irregular']['singular'] . ')$/i', $word, $regs)) {
550
            static::$_cache['singularize'][$word] = $regs[1] . substr($regs[2], 0, 1) .
551
                substr(array_search(strtolower($regs[2]), static::$_irregular), 1);
552
            return static::$_cache['singularize'][$word];
553
        }
554
555 View Code Duplication
        if (!isset(static::$_cache['uninflected'])) {
556
            static::$_cache['uninflected'] = '(?:' . implode('|', static::$_uninflected) . ')';
557
        }
558
559 View Code Duplication
        if (preg_match('/^(' . static::$_cache['uninflected'] . ')$/i', $word, $regs)) {
560
            static::$_cache['pluralize'][$word] = $word;
561
            return $word;
562
        }
563
564 View Code Duplication
        foreach (static::$_singular as $rule => $replacement) {
565
            if (preg_match($rule, $word)) {
566
                static::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
567
                return static::$_cache['singularize'][$word];
568
            }
569
        }
570
        static::$_cache['singularize'][$word] = $word;
571
        return $word;
572
    }
573
574
    /**
575
     * Returns the input lower_case_delimited_string as a CamelCasedString.
576
     *
577
     * @param string $string String to camelize
578
     * @param string $delimiter the delimiter in the input string
579
     * @return string CamelizedStringLikeThis.
580
     * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-camelcase-and-under-scored-forms
581
     */
582
    public static function camelize($string, $delimiter = '_')
583
    {
584
        $cacheKey = __FUNCTION__ . $delimiter;
585
586
        $result = static::_cache($cacheKey, $string);
587
588
        if ($result === false) {
589
            $result = str_replace(' ', '', static::humanize($string, $delimiter));
590
            static::_cache($cacheKey, $string, $result);
591
        }
592
593
        return $result;
594
    }
595
596
    /**
597
     * Returns the input CamelCasedString as an underscored_string.
598
     *
599
     * Also replaces dashes with underscores
600
     *
601
     * @param string $string CamelCasedString to be "underscorized"
602
     * @return string underscore_version of the input string
603
     * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-camelcase-and-under-scored-forms
604
     */
605
    public static function underscore($string)
606
    {
607
        return static::delimit(str_replace('-', '_', $string), '_');
608
    }
609
610
    /**
611
     * Returns the input CamelCasedString as an dashed-string.
612
     *
613
     * Also replaces underscores with dashes
614
     *
615
     * @param string $string The string to dasherize.
616
     * @return string Dashed version of the input string
617
     */
618
    public static function dasherize($string)
619
    {
620
        return static::delimit(str_replace('_', '-', $string), '-');
621
    }
622
623
    /**
624
     * Returns the input lower_case_delimited_string as 'A Human Readable String'.
625
     * (Underscores are replaced by spaces and capitalized following words.)
626
     *
627
     * @param string $string String to be humanized
628
     * @param string $delimiter the character to replace with a space
629
     * @return string Human-readable string
630
     * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-human-readable-forms
631
     */
632
    public static function humanize($string, $delimiter = '_')
633
    {
634
        $cacheKey = __FUNCTION__ . $delimiter;
635
636
        $result = static::_cache($cacheKey, $string);
637
638
        if ($result === false) {
639
            $result = explode(' ', str_replace($delimiter, ' ', $string));
640
            foreach ($result as &$word) {
641
                $word = mb_strtoupper(mb_substr($word, 0, 1)) . mb_substr($word, 1);
642
            }
643
            $result = implode(' ', $result);
644
            static::_cache($cacheKey, $string, $result);
645
        }
646
647
        return $result;
648
    }
649
650
    /**
651
     * Expects a CamelCasedInputString, and produces a lower_case_delimited_string
652
     *
653
     * @param string $string String to delimit
654
     * @param string $delimiter the character to use as a delimiter
655
     * @return string delimited string
656
     */
657
    public static function delimit($string, $delimiter = '_')
658
    {
659
        $cacheKey = __FUNCTION__ . $delimiter;
660
661
        $result = static::_cache($cacheKey, $string);
662
663
        if ($result === false) {
664
            $result = mb_strtolower(preg_replace('/(?<=\\w)([A-Z])/', $delimiter . '\\1', $string));
665
            static::_cache($cacheKey, $string, $result);
666
        }
667
668
        return $result;
669
    }
670
671
    /**
672
     * Returns corresponding table name for given model $className. ("people" for the model class "Person").
673
     *
674
     * @param string $className Name of class to get database table name for
675
     * @return string Name of the database table for given class
676
     * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-table-and-class-name-forms
677
     */
678
    public static function tableize($className)
679
    {
680
        $result = static::_cache(__FUNCTION__, $className);
681
682
        if ($result === false) {
683
            $result = static::pluralize(static::underscore($className));
684
            static::_cache(__FUNCTION__, $className, $result);
685
        }
686
687
        return $result;
688
    }
689
690
    /**
691
     * Returns Cake model class name ("Person" for the database table "people".) for given database table.
692
     *
693
     * @param string $tableName Name of database table to get class name for
694
     * @return string Class name
695
     * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-table-and-class-name-forms
696
     */
697
    public static function classify($tableName)
698
    {
699
        $result = static::_cache(__FUNCTION__, $tableName);
700
701
        if ($result === false) {
702
            $result = static::camelize(static::singularize($tableName));
703
            static::_cache(__FUNCTION__, $tableName, $result);
704
        }
705
706
        return $result;
707
    }
708
709
    /**
710
     * Returns camelBacked version of an underscored string.
711
     *
712
     * @param string $string String to convert.
713
     * @return string in variable form
714
     * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-variable-names
715
     */
716
    public static function variable($string)
717
    {
718
        $result = static::_cache(__FUNCTION__, $string);
719
720
        if ($result === false) {
721
            $camelized = static::camelize(static::underscore($string));
722
            $replace = strtolower(substr($camelized, 0, 1));
723
            $result = $replace . substr($camelized, 1);
724
            static::_cache(__FUNCTION__, $string, $result);
725
        }
726
727
        return $result;
728
    }
729
730
    /**
731
     * Returns a string with all spaces converted to dashes (by default), accented
732
     * characters converted to non-accented characters, and non word characters removed.
733
     *
734
     * @param string $string the string you want to slug
735
     * @param string $replacement will replace keys in map
736
     * @return string
737
     * @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-url-safe-strings
738
     */
739
    public static function slug($string, $replacement = '-')
740
    {
741
        $quotedReplacement = preg_quote($replacement, '/');
742
743
        $map = [
744
            '/[^\s\p{Zs}\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
745
            '/[\s\p{Zs}]+/mu' => $replacement,
746
            sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
747
        ];
748
749
        $string = str_replace(
750
            array_keys(static::$_transliteration),
751
            array_values(static::$_transliteration),
752
            $string
753
        );
754
        return preg_replace(array_keys($map), array_values($map), $string);
755
    }
756
}
757