-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathColumnParser.php
More file actions
417 lines (365 loc) · 13.1 KB
/
ColumnParser.php
File metadata and controls
417 lines (365 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
declare(strict_types=1);
namespace Migrations\Util;
use Cake\Collection\Collection;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use Migrations\Db\Adapter\AdapterInterface;
use ReflectionClass;
/**
* Utility class used to parse arguments passed to a ``bake migration`` class
*/
class ColumnParser
{
/**
* Regex used to parse the column definition passed through the shell
*
* @var string
*/
protected string $regexpParseColumn = '/
^
(\w+)
(?::(\w+\??
(?:\[
(?:[0-9]|[1-9][0-9]+)
(?:,(?:[0-9]|[1-9][0-9]+))?
\])?
))?
(?::default\[([^\]]+)\])?
(?::(\w+))?
(?::(\w+))?
$
/x';
/**
* Regex used to parse the field type and length
*
* @var string
*/
protected string $regexpParseField = '/(\w+\??)\[([0-9,]+)\]/';
/**
* Parses a list of arguments into an array of fields
*
* @param array<int, string> $arguments A list of arguments being parsed
* @return array<string, array>
*/
public function parseFields(array $arguments): array
{
$fields = [];
$arguments = $this->validArguments($arguments);
foreach ($arguments as $field) {
preg_match($this->regexpParseColumn, $field, $matches);
$field = $matches[1];
$type = Hash::get($matches, 2, '');
$defaultValue = Hash::get($matches, 3);
$indexType = Hash::get($matches, 4);
$typeIsPk = in_array($type, ['primary', 'primary_key'], true);
$isPrimaryKey = false;
if ($typeIsPk || in_array($indexType, ['primary', 'primary_key'], true)) {
$isPrimaryKey = true;
if ($typeIsPk) {
$type = 'primary';
}
}
// Handle references - convert to integer type
$isReference = in_array($type, ['references', 'references?'], true);
if ($isReference) {
$type = str_contains($type, '?') ? 'integer?' : 'integer';
}
$nullable = (bool)strpos($type, '?');
$type = $nullable ? str_replace('?', '', $type) : $type;
[$type, $length] = $this->getTypeAndLength($field, $type);
$fields[$field] = [
'columnType' => $type,
'options' => [
'null' => $nullable,
'default' => $this->parseDefaultValue($defaultValue, $type ?? 'string'),
],
];
if ($length !== null) {
if (is_array($length)) {
[$fields[$field]['options']['precision'], $fields[$field]['options']['scale']] = $length;
} else {
$fields[$field]['options']['limit'] = $length;
}
}
if ($isPrimaryKey === true && $type === 'integer') {
$fields[$field]['options']['autoIncrement'] = true;
}
}
return $fields;
}
/**
* Parses a list of arguments into an array of indexes
*
* @param array<int, string> $arguments A list of arguments being parsed
* @return array<string, array>
*/
public function parseIndexes(array $arguments): array
{
$indexes = [];
$arguments = $this->validArguments($arguments);
foreach ($arguments as $field) {
preg_match($this->regexpParseColumn, $field, $matches);
$field = $matches[1];
$type = Hash::get($matches, 2);
$indexType = Hash::get($matches, 4);
$indexName = Hash::get($matches, 5);
// Skip references - they create foreign keys, not indexes
if ($type && str_starts_with($type, 'references')) {
continue;
}
if (
in_array($type, ['primary', 'primary_key'], true) ||
in_array($indexType, ['primary', 'primary_key'], true) ||
$indexType === null
) {
continue;
}
$indexUnique = false;
if ($indexType === 'unique') {
$indexUnique = true;
}
$indexName = $this->getIndexName($field, $indexType, $indexName, $indexUnique);
if (empty($indexes[$indexName])) {
$indexes[$indexName] = [
'columns' => [],
'options' => [
'unique' => $indexUnique,
'name' => $indexName,
],
];
}
$indexes[$indexName]['columns'][] = $field;
}
return $indexes;
}
/**
* Parses a list of arguments into an array of fields composing the primary key
* of the table
*
* @param array<int, string> $arguments A list of arguments being parsed
* @return array<string>
*/
public function parsePrimaryKey(array $arguments): array
{
$primaryKey = [];
$arguments = $this->validArguments($arguments);
foreach ($arguments as $field) {
preg_match($this->regexpParseColumn, $field, $matches);
$field = $matches[1];
$type = Hash::get($matches, 2);
$indexType = Hash::get($matches, 4);
if (
in_array($type, ['primary', 'primary_key'], true)
|| in_array($indexType, ['primary', 'primary_key'], true)
) {
$primaryKey[] = $field;
}
}
return $primaryKey;
}
/**
* Parses a list of arguments into an array of foreign key constraints
*
* @param array<int, string> $arguments A list of arguments being parsed
* @return array<string, array>
*/
public function parseForeignKeys(array $arguments): array
{
$foreignKeys = [];
$arguments = $this->validArguments($arguments);
foreach ($arguments as $field) {
preg_match($this->regexpParseColumn, $field, $matches);
$fieldName = $matches[1];
$type = Hash::get($matches, 2, '');
$indexType = Hash::get($matches, 4);
$indexName = Hash::get($matches, 5);
// Check if type is 'references' or 'references?'
$isReference = str_starts_with($type, 'references');
if (!$isReference) {
continue;
}
// Determine referenced table
// If indexType is provided, use it as the referenced table name
// Otherwise, infer from field name (e.g., category_id -> categories)
$referencedTable = $indexType;
if (!$referencedTable) {
// Remove common suffixes like _id and pluralize
$referencedTable = preg_replace('/_id$/', '', $fieldName);
$referencedTable = Inflector::pluralize($referencedTable);
}
// Generate constraint name
$constraintName = $indexName ?: 'fk_' . $fieldName;
$foreignKeys[$constraintName] = [
'type' => 'foreign',
'columns' => [$fieldName],
'references' => [$referencedTable, 'id'],
'update' => 'CASCADE',
'delete' => 'CASCADE',
];
}
return $foreignKeys;
}
/**
* Returns a list of only valid arguments
*
* @param array<string> $arguments A list of arguments
* @return array<string>
*/
public function validArguments(array $arguments): array
{
$collection = new Collection($arguments);
return $collection->filter(function ($value, $field) {
return preg_match($this->regexpParseColumn, (string)$field);
})->toArray();
}
/**
* Get the type and length of a field based on the field and the type passed
*
* @param string $field Name of field
* @param string|null $type User-specified type
* @return array{0: string|null, 1: int|array<int>|null} First value is the field type, second value is the field length. If no length
* can be extracted, null is returned for the second value
*/
public function getTypeAndLength(string $field, ?string $type): array
{
if ($type && preg_match($this->regexpParseField, $type, $matches)) {
$length = $matches[2];
if (str_contains($length, ',')) {
$length = array_map('intval', explode(',', $length));
} else {
$length = (int)$length;
}
return [$matches[1], $length];
}
/** @var string $fieldType */
$fieldType = $this->getType($field, $type);
$length = $this->getLength($fieldType);
return [$fieldType, $length];
}
/**
* Retrieves a type that should be used for a specific field
*
* @param string $field Name of field
* @param string|null $type User-specified type
* @return string|null
*/
public function getType(string $field, ?string $type): ?string
{
$reflector = new ReflectionClass(AdapterInterface::class);
$collection = new Collection($reflector->getConstants());
$validTypes = $collection->filter(function ($value, $constant) {
return substr($constant, 0, strlen('TYPE_')) === 'TYPE_' ||
substr($constant, 0, strlen('PHINX_TYPE_')) === 'PHINX_TYPE_';
})->toArray();
$fieldType = $type;
if ($type === null || !in_array($type, $validTypes, true)) {
if ($type === 'primary') {
$fieldType = 'integer';
} elseif ($field === 'id') {
$fieldType = 'integer';
} elseif (in_array($field, ['created', 'modified', 'updated'], true) || substr($field, -3) === '_at') {
$fieldType = 'datetime';
} elseif (in_array($field, ['latitude', 'longitude', 'lat', 'lng'], true)) {
$fieldType = 'decimal';
} else {
$fieldType = 'string';
}
}
return $fieldType;
}
/**
* Returns the default length to be used for a given type.
*
* @param string $type User-specified type
* @return int|int[]|null
*/
public function getLength(string $type): int|array|null
{
$length = null;
if ($type === 'string') {
$length = 255;
} elseif ($type === 'tinyinteger') {
$length = 4;
} elseif ($type === 'smallinteger') {
$length = 6;
} elseif ($type === 'integer') {
$length = 11;
} elseif ($type === 'biginteger') {
$length = 20;
} elseif ($type === 'decimal') {
$length = [10, 6];
}
return $length;
}
/**
* Returns the default length to be used for a given fie
*
* @param string $field Name of field
* @param string|null $indexType Type of index
* @param string|null $indexName Name of index
* @param bool $indexUnique Whether this is a unique index or not
* @return string
*/
public function getIndexName(string $field, ?string $indexType, ?string $indexName, bool $indexUnique): string
{
if (!$indexName) {
$indexName = strtoupper('BY_' . $field);
if ($indexType === 'primary') {
$indexName = 'PRIMARY';
} elseif ($indexUnique) {
$indexName = strtoupper('UNIQUE_' . $field);
}
}
return $indexName;
}
/**
* Parses a default value string into the appropriate PHP type.
*
* Supports:
* - Booleans: true, false
* - Null: null, NULL
* - Integers: 123, -123
* - Floats: 1.5, -1.5
* - Strings: 'hello' (quoted) or unquoted values
*
* @param string|null $value The raw default value from the command line
* @param string $columnType The column type to help with type coercion
* @return string|int|float|bool|null The parsed default value
*/
public function parseDefaultValue(?string $value, string $columnType): string|int|float|bool|null
{
if ($value === null || $value === '') {
return null;
}
$lowerValue = strtolower($value);
// Handle null
if ($lowerValue === 'null') {
return null;
}
// Handle booleans
if ($lowerValue === 'true') {
return true;
}
if ($lowerValue === 'false') {
return false;
}
// Handle quoted strings - strip quotes
if (
(str_starts_with($value, "'") && str_ends_with($value, "'")) ||
(str_starts_with($value, '"') && str_ends_with($value, '"'))
) {
return substr($value, 1, -1);
}
// Handle integers
if (preg_match('/^-?[0-9]+$/', $value)) {
return (int)$value;
}
// Handle floats
if (preg_match('/^-?[0-9]+\.[0-9]+$/', $value)) {
return (float)$value;
}
// Return as-is for SQL expressions like CURRENT_TIMESTAMP
return $value;
}
}