Completed
Push — develop ( 8dd5aa...fee724 )
by Freddie
14:52
created

Inflector::dbName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Generator\Domain\Builders;
11
12
use Jawira\CaseConverter\Convert;
13
use Symfony\Component\Inflector\Inflector as I;
14
15
final class Inflector
16
{
17
    public function table(string $string): string
18
    {
19
        return $string;
20
    }
21
22
    public function entity(string $string): string
23
    {
24
        return $this->pascal($this->singularize($string));
25
    }
26
27
    public function fnPlural(string $string): string
28
    {
29
        return $this->pluralize($this->pascal($string));
30
    }
31
32
    public function fnSingular(string $string): string
33
    {
34
        return $this->singularize($this->pascal($string));
35
    }
36
37
    public function item(string $string): string
38
    {
39
        return $this->singularize($this->camel($string));
40
    }
41
42
    public function items(string $string): string
43
    {
44
        return $this->pluralize($this->camel($string));
45
    }
46
47
    public function route(string $string): string
48
    {
49
        return $this->pluralize($this->dash($string));
50
    }
51
52
    public function routeName(string $name, string $action): string
53
    {
54
        return $this->pluralize($this->dash($name)) . '.' . $this->dash($action);
55
    }
56
57
    public function pascalProperty(string $string): string
58
    {
59
        return $this->pascal($string);
60
    }
61
62
    public function camelProperty(string $string): string
63
    {
64
        return $this->camel($string);
65
    }
66
67
    public function form(string $string): string
68
    {
69
        return $this->snake($this->singularize($string));
70
    }
71
72
    public function action(string $string): string
73
    {
74
        return $this->snake($string);
75
    }
76
77
    public function pascalAction(string $string): string
78
    {
79
        return $this->pascal($string);
80
    }
81
82
    public function camelAction(string $string): string
83
    {
84
        return $this->camel($string);
85
    }
86
87
    public function dashAction(string $string): string
88
    {
89
        return $this->dash($string);
90
    }
91
92
    public function role(string $string): string
93
    {
94
        return \strtoupper($this->singularize($this->camel($string)));
95
    }
96
97
    public function commandName(string $entity, string $action): string
98
    {
99
        return $this->pluralize($this->dash($entity)) . ':' . $this->dash($action);
100
    }
101
102
    public function entityTitleSingular(string $entity): string
103
    {
104
        return $this->singularize((new Convert($entity))->toTitle());
105
    }
106
107
    public function entityTitlePlural(string $entity): string
108
    {
109
        return $this->pluralize((new Convert($entity))->toTitle());
110
    }
111
112
    public function propertyTitle(string $name): string
113
    {
114
        return (new Convert($name))->toTitle();
115
    }
116
117
    public function dbName(string $string): string
118
    {
119
        return $this->snake($string);
120
    }
121
122
    public function jsName(string $string): string
123
    {
124
        return $this->pluralize($this->camel($string));
125
    }
126
127
    public function sheetName(string $string): string
128
    {
129
        return $this->pascal($string);
130
    }
131
132
    public function prototypeName(string $string): string
133
    {
134
        return $this->snake($string);
135
    }
136
137
    public function singular(string $string): string
138
    {
139
        return $this->singularize($string);
140
    }
141
142
    public function plural(string $string): string
143
    {
144
        return $this->pluralize($string);
145
    }
146
147
    private function pascal(string $string): string
148
    {
149
        return (new Convert($string))->toPascal();
150
    }
151
152
    private function camel(string $string): string
153
    {
154
        return (new Convert($string))->toCamel();
155
    }
156
157
    private function snake(string $string): string
158
    {
159
        return (new Convert($string))->toSnake();
160
    }
161
162
    private function dash(string $string): string
163
    {
164
        return (new Convert($string))->toKebab();
165
    }
166
167
    private function singularize(string $string): string
168
    {
169
        if (\substr(\strtolower($string), -3) === 'ice') {
170
            return $string;
171
        }
172
173
        if (\substr(\strtolower($string), -6) === 'status') {
174
            return $string;
175
        }
176
177
        $singularize = I::singularize($string);
178
179
        return \is_array($singularize)
180
            ? \array_pop($singularize)
181
            : $singularize;
182
    }
183
184
    private function pluralize(string $string): string
185
    {
186
        if (\substr(\strtolower($string), -3) === 'ice') {
187
            $onlyUpper = \preg_match('@^[A-Z]+$@', $string);
188
189
            return $string . ($onlyUpper ? 'S' : 's');
190
        }
191
192
        if (\substr(\strtolower($string), -6) === 'status') {
193
            return $string;
194
        }
195
196
        $pluralize = I::pluralize($this->singularize($string));
197
198
        return \is_array($pluralize)
199
            ? $pluralize[0]
200
            : $pluralize;
201
    }
202
}
203