|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\Inflector; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\Inflector\Rules\Uninflected; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Doctrine inflector has static methods for inflecting text. |
|
11
|
|
|
* |
|
12
|
|
|
* The methods in these classes are from several different sources collected |
|
13
|
|
|
* across several different php projects and several different authors. The |
|
14
|
|
|
* original author names and emails are not known. |
|
15
|
|
|
* |
|
16
|
|
|
* Pluralize & Singularize implementation are borrowed from CakePHP with some modifications. |
|
17
|
|
|
* |
|
18
|
|
|
* @deprecated |
|
19
|
|
|
*/ |
|
20
|
|
|
class Inflector |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var Pluralizer */ |
|
23
|
|
|
private static $pluralizer; |
|
24
|
|
|
|
|
25
|
|
|
/** @var Singularizer */ |
|
26
|
|
|
private static $singularizer; |
|
27
|
|
|
|
|
28
|
|
|
/** @var Inflect */ |
|
29
|
|
|
private static $inflect; |
|
30
|
|
|
|
|
31
|
4 |
|
public static function initialize() : void |
|
32
|
|
|
{ |
|
33
|
4 |
|
$uninflected = new Uninflected(Uninflected::DEFAULT); |
|
34
|
4 |
|
self::$pluralizer = new Pluralizer($uninflected); |
|
35
|
4 |
|
self::$singularizer = new Singularizer($uninflected, self::$pluralizer); |
|
36
|
|
|
|
|
37
|
4 |
|
self::$inflect = new Inflect(self::$singularizer, self::$pluralizer); |
|
38
|
4 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Converts a word into the format for a Doctrine table name. Converts 'ModelName' to 'model_name'. |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public static function tableize(string $word) : string |
|
44
|
|
|
{ |
|
45
|
3 |
|
return self::$inflect->tableize($word); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Converts a word into the format for a Doctrine class name. Converts 'table_name' to 'TableName'. |
|
50
|
|
|
*/ |
|
51
|
6 |
|
public static function classify(string $word) : string |
|
52
|
|
|
{ |
|
53
|
6 |
|
return self::$inflect->classify($word); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Camelizes a word. This uses the classify() method and turns the first character to lowercase. |
|
58
|
|
|
*/ |
|
59
|
5 |
|
public static function camelize(string $word) : string |
|
60
|
|
|
{ |
|
61
|
5 |
|
return self::$inflect->camelize($word); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Uppercases words with configurable delimiters between words. |
|
66
|
|
|
* |
|
67
|
|
|
* Takes a string and capitalizes all of the words, like PHP's built-in |
|
68
|
|
|
* ucwords function. This extends that behavior, however, by allowing the |
|
69
|
|
|
* word delimiters to be configured, rather than only separating on |
|
70
|
|
|
* whitespace. |
|
71
|
|
|
* |
|
72
|
|
|
* Here is an example: |
|
73
|
|
|
* <code> |
|
74
|
|
|
* <?php |
|
75
|
|
|
* $string = 'top-o-the-morning to all_of_you!'; |
|
76
|
|
|
* echo \Doctrine\Common\Inflector\Inflector::ucwords($string); |
|
77
|
|
|
* // Top-O-The-Morning To All_of_you! |
|
78
|
|
|
* |
|
79
|
|
|
* echo \Doctrine\Common\Inflector\Inflector::ucwords($string, '-_ '); |
|
80
|
|
|
* // Top-O-The-Morning To All_Of_You! |
|
81
|
|
|
* ?> |
|
82
|
|
|
* </code> |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $string The string to operate on. |
|
85
|
|
|
* @param string $delimiters A list of word separators. |
|
86
|
|
|
* |
|
87
|
|
|
* @return string The string with all delimiter-separated words capitalized. |
|
88
|
|
|
*/ |
|
89
|
2 |
|
public static function ucwords(string $string, string $delimiters = " \n\t\r\0\x0B-") : string |
|
90
|
|
|
{ |
|
91
|
2 |
|
return self::$inflect->ucwords($string, $delimiters); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Clears Inflectors inflected value caches, and resets the inflection |
|
96
|
|
|
* rules to the initial values. |
|
97
|
|
|
*/ |
|
98
|
4 |
|
public static function reset() : void |
|
99
|
|
|
{ |
|
100
|
4 |
|
self::initialize(); |
|
101
|
4 |
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Adds custom inflection $rules, of either 'plural' or 'singular' $type. |
|
105
|
|
|
* |
|
106
|
|
|
* ### Usage: |
|
107
|
|
|
* |
|
108
|
|
|
* {{{ |
|
109
|
|
|
* Inflector::rules('plural', ['/^(inflect)or$/i' => '\1ables']); |
|
110
|
|
|
* Inflector::rules('plural', [ |
|
111
|
|
|
* 'rules' => ['/^(inflect)ors$/i' => '\1ables'], |
|
112
|
|
|
* 'uninflected' => ['dontinflectme'], |
|
113
|
|
|
* 'irregular' => ['red' => 'redlings'] |
|
114
|
|
|
* ]); |
|
115
|
|
|
* }}} |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $type The type of inflection, either 'plural' or 'singular' |
|
118
|
|
|
* @param array|mixed[] $rules An array of rules to be added. |
|
119
|
|
|
* new rules that are being defined in $rules. |
|
120
|
|
|
* @param bool $reset If true, will unset default inflections for all |
|
121
|
|
|
* new rules that are being defined in $rules. |
|
122
|
|
|
*/ |
|
123
|
4 |
|
public static function rules(string $type, array $rules, bool $reset = false) : void |
|
124
|
|
|
{ |
|
125
|
4 |
|
if ($type === 'plural') { |
|
126
|
3 |
|
self::$pluralizer->addRules($rules, $reset); |
|
127
|
|
|
} else { |
|
128
|
3 |
|
self::$singularizer->addRules($rules, $reset); |
|
129
|
|
|
} |
|
130
|
4 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Returns a word in plural form. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $word The word in singular form. |
|
136
|
|
|
* |
|
137
|
|
|
* @return string The word in plural form. |
|
138
|
|
|
*/ |
|
139
|
256 |
|
public static function pluralize(string $word) : string |
|
140
|
|
|
{ |
|
141
|
256 |
|
return self::$inflect->pluralize($word); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Returns a word in singular form. |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $word The word in plural form. |
|
148
|
|
|
* |
|
149
|
|
|
* @return string The word in singular form. |
|
150
|
|
|
*/ |
|
151
|
256 |
|
public static function singularize(string $word) : string |
|
152
|
|
|
{ |
|
153
|
256 |
|
return self::$inflect->singularize($word); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
Inflector::initialize(); |
|
158
|
|
|
|