|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ORM; |
|
4
|
|
|
|
|
5
|
|
|
use ORM\Exception\InvalidConfiguration; |
|
6
|
|
|
use ORM\Exception\InvalidName; |
|
7
|
|
|
use ReflectionClass; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Namer is for naming errors, columns, tables and methods |
|
11
|
|
|
* |
|
12
|
|
|
* Namer is an artificial word and is more a name giver. We just don't wanted to write so much. |
|
13
|
|
|
* |
|
14
|
|
|
* @package ORM |
|
15
|
|
|
*/ |
|
16
|
|
|
class Namer |
|
17
|
|
|
{ |
|
18
|
|
|
/** The template to use to calculate the table name. |
|
19
|
|
|
* @var string */ |
|
20
|
|
|
protected $tableNameTemplate = '%short%'; |
|
21
|
|
|
|
|
22
|
|
|
/** The naming scheme to use for table names. |
|
23
|
|
|
* @var string */ |
|
24
|
|
|
protected $tableNameScheme = 'snake_lower'; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string[] */ |
|
27
|
|
|
protected $tableNames = []; |
|
28
|
|
|
|
|
29
|
|
|
/** @var string[][] */ |
|
30
|
|
|
protected $columnNames = []; |
|
31
|
|
|
|
|
32
|
|
|
/** The naming scheme to use for column names. |
|
33
|
|
|
* @var string */ |
|
34
|
|
|
protected $columnNameScheme = 'snake_lower'; |
|
35
|
|
|
|
|
36
|
|
|
/** The naming scheme used for method names. |
|
37
|
|
|
* @var string */ |
|
38
|
|
|
protected $methodNameScheme = 'camelCase'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Namer constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param array $options |
|
44
|
|
|
*/ |
|
45
|
271 |
|
public function __construct($options = []) |
|
46
|
|
|
{ |
|
47
|
271 |
|
foreach ($options as $option => $value) { |
|
48
|
4 |
|
$this->setOption($option, $value); |
|
49
|
|
|
} |
|
50
|
271 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Set $option to $value |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $option |
|
56
|
|
|
* @param mixed $value |
|
57
|
|
|
* @return self |
|
58
|
|
|
*/ |
|
59
|
4 |
|
public function setOption($option, $value) |
|
60
|
|
|
{ |
|
61
|
|
|
switch ($option) { |
|
62
|
4 |
|
case EntityManager::OPT_TABLE_NAME_TEMPLATE: |
|
63
|
1 |
|
$this->tableNameTemplate = $value; |
|
64
|
1 |
|
break; |
|
65
|
|
|
|
|
66
|
3 |
|
case EntityManager::OPT_NAMING_SCHEME_TABLE: |
|
67
|
1 |
|
$this->tableNameScheme = $value; |
|
68
|
1 |
|
break; |
|
69
|
|
|
|
|
70
|
2 |
|
case EntityManager::OPT_NAMING_SCHEME_COLUMN: |
|
71
|
1 |
|
$this->columnNameScheme = $value; |
|
72
|
1 |
|
break; |
|
73
|
|
|
|
|
74
|
1 |
|
case EntityManager::OPT_NAMING_SCHEME_METHODS: |
|
75
|
1 |
|
$this->methodNameScheme = $value; |
|
76
|
1 |
|
break; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the table name for $reflection |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $class |
|
86
|
|
|
* @param string $template |
|
87
|
|
|
* @param string $namingScheme |
|
88
|
|
|
* @return string |
|
89
|
|
|
* @throws InvalidName |
|
90
|
|
|
*/ |
|
91
|
139 |
|
public function getTableName($class, $template = null, $namingScheme = null) |
|
92
|
|
|
{ |
|
93
|
139 |
|
if (!isset($this->tableNames[$class])) { |
|
94
|
139 |
|
if ($template === null) { |
|
95
|
2 |
|
$template = $this->tableNameTemplate; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
139 |
|
if ($namingScheme === null) { |
|
99
|
4 |
|
$namingScheme = $this->tableNameScheme; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
139 |
|
$reflection = new ReflectionClass($class); |
|
103
|
|
|
|
|
104
|
139 |
|
$name = $this->substitute($template, [ |
|
105
|
139 |
|
'short' => $reflection->getShortName(), |
|
106
|
139 |
|
'namespace' => explode('\\', $reflection->getNamespaceName()), |
|
107
|
139 |
|
'name' => preg_split('/[\\\\_]+/', $reflection->name), |
|
108
|
139 |
|
], '_'); |
|
109
|
|
|
|
|
110
|
138 |
|
if (empty($name)) { |
|
111
|
2 |
|
throw new InvalidName('Table name can not be empty'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
136 |
|
$this->tableNames[$class] = $this->forceNamingScheme($name, $namingScheme); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
135 |
|
return $this->tableNames[$class]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get the column name with $namingScheme or default naming scheme |
|
122
|
|
|
* |
|
123
|
|
|
* @param $class |
|
124
|
|
|
* @param string $attribute |
|
125
|
|
|
* @param string $prefix |
|
126
|
|
|
* @param string $namingScheme |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
145 |
|
public function getColumnName($class, $attribute, $prefix = null, $namingScheme = null) |
|
130
|
|
|
{ |
|
131
|
145 |
|
if (!isset($this->columnNames[$class][$attribute])) { |
|
132
|
145 |
|
if (!$namingScheme) { |
|
|
|
|
|
|
133
|
2 |
|
$namingScheme = $this->columnNameScheme; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
145 |
|
$name = $this->forceNamingScheme($attribute, $namingScheme); |
|
137
|
|
|
|
|
138
|
145 |
|
if ($prefix !== null && strpos($name, $prefix) !== 0) { |
|
139
|
22 |
|
$name = $prefix . $name; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
145 |
|
$this->columnNames[$class][$attribute] = $name; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
145 |
|
return $this->columnNames[$class][$attribute]; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Get the column name with $namingScheme or default naming scheme |
|
150
|
|
|
* |
|
151
|
|
|
* @param $name |
|
152
|
|
|
* @param null $namingScheme |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
96 |
|
public function getMethodName($name, $namingScheme = null) |
|
156
|
|
|
{ |
|
157
|
96 |
|
if (!$namingScheme) { |
|
158
|
2 |
|
$namingScheme = $this->methodNameScheme; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
96 |
|
return $this->forceNamingScheme($name, $namingScheme); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Substitute a $template with $values |
|
166
|
|
|
* |
|
167
|
|
|
* $values is a key value pair array. The value should be a string or an array o |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $template |
|
170
|
|
|
* @param array $values |
|
171
|
|
|
* @param string $arrayGlue |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
177 |
|
public function substitute($template, $values = [], $arrayGlue = ', ') |
|
175
|
|
|
{ |
|
176
|
177 |
|
return preg_replace_callback( |
|
177
|
177 |
|
'/%(.*?)%/', |
|
178
|
|
|
function ($match) use ($values, $arrayGlue) { |
|
179
|
|
|
// escape % with another % ( %% => % ) |
|
180
|
176 |
|
if ($match[0] === '%%') { |
|
181
|
1 |
|
return '%'; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
176 |
|
return $this->getValue(trim($match[1]), $values, $arrayGlue); |
|
185
|
177 |
|
}, |
|
186
|
|
|
$template |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Enforce $namingScheme to $name |
|
192
|
|
|
* |
|
193
|
|
|
* Supported naming schemes: snake_case, snake_lower, SNAKE_UPPER, Snake_Ucfirst, camelCase, StudlyCaps, lower |
|
194
|
|
|
* and UPPER. |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $name The name of the var / column |
|
197
|
|
|
* @param string $namingScheme The naming scheme to use |
|
198
|
|
|
* @return string |
|
199
|
|
|
* @throws InvalidConfiguration |
|
200
|
|
|
*/ |
|
201
|
230 |
|
public function forceNamingScheme($name, $namingScheme) |
|
202
|
|
|
{ |
|
203
|
230 |
|
$words = explode('_', preg_replace( |
|
204
|
230 |
|
'/([a-z0-9])([A-Z])/', |
|
205
|
230 |
|
'$1_$2', |
|
206
|
230 |
|
preg_replace_callback('/([a-z0-9])?([A-Z]+)([A-Z][a-z])/', function ($d) { |
|
207
|
24 |
|
return ($d[1] ? $d[1] . '_' : '') . $d[2] . '_' . $d[3]; |
|
208
|
230 |
|
}, $name) |
|
209
|
|
|
)); |
|
210
|
|
|
|
|
211
|
|
|
switch ($namingScheme) { |
|
212
|
230 |
|
case 'snake_case': |
|
213
|
23 |
|
$newName = implode('_', $words); |
|
214
|
23 |
|
break; |
|
215
|
|
|
|
|
216
|
207 |
|
case 'snake_lower': |
|
217
|
174 |
|
$newName = implode('_', array_map('strtolower', $words)); |
|
218
|
174 |
|
break; |
|
219
|
|
|
|
|
220
|
124 |
|
case 'SNAKE_UPPER': |
|
221
|
4 |
|
$newName = implode('_', array_map('strtoupper', $words)); |
|
222
|
4 |
|
break; |
|
223
|
|
|
|
|
224
|
120 |
|
case 'Snake_Ucfirst': |
|
225
|
4 |
|
$newName = implode('_', array_map('ucfirst', $words)); |
|
226
|
4 |
|
break; |
|
227
|
|
|
|
|
228
|
116 |
|
case 'camelCase': |
|
229
|
98 |
|
$newName = lcfirst(implode('', array_map('ucfirst', array_map('strtolower', $words)))); |
|
230
|
98 |
|
break; |
|
231
|
|
|
|
|
232
|
18 |
|
case 'StudlyCaps': |
|
233
|
9 |
|
$newName = implode('', array_map('ucfirst', array_map('strtolower', $words))); |
|
234
|
9 |
|
break; |
|
235
|
|
|
|
|
236
|
9 |
|
case 'lower': |
|
237
|
4 |
|
$newName = implode('', array_map('strtolower', $words)); |
|
238
|
4 |
|
break; |
|
239
|
|
|
|
|
240
|
5 |
|
case 'UPPER': |
|
241
|
4 |
|
$newName = implode('', array_map('strtoupper', $words)); |
|
242
|
4 |
|
break; |
|
243
|
|
|
|
|
244
|
|
|
default: |
|
245
|
1 |
|
throw new InvalidConfiguration('Naming scheme ' . $namingScheme . ' unknown'); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
229 |
|
return $newName; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Get the value for $attribute from $values using $arrayGlue |
|
253
|
|
|
* |
|
254
|
|
|
* @param string $attribute The key for $values |
|
255
|
|
|
* @param array $values |
|
256
|
|
|
* @param string $arrayGlue |
|
257
|
|
|
* @return string |
|
258
|
|
|
* @throws InvalidConfiguration |
|
259
|
|
|
*/ |
|
260
|
176 |
|
protected function getValue($attribute, $values, $arrayGlue) |
|
261
|
|
|
{ |
|
262
|
176 |
|
$placeholder = '%' . $attribute . '%'; |
|
263
|
176 |
|
if (preg_match('/\[(-?\d+\*?)\]$/', $attribute, $arrayAccessor)) { |
|
264
|
11 |
|
$attribute = substr($attribute, 0, strpos($attribute, '[')); |
|
265
|
11 |
|
$arrayAccessor = $arrayAccessor[1]; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
// throw when the variable is unknown |
|
269
|
176 |
|
if (!array_key_exists($attribute, $values)) { |
|
270
|
1 |
|
throw new InvalidConfiguration( |
|
271
|
1 |
|
'Template invalid: Placeholder ' . $placeholder . ' is not allowed' |
|
272
|
|
|
); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
175 |
|
if (is_scalar($values[$attribute]) || is_null($values[$attribute])) { |
|
276
|
159 |
|
return (string)$values[$attribute]; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
// otherwise we assume it is an array |
|
280
|
16 |
|
$array = $values[$attribute]; |
|
281
|
|
|
|
|
282
|
16 |
|
if (isset($arrayAccessor[0])) { |
|
283
|
11 |
|
$from = $arrayAccessor[0] === '-' ? |
|
284
|
11 |
|
count($array) - abs($arrayAccessor) : (int)$arrayAccessor; |
|
285
|
|
|
|
|
286
|
11 |
|
if ($from >= count($array)) { |
|
287
|
2 |
|
return ''; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
9 |
|
$array = substr($arrayAccessor, -1) === '*' ? |
|
291
|
9 |
|
array_slice($array, $from) : [$array[$from]]; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
14 |
|
return implode($arrayGlue, $array); |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: