zend-blog-3-backend

Форк
0
130 строк · 3.6 Кб
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: morontt
5
 * Date: 28.02.16
6
 * Time: 18:42
7
 */
8

9
namespace App\Utils;
10

11
/**
12
 * PHP implementation Ember.Inflector.pluralize
13
 *
14
 * @see https://github.com/stefanpenner/ember-inflector/blob/1.9.4/addon/lib/system/inflections.js
15
 * @see https://github.com/stefanpenner/ember-inflector/blob/1.9.4/addon/lib/system/inflector.js
16
 *
17
 * Class Inflector
18
 */
19
class Inflector
20
{
21
    private const BLANK_REGEX = '/^\s*$/';
22
    private const LAST_WORD_DASHED_REGEX = '/([\w\/-]+[_\/\s-])([a-z\d]+$)/';
23
    private const LAST_WORD_CAMELIZED_REGEX = '/([\w\/\s-]+)([A-Z][a-z\d]*$)/';
24
    private const CAMELIZED_REGEX = '/[A-Z][a-z\d]*$/';
25

26
    protected static array $plurals = [
27
        ['/$/', 's'],
28
        ['/s$/i', 's'],
29
        ['/^(ax|test)is$/i', '$1es'],
30
        ['/(octop|vir)us$/i', '$1i'],
31
        ['/(octop|vir)i$/i', '$1i'],
32
        ['/(alias|status)$/i', '$1es'],
33
        ['/(bu)s$/i', '$1ses'],
34
        ['/(buffal|tomat)o$/i', '$1oes'],
35
        ['/([ti])um$/i', '$1a'],
36
        ['/([ti])a$/i', '$1a'],
37
        ['/sis$/i', 'ses'],
38
        ['/(?:([^f])fe|([lr])f)$/i', '$1$2ves'],
39
        ['/(hive)$/i', '$1s'],
40
        ['/([^aeiouy]|qu)y$/i', '$1ies'],
41
        ['/(x|ch|ss|sh)$/i', '$1es'],
42
        ['/(matr|vert|ind)(?:ix|ex)$/i', '$1ices'],
43
        ['/^(m|l)ouse$/i', '$1ice'],
44
        ['/^(m|l)ice$/i', '$1ice'],
45
        ['/^(ox)$/i', '$1en'],
46
        ['/^(oxen)$/i', '$1'],
47
        ['/(quiz)$/i', '$1zes'],
48
    ];
49

50
    protected static array $irregularPairs = [
51
        ['person', 'people'],
52
        ['man', 'men'],
53
        ['child', 'children'],
54
        ['sex', 'sexes'],
55
        ['move', 'moves'],
56
        ['cow', 'kine'],
57
        ['zombie', 'zombies'],
58
    ];
59

60
    protected static array $uncountable = [
61
        'equipment',
62
        'information',
63
        'rice',
64
        'money',
65
        'species',
66
        'series',
67
        'fish',
68
        'sheep',
69
        'jeans',
70
        'police',
71
    ];
72

73
    /**
74
     * @param string $word
75
     *
76
     * @return string
77
     */
78
    public static function pluralize(string $word): string
79
    {
80
        if (!$word || preg_match(self::BLANK_REGEX, $word)) {
81
            return $word;
82
        }
83

84
        $isCamelized = preg_match(self::CAMELIZED_REGEX, $word);
85
        $firstPhrase = '';
86
        $lastWord = $word;
87

88
        $lowercase = strtolower($word);
89
        $wordSplit = [];
90
        if (preg_match(self::LAST_WORD_DASHED_REGEX, $word, $wordSplit)) {
91
            $firstPhrase = $wordSplit[1];
92
            $lastWord = strtolower($wordSplit[2]);
93
        } else {
94
            $wordSplit = [];
95
            if (preg_match(self::LAST_WORD_CAMELIZED_REGEX, $word, $wordSplit)) {
96
                $firstPhrase = $wordSplit[1];
97
                $lastWord = strtolower($wordSplit[2]);
98
            }
99
        }
100

101
        if (in_array($lowercase, self::$uncountable) || in_array($lastWord, self::$uncountable)) {
102
            return $word;
103
        }
104

105
        foreach (self::$irregularPairs as $rule) {
106
            if ($rule[0] === $lastWord) {
107
                $substitution = $rule[1];
108
                if ($isCamelized) {
109
                    $substitution = ucfirst($substitution);
110
                }
111

112
                return $firstPhrase . $substitution;
113
            }
114
        }
115

116
        for ($i = count(self::$plurals); $i > 0; $i--) {
117
            $inflection = self::$plurals[$i - 1];
118
            if (preg_match($inflection[0], $lastWord)) {
119
                $res = preg_replace($inflection[0], $inflection[1], $lastWord);
120
                if ($isCamelized) {
121
                    $res = ucfirst($res);
122
                }
123

124
                return $firstPhrase . $res;
125
            }
126
        }
127

128
        return 'n/a';
129
    }
130
}
131

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.