1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PrismX\Generators\Support; |
4
|
|
|
|
5
|
|
|
class Lexer |
6
|
|
|
{ |
7
|
|
|
private static $dataTypes = [ |
8
|
|
|
'bigincrements' => 'bigIncrements', |
9
|
|
|
'biginteger' => 'bigInteger', |
10
|
|
|
'binary' => 'binary', |
11
|
|
|
'boolean' => 'boolean', |
12
|
|
|
'char' => 'char', |
13
|
|
|
'date' => 'date', |
14
|
|
|
'datetime' => 'dateTime', |
15
|
|
|
'datetimetz' => 'dateTimeTz', |
16
|
|
|
'decimal' => 'decimal', |
17
|
|
|
'double' => 'double', |
18
|
|
|
'enum' => 'enum', |
19
|
|
|
'float' => 'float', |
20
|
|
|
'geometry' => 'geometry', |
21
|
|
|
'geometrycollection' => 'geometryCollection', |
22
|
|
|
'increments' => 'increments', |
23
|
|
|
'integer' => 'integer', |
24
|
|
|
'ipaddress' => 'ipAddress', |
25
|
|
|
'json' => 'json', |
26
|
|
|
'jsonb' => 'jsonb', |
27
|
|
|
'linestring' => 'lineString', |
28
|
|
|
'longtext' => 'longText', |
29
|
|
|
'macaddress' => 'macAddress', |
30
|
|
|
'mediumincrements' => 'mediumIncrements', |
31
|
|
|
'mediuminteger' => 'mediumInteger', |
32
|
|
|
'mediumtext' => 'mediumText', |
33
|
|
|
'morphs' => 'morphs', |
34
|
|
|
'uuidmorphs' => 'uuidMorphs', |
35
|
|
|
'multilinestring' => 'multiLineString', |
36
|
|
|
'multipoint' => 'multiPoint', |
37
|
|
|
'multipolygon' => 'multiPolygon', |
38
|
|
|
'nullablemorphs' => 'nullableMorphs', |
39
|
|
|
'nullableuuidmorphs' => 'nullableUuidMorphs', |
40
|
|
|
'nullabletimestamps' => 'nullableTimestamps', |
41
|
|
|
'point' => 'point', |
42
|
|
|
'polygon' => 'polygon', |
43
|
|
|
'remembertoken' => 'rememberToken', |
44
|
|
|
'set' => 'set', |
45
|
|
|
'smallincrements' => 'smallIncrements', |
46
|
|
|
'smallinteger' => 'smallInteger', |
47
|
|
|
'softdeletes' => 'softDeletes', |
48
|
|
|
'softdeletestz' => 'softDeletesTz', |
49
|
|
|
'string' => 'string', |
50
|
|
|
'text' => 'text', |
51
|
|
|
'time' => 'time', |
52
|
|
|
'timetz' => 'timeTz', |
53
|
|
|
'timestamp' => 'timestamp', |
54
|
|
|
'timestamptz' => 'timestampTz', |
55
|
|
|
'timestamps' => 'timestamps', |
56
|
|
|
'timestampstz' => 'timestampsTz', |
57
|
|
|
'tinyincrements' => 'tinyIncrements', |
58
|
|
|
'tinyinteger' => 'tinyInteger', |
59
|
|
|
'unsignedbiginteger' => 'unsignedBigInteger', |
60
|
|
|
'unsigneddecimal' => 'unsignedDecimal', |
61
|
|
|
'unsignedinteger' => 'unsignedInteger', |
62
|
|
|
'unsignedmediuminteger' => 'unsignedMediumInteger', |
63
|
|
|
'unsignedsmallinteger' => 'unsignedSmallInteger', |
64
|
|
|
'unsignedtinyinteger' => 'unsignedTinyInteger', |
65
|
|
|
'uuid' => 'uuid', |
66
|
|
|
'year' => 'year', |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
private static $modifiers = [ |
70
|
|
|
'autoincrement' => 'autoIncrement', |
71
|
|
|
'charset' => 'charset', |
72
|
|
|
'collation' => 'collation', |
73
|
|
|
'default' => 'default', |
74
|
|
|
'nullable' => 'nullable', |
75
|
|
|
'unsigned' => 'unsigned', |
76
|
|
|
'usecurrent' => 'useCurrent', |
77
|
|
|
'always' => 'always', |
78
|
|
|
'unique' => 'unique', |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
public function analyze(array $tokens): array |
82
|
|
|
{ |
83
|
|
|
$registry = []; |
84
|
|
|
|
85
|
|
|
foreach ($tokens as $name => $definition) { |
86
|
|
|
$registry[$name] = $this->buildModel($name, $definition); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $registry; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function buildModel(string $name, array $columns) |
93
|
|
|
{ |
94
|
|
|
$model = new Model($name); |
95
|
|
|
|
96
|
|
|
if (isset($columns['timestamps'])) { |
97
|
|
|
if ($columns['timestamps'] === false) { |
98
|
|
|
$model->disableTimestamps(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
unset($columns['timestamps']); |
102
|
|
|
} elseif (isset($columns['timestampstz'])) { |
103
|
|
|
$model->enableTimestamps(true); |
104
|
|
|
unset($columns['timestampstz']); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (isset($columns['softdeletes'])) { |
108
|
|
|
$model->enableSoftDeletes(); |
109
|
|
|
unset($columns['softdeletes']); |
110
|
|
|
} elseif (isset($columns['softdeletestz'])) { |
111
|
|
|
$model->enableSoftDeletes(true); |
112
|
|
|
unset($columns['softdeletestz']); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (! isset($columns['id'])) { |
116
|
|
|
$column = $this->buildColumn('id', 'id'); |
117
|
|
|
$model->addColumn($column); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
foreach ($columns as $name => $definition) { |
121
|
|
|
$column = $this->buildColumn($name, $definition); |
122
|
|
|
$model->addColumn($column); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $model; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function buildColumn(string $name, string $definition) |
129
|
|
|
{ |
130
|
|
|
$data_type = 'string'; |
131
|
|
|
$modifiers = []; |
132
|
|
|
|
133
|
|
|
$tokens = explode(' ', $definition); |
134
|
|
|
foreach ($tokens as $token) { |
135
|
|
|
$parts = explode(':', $token); |
136
|
|
|
$value = $parts[0]; |
137
|
|
|
|
138
|
|
|
if ($value === 'id') { |
139
|
|
|
$data_type = 'id'; |
140
|
|
|
if (isset($parts[1])) { |
141
|
|
|
$attributes = [$parts[1]]; |
142
|
|
|
} |
143
|
|
|
} elseif (isset(self::$dataTypes[strtolower($value)])) { |
144
|
|
|
$attributes = $parts[1] ?? null; |
145
|
|
|
$data_type = self::$dataTypes[strtolower($value)]; |
146
|
|
|
if (! empty($attributes)) { |
147
|
|
|
$attributes = explode(',', $attributes); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (isset(self::$modifiers[strtolower($value)])) { |
152
|
|
|
$modifierAttributes = $parts[1] ?? null; |
153
|
|
|
if (empty($modifierAttributes)) { |
154
|
|
|
$modifiers[] = self::$modifiers[strtolower($value)]; |
155
|
|
|
} else { |
156
|
|
|
$modifiers[] = [self::$modifiers[strtolower($value)] => $modifierAttributes]; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return new Column($name, $data_type, $modifiers, $attributes ?? []); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|