|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\Common\Inflector; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\Inflector\Inflector as InflectorObject; |
|
23
|
|
|
use Doctrine\Inflector\InflectorFactory; |
|
24
|
|
|
use Doctrine\Inflector\LanguageInflectorFactory; |
|
25
|
|
|
use Doctrine\Inflector\Rules\Pattern; |
|
26
|
|
|
use Doctrine\Inflector\Rules\Patterns; |
|
27
|
|
|
use Doctrine\Inflector\Rules\Ruleset; |
|
28
|
|
|
use Doctrine\Inflector\Rules\Substitution; |
|
29
|
|
|
use Doctrine\Inflector\Rules\Substitutions; |
|
30
|
|
|
use Doctrine\Inflector\Rules\Transformation; |
|
31
|
|
|
use Doctrine\Inflector\Rules\Transformations; |
|
32
|
|
|
use Doctrine\Inflector\Rules\Word; |
|
33
|
|
|
use InvalidArgumentException; |
|
34
|
|
|
use function array_keys; |
|
35
|
|
|
use function array_map; |
|
36
|
|
|
use function array_unshift; |
|
37
|
|
|
use function array_values; |
|
38
|
|
|
use function sprintf; |
|
39
|
|
|
use function trigger_error; |
|
40
|
|
|
use const E_USER_DEPRECATED; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @deprecated |
|
44
|
|
|
*/ |
|
45
|
|
|
final class Inflector |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* @var LanguageInflectorFactory|null |
|
49
|
|
|
*/ |
|
50
|
|
|
private static $factory; |
|
51
|
|
|
|
|
52
|
|
|
/** @var InflectorObject|null */ |
|
53
|
|
|
private static $instance; |
|
54
|
|
|
|
|
55
|
520 |
|
private static function getInstance() : InflectorObject |
|
56
|
|
|
{ |
|
57
|
520 |
|
if (self::$factory === null) { |
|
58
|
2 |
|
self::$factory = self::createFactory(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
520 |
|
if (self::$instance === null) { |
|
62
|
5 |
|
self::$instance = self::$factory->build(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
520 |
|
return self::$instance; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
5 |
|
private static function createFactory() : LanguageInflectorFactory |
|
69
|
|
|
{ |
|
70
|
5 |
|
return InflectorFactory::create(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Converts a word into the format for a Doctrine table name. Converts 'ModelName' to 'model_name'. |
|
75
|
|
|
* |
|
76
|
|
|
* @deprecated |
|
77
|
|
|
*/ |
|
78
|
3 |
|
public static function tableize(string $word) : string |
|
79
|
|
|
{ |
|
80
|
3 |
|
@trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED); |
|
81
|
|
|
|
|
82
|
3 |
|
return self::getInstance()->tableize($word); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Converts a word into the format for a Doctrine class name. Converts 'table_name' to 'TableName'. |
|
87
|
|
|
*/ |
|
88
|
6 |
|
public static function classify(string $word) : string |
|
89
|
|
|
{ |
|
90
|
6 |
|
@trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED); |
|
91
|
|
|
|
|
92
|
6 |
|
return self::getInstance()->classify($word); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Camelizes a word. This uses the classify() method and turns the first character to lowercase. |
|
97
|
|
|
* |
|
98
|
|
|
* @deprecated |
|
99
|
|
|
*/ |
|
100
|
5 |
|
public static function camelize(string $word) : string |
|
101
|
|
|
{ |
|
102
|
5 |
|
@trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED); |
|
103
|
|
|
|
|
104
|
5 |
|
return self::getInstance()->camelize($word); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Uppercases words with configurable delimiters between words. |
|
109
|
|
|
* |
|
110
|
|
|
* Takes a string and capitalizes all of the words, like PHP's built-in |
|
111
|
|
|
* ucwords function. This extends that behavior, however, by allowing the |
|
112
|
|
|
* word delimiters to be configured, rather than only separating on |
|
113
|
|
|
* whitespace. |
|
114
|
|
|
* |
|
115
|
|
|
* Here is an example: |
|
116
|
|
|
* <code> |
|
117
|
|
|
* <?php |
|
118
|
|
|
* $string = 'top-o-the-morning to all_of_you!'; |
|
119
|
|
|
* echo \Doctrine\Common\Inflector\Inflector::ucwords($string); |
|
120
|
|
|
* // Top-O-The-Morning To All_of_you! |
|
121
|
|
|
* |
|
122
|
|
|
* echo \Doctrine\Common\Inflector\Inflector::ucwords($string, '-_ '); |
|
123
|
|
|
* // Top-O-The-Morning To All_Of_You! |
|
124
|
|
|
* ?> |
|
125
|
|
|
* </code> |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $string The string to operate on. |
|
128
|
|
|
* @param string $delimiters A list of word separators. |
|
129
|
|
|
* |
|
130
|
|
|
* @return string The string with all delimiter-separated words capitalized. |
|
131
|
|
|
* |
|
132
|
|
|
* @deprecated |
|
133
|
|
|
*/ |
|
134
|
2 |
|
public static function ucwords(string $string, string $delimiters = " \n\t\r\0\x0B-") : string |
|
135
|
|
|
{ |
|
136
|
2 |
|
@trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please use the "ucwords" function instead.', __METHOD__), E_USER_DEPRECATED); |
|
137
|
|
|
|
|
138
|
2 |
|
return ucwords($string, $delimiters); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Clears Inflectors inflected value caches, and resets the inflection |
|
143
|
|
|
* rules to the initial values. |
|
144
|
|
|
* |
|
145
|
|
|
* @deprecated |
|
146
|
|
|
*/ |
|
147
|
4 |
|
public static function reset() : void |
|
148
|
|
|
{ |
|
149
|
4 |
|
@trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED); |
|
150
|
|
|
|
|
151
|
4 |
|
self::$factory = null; |
|
152
|
4 |
|
self::$instance = null; |
|
153
|
4 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Adds custom inflection $rules, of either 'plural' or 'singular' $type. |
|
157
|
|
|
* |
|
158
|
|
|
* ### Usage: |
|
159
|
|
|
* |
|
160
|
|
|
* {{{ |
|
161
|
|
|
* Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables')); |
|
162
|
|
|
* Inflector::rules('plural', array( |
|
163
|
|
|
* 'rules' => array('/^(inflect)ors$/i' => '\1ables'), |
|
164
|
|
|
* 'uninflected' => array('dontinflectme'), |
|
165
|
|
|
* 'irregular' => array('red' => 'redlings') |
|
166
|
|
|
* )); |
|
167
|
|
|
* }}} |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $type The type of inflection, either 'plural' or 'singular' |
|
170
|
|
|
* @param array|iterable $rules An array of rules to be added. |
|
171
|
|
|
* @param boolean $reset If true, will unset default inflections for all |
|
172
|
|
|
* new rules that are being defined in $rules. |
|
173
|
|
|
* |
|
174
|
|
|
* @return void |
|
175
|
|
|
* |
|
176
|
|
|
* @deprecated |
|
177
|
|
|
*/ |
|
178
|
4 |
|
public static function rules(string $type, iterable $rules, bool $reset = false) : void |
|
179
|
|
|
{ |
|
180
|
4 |
|
@trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED); |
|
181
|
|
|
|
|
182
|
4 |
|
if (self::$factory === null) { |
|
183
|
3 |
|
self::$factory = self::createFactory(); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
4 |
|
self::$instance = null; |
|
187
|
|
|
|
|
188
|
4 |
|
switch ($type) { |
|
189
|
4 |
|
case 'singular': |
|
190
|
3 |
|
self::$factory->withSingularRules(self::buildRuleset($rules), $reset); |
|
191
|
3 |
|
break; |
|
192
|
3 |
|
case 'plural': |
|
193
|
3 |
|
self::$factory->withPluralRules(self::buildRuleset($rules), $reset); |
|
194
|
3 |
|
break; |
|
195
|
|
|
default: |
|
196
|
|
|
throw new InvalidArgumentException(sprintf('Cannot define custom inflection rules for type "%s".', $type)); |
|
197
|
|
|
} |
|
198
|
4 |
|
} |
|
199
|
|
|
|
|
200
|
4 |
|
private static function buildRuleset(iterable $rules) : Ruleset |
|
201
|
|
|
{ |
|
202
|
4 |
|
$regular = []; |
|
203
|
4 |
|
$irregular = []; |
|
204
|
4 |
|
$uninflected = []; |
|
205
|
|
|
|
|
206
|
4 |
|
foreach ($rules as $rule => $pattern) { |
|
207
|
4 |
|
if ( ! is_array($pattern)) { |
|
208
|
2 |
|
$regular[$rule] = $pattern; |
|
209
|
|
|
|
|
210
|
2 |
|
continue; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
4 |
|
switch ($rule) { |
|
214
|
4 |
|
case 'uninflected': |
|
215
|
3 |
|
$uninflected = $pattern; |
|
216
|
3 |
|
break; |
|
217
|
4 |
|
case 'irregular': |
|
218
|
4 |
|
$irregular = $pattern; |
|
219
|
4 |
|
break; |
|
220
|
4 |
|
case 'rules': |
|
221
|
4 |
|
$regular = $pattern; |
|
222
|
4 |
|
break; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
4 |
|
return new Ruleset( |
|
227
|
4 |
|
new Transformations(...array_map( |
|
228
|
|
|
static function (string $pattern, string $replacement) : Transformation { |
|
229
|
4 |
|
return new Transformation(new Pattern($pattern), $replacement); |
|
230
|
4 |
|
}, |
|
231
|
4 |
|
array_keys($regular), |
|
232
|
4 |
|
array_values($regular) |
|
233
|
|
|
)), |
|
234
|
4 |
|
new Patterns(...array_map( |
|
235
|
|
|
static function (string $pattern) : Pattern { |
|
236
|
3 |
|
return new Pattern($pattern); |
|
237
|
4 |
|
}, |
|
238
|
4 |
|
$uninflected |
|
239
|
|
|
)), |
|
240
|
4 |
|
new Substitutions(...array_map( |
|
241
|
|
|
static function (string $word, string $to) : Substitution { |
|
242
|
4 |
|
return new Substitution(new Word($word), new Word($to)); |
|
243
|
4 |
|
}, |
|
244
|
4 |
|
array_keys($irregular), |
|
245
|
4 |
|
array_values($irregular) |
|
246
|
|
|
)) |
|
247
|
|
|
); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Returns a word in plural form. |
|
252
|
|
|
* |
|
253
|
|
|
* @param string $word The word in singular form. |
|
254
|
|
|
* |
|
255
|
|
|
* @return string The word in plural form. |
|
256
|
|
|
* |
|
257
|
|
|
* @deprecated |
|
258
|
|
|
*/ |
|
259
|
254 |
|
public static function pluralize(string $word) : string |
|
260
|
|
|
{ |
|
261
|
254 |
|
@trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED); |
|
262
|
|
|
|
|
263
|
254 |
|
return self::getInstance()->pluralize($word); |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Returns a word in singular form. |
|
268
|
|
|
* |
|
269
|
|
|
* @param string $word The word in plural form. |
|
270
|
|
|
* |
|
271
|
|
|
* @return string The word in singular form. |
|
272
|
|
|
* |
|
273
|
|
|
* @deprecated |
|
274
|
|
|
*/ |
|
275
|
254 |
|
public static function singularize(string $word) : string |
|
276
|
|
|
{ |
|
277
|
254 |
|
@trigger_error(sprintf('The "%s" method is deprecated and will be dropped in Doctrine Inflector 3.0. Please update to the new Inflector API.', __METHOD__), E_USER_DEPRECATED); |
|
278
|
|
|
|
|
279
|
254 |
|
return self::getInstance()->singularize($word); |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|