|
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
|
|
|
* @author Thomas Flori <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class Namer |
|
18
|
|
|
{ |
|
19
|
|
|
/** The template to use to calculate the table name. |
|
20
|
|
|
* @var string */ |
|
21
|
|
|
protected $tableNameTemplate = '%short%'; |
|
22
|
|
|
|
|
23
|
|
|
/** The naming scheme to use for table names. |
|
24
|
|
|
* @var string */ |
|
25
|
|
|
protected $tableNameScheme = 'snake_lower'; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string[] */ |
|
28
|
|
|
protected $tableNames = []; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string[][] */ |
|
31
|
|
|
protected $columnNames = []; |
|
32
|
|
|
|
|
33
|
|
|
/** The naming scheme to use for column names. |
|
34
|
|
|
* @var string */ |
|
35
|
|
|
protected $columnNameScheme = 'snake_lower'; |
|
36
|
|
|
|
|
37
|
|
|
/** The naming scheme used for method names. |
|
38
|
|
|
* @var string */ |
|
39
|
|
|
protected $methodNameScheme = 'camelCase'; |
|
40
|
|
|
|
|
41
|
|
|
/** The naming scheme used for attributes. |
|
42
|
|
|
* @var string */ |
|
43
|
|
|
protected $attributeNameScheme = 'camelCase'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
297 |
|
* Namer constructor. |
|
47
|
|
|
* |
|
48
|
297 |
|
* @param array $options |
|
49
|
4 |
|
*/ |
|
50
|
|
|
public function __construct($options = []) |
|
51
|
297 |
|
{ |
|
52
|
|
|
foreach ($options as $option => $value) { |
|
53
|
|
|
$this->setOption($option, $value); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Set $option to $value |
|
59
|
|
|
* |
|
60
|
4 |
|
* @param string $option |
|
61
|
|
|
* @param mixed $value |
|
62
|
|
|
* @return static |
|
63
|
4 |
|
*/ |
|
64
|
1 |
|
public function setOption($option, $value) |
|
65
|
1 |
|
{ |
|
66
|
|
|
switch ($option) { |
|
67
|
3 |
|
case EntityManager::OPT_TABLE_NAME_TEMPLATE: |
|
68
|
1 |
|
$this->tableNameTemplate = $value; |
|
69
|
1 |
|
break; |
|
70
|
|
|
|
|
71
|
2 |
|
case EntityManager::OPT_NAMING_SCHEME_TABLE: |
|
72
|
1 |
|
$this->tableNameScheme = $value; |
|
73
|
1 |
|
break; |
|
74
|
|
|
|
|
75
|
1 |
|
case EntityManager::OPT_NAMING_SCHEME_COLUMN: |
|
76
|
1 |
|
$this->columnNameScheme = $value; |
|
77
|
1 |
|
break; |
|
78
|
|
|
|
|
79
|
|
|
case EntityManager::OPT_NAMING_SCHEME_METHODS: |
|
80
|
4 |
|
$this->methodNameScheme = $value; |
|
81
|
|
|
break; |
|
82
|
|
|
|
|
83
|
|
|
case EntityManager::OPT_NAMING_SCHEME_ATTRIBUTE: |
|
84
|
|
|
$this->attributeNameScheme = $value; |
|
85
|
|
|
break; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
147 |
|
* Get the table name for $reflection |
|
93
|
|
|
* |
|
94
|
147 |
|
* @param string $class |
|
95
|
147 |
|
* @param string $template |
|
96
|
2 |
|
* @param string $namingScheme |
|
97
|
|
|
* @return string |
|
98
|
|
|
* @throws InvalidName |
|
99
|
147 |
|
*/ |
|
100
|
4 |
|
public function getTableName($class, $template = null, $namingScheme = null) |
|
101
|
|
|
{ |
|
102
|
|
|
if (!isset($this->tableNames[$class])) { |
|
103
|
147 |
|
if ($template === null) { |
|
104
|
|
|
$template = $this->tableNameTemplate; |
|
105
|
147 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
if ($namingScheme === null) { |
|
108
|
147 |
|
$namingScheme = $this->tableNameScheme; |
|
109
|
147 |
|
} |
|
110
|
147 |
|
|
|
111
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
112
|
147 |
|
$reflection = new ReflectionClass($class); |
|
113
|
|
|
|
|
114
|
|
|
$name = $this->substitute( |
|
115
|
146 |
|
$template, |
|
116
|
2 |
|
[ |
|
117
|
|
|
'short' => $reflection->getShortName(), |
|
118
|
|
|
'namespace' => explode('\\', $reflection->getNamespaceName()), |
|
119
|
144 |
|
'name' => preg_split('/[\\\\_]+/', $reflection->name), |
|
120
|
|
|
], |
|
121
|
|
|
'_' |
|
122
|
143 |
|
); |
|
123
|
|
|
|
|
124
|
|
|
if (empty($name)) { |
|
125
|
|
|
throw new InvalidName('Table name can not be empty'); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$this->tableNames[$class] = $this->forceNamingScheme($name, $namingScheme); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $this->tableNames[$class]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
166 |
|
/** |
|
135
|
|
|
* Get the column name with $namingScheme or default naming scheme |
|
136
|
166 |
|
* |
|
137
|
166 |
|
* @param string $class |
|
138
|
2 |
|
* @param string $attribute |
|
139
|
|
|
* @param string $prefix |
|
140
|
|
|
* @param string $namingScheme |
|
141
|
166 |
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
166 |
|
public function getColumnName($class, $attribute, $prefix = null, $namingScheme = null) |
|
144
|
22 |
|
{ |
|
145
|
|
|
if (!isset($this->columnNames[$class][$attribute])) { |
|
146
|
|
|
if ($namingScheme === null) { |
|
147
|
166 |
|
$namingScheme = $this->columnNameScheme; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
166 |
|
$name = $this->forceNamingScheme($attribute, $namingScheme); |
|
151
|
|
|
|
|
152
|
|
|
if ($prefix !== null && strpos($name, $prefix) !== 0) { |
|
153
|
|
|
$name = $prefix . $name; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
$this->columnNames[$class][$attribute] = $name; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $this->columnNames[$class][$attribute]; |
|
160
|
121 |
|
} |
|
161
|
|
|
|
|
162
|
121 |
|
/** |
|
163
|
2 |
|
* Get the method name with $namingScheme or default naming scheme |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $name |
|
166
|
121 |
|
* @param string $namingScheme |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getMethodName($name, $namingScheme = null) |
|
170
|
|
|
{ |
|
171
|
|
|
if ($namingScheme === null) { |
|
172
|
|
|
$namingScheme = $this->methodNameScheme; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $this->forceNamingScheme($name, $namingScheme); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
|
|
179
|
186 |
|
/** |
|
180
|
|
|
* Get the attribute name with $namingScheme or default naming scheme |
|
181
|
186 |
|
* |
|
182
|
186 |
|
* @param string $name |
|
183
|
186 |
|
* @param string $namingScheme |
|
184
|
|
|
* @return string |
|
185
|
185 |
|
*/ |
|
186
|
1 |
|
public function getAttributeName($name, $prefix = null, $namingScheme = null) |
|
187
|
|
|
{ |
|
188
|
|
|
if ($namingScheme === null) { |
|
189
|
185 |
|
$namingScheme = $this->attributeNameScheme; |
|
190
|
186 |
|
} |
|
191
|
|
|
|
|
192
|
|
|
if ($prefix !== null) { |
|
193
|
|
|
$name = preg_replace('~^' . preg_quote($prefix) . '~', '', $name); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
return $this->forceNamingScheme($name, $namingScheme); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Substitute a $template with $values |
|
201
|
|
|
* |
|
202
|
|
|
* $values is a key value pair array. The value should be a string or an array o |
|
203
|
|
|
* |
|
204
|
|
|
* @param string $template |
|
205
|
|
|
* @param array $values |
|
206
|
255 |
|
* @param string $arrayGlue |
|
207
|
|
|
* @return string |
|
208
|
255 |
|
*/ |
|
209
|
255 |
|
public function substitute($template, $values = [], $arrayGlue = ', ') |
|
210
|
255 |
|
{ |
|
211
|
255 |
|
return preg_replace_callback( |
|
212
|
24 |
|
'/%(.*?)%/', |
|
213
|
255 |
|
function ($match) use ($values, $arrayGlue) { |
|
214
|
|
|
// escape % with another % ( %% => % ) |
|
215
|
|
|
if ($match[0] === '%%') { |
|
216
|
|
|
return '%'; |
|
217
|
255 |
|
} |
|
218
|
23 |
|
|
|
219
|
23 |
|
return $this->getValue(trim($match[1]), $values, $arrayGlue); |
|
220
|
|
|
}, |
|
221
|
232 |
|
$template |
|
222
|
198 |
|
); |
|
223
|
198 |
|
} |
|
224
|
|
|
|
|
225
|
149 |
|
/** |
|
226
|
4 |
|
* Enforce $namingScheme to $name |
|
227
|
4 |
|
* |
|
228
|
|
|
* Supported naming schemes: snake_case, snake_lower, SNAKE_UPPER, Snake_Ucfirst, camelCase, StudlyCaps, lower |
|
229
|
145 |
|
* and UPPER. |
|
230
|
4 |
|
* |
|
231
|
4 |
|
* @param string $name The name of the var / column |
|
232
|
|
|
* @param string $namingScheme The naming scheme to use |
|
233
|
141 |
|
* @return string |
|
234
|
123 |
|
* @throws InvalidConfiguration |
|
235
|
123 |
|
*/ |
|
236
|
|
|
public function forceNamingScheme($name, $namingScheme) |
|
237
|
18 |
|
{ |
|
238
|
9 |
|
$words = explode('_', preg_replace( |
|
239
|
9 |
|
'/([a-z0-9])([A-Z])/', |
|
240
|
|
|
'$1_$2', |
|
241
|
9 |
|
preg_replace_callback('/([a-z0-9])?([A-Z]+)([A-Z][a-z])/', function ($match) { |
|
242
|
4 |
|
return ($match[1] ? $match[1] . '_' : '') . $match[2] . '_' . $match[3]; |
|
243
|
4 |
|
}, $name) |
|
244
|
|
|
)); |
|
245
|
5 |
|
|
|
246
|
4 |
|
switch ($namingScheme) { |
|
247
|
4 |
|
case 'snake_case': |
|
248
|
|
|
$newName = implode('_', $words); |
|
249
|
|
|
break; |
|
250
|
1 |
|
|
|
251
|
|
|
case 'snake_lower': |
|
252
|
|
|
$newName = implode('_', array_map('strtolower', $words)); |
|
253
|
254 |
|
break; |
|
254
|
|
|
|
|
255
|
|
|
case 'SNAKE_UPPER': |
|
256
|
|
|
$newName = implode('_', array_map('strtoupper', $words)); |
|
257
|
|
|
break; |
|
258
|
|
|
|
|
259
|
|
|
case 'Snake_Ucfirst': |
|
260
|
|
|
$newName = implode('_', array_map('ucfirst', $words)); |
|
261
|
|
|
break; |
|
262
|
|
|
|
|
263
|
|
|
case 'camelCase': |
|
264
|
|
|
$newName = lcfirst(implode('', array_map('ucfirst', array_map('strtolower', $words)))); |
|
265
|
185 |
|
break; |
|
266
|
|
|
|
|
267
|
185 |
|
case 'StudlyCaps': |
|
268
|
185 |
|
$newName = implode('', array_map('ucfirst', array_map('strtolower', $words))); |
|
269
|
11 |
|
break; |
|
270
|
11 |
|
|
|
271
|
|
|
case 'lower': |
|
272
|
174 |
|
$newName = implode('', array_map('strtolower', $words)); |
|
273
|
|
|
break; |
|
274
|
|
|
|
|
275
|
|
|
case 'UPPER': |
|
276
|
185 |
|
$newName = implode('', array_map('strtoupper', $words)); |
|
277
|
1 |
|
break; |
|
278
|
1 |
|
|
|
279
|
|
|
default: |
|
280
|
|
|
throw new InvalidConfiguration('Naming scheme ' . $namingScheme . ' unknown'); |
|
281
|
|
|
} |
|
282
|
184 |
|
|
|
283
|
168 |
|
return $newName; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
16 |
|
/** |
|
287
|
|
|
* Get the value for $attribute from $values using $arrayGlue |
|
288
|
|
|
* |
|
289
|
|
|
* @param string $attribute The key for $values |
|
290
|
|
|
* @param array $values |
|
291
|
|
|
* @param string $arrayGlue |
|
292
|
|
|
* @return string |
|
293
|
|
|
* @throws InvalidConfiguration |
|
294
|
|
|
*/ |
|
295
|
|
|
protected function getValue($attribute, $values, $arrayGlue) |
|
296
|
|
|
{ |
|
297
|
16 |
|
$placeholder = '%' . $attribute . '%'; |
|
298
|
|
|
if (preg_match('/\[(-?\d+\*?)\]$/', $attribute, $arrayAccessor)) { |
|
299
|
16 |
|
$attribute = substr($attribute, 0, strpos($attribute, '[')); |
|
300
|
11 |
|
$arrayAccessor = $arrayAccessor[1]; |
|
301
|
11 |
|
} else { |
|
302
|
|
|
$arrayAccessor = ''; |
|
303
|
11 |
|
} |
|
304
|
2 |
|
|
|
305
|
|
|
// throw when the variable is unknown |
|
306
|
|
|
if (!array_key_exists($attribute, $values)) { |
|
307
|
9 |
|
throw new InvalidConfiguration( |
|
308
|
9 |
|
'Template invalid: Placeholder ' . $placeholder . ' is not allowed' |
|
309
|
|
|
); |
|
310
|
|
|
} |
|
311
|
14 |
|
|
|
312
|
|
|
if (is_scalar($values[$attribute]) || is_null($values[$attribute])) { |
|
313
|
|
|
return (string) $values[$attribute]; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
return $this->arrayToString($values[$attribute], $arrayAccessor, $arrayGlue); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Convert array to string using indexes defined by $accessor |
|
321
|
|
|
* |
|
322
|
|
|
* @param array $array |
|
323
|
|
|
* @param string $accessor |
|
324
|
|
|
* @param string $glue |
|
325
|
|
|
* @return string |
|
326
|
|
|
*/ |
|
327
|
|
|
protected function arrayToString(array $array, $accessor, $glue) |
|
328
|
|
|
{ |
|
329
|
|
|
if (isset($accessor[0])) { |
|
330
|
|
|
$from = $accessor[0] === '-' ? |
|
331
|
|
|
count($array) - abs($accessor) : (int) $accessor; |
|
332
|
|
|
|
|
333
|
|
|
if ($from >= count($array)) { |
|
334
|
|
|
return ''; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
$array = substr($accessor, -1) === '*' ? |
|
338
|
|
|
array_slice($array, $from) : [ $array[$from] ]; |
|
|
|
|
|
|
339
|
|
|
} |
|
340
|
|
|
|
|
341
|
|
|
return implode($glue, $array); |
|
342
|
|
|
} |
|
343
|
|
|
} |
|
344
|
|
|
|