8
8
9
9
class ModelLexer implements Lexer
10
10
{
11
+ private static $ relationships = [
12
+ 'belongsto ' => 'belongsTo ' ,
13
+ 'hasone ' => 'hasOne ' ,
14
+ 'hasmany ' => 'hasMany ' ,
15
+ ];
16
+
11
17
private static $ dataTypes = [
12
18
'bigincrements ' => 'bigIncrements ' ,
13
19
'biginteger ' => 'bigInteger ' ,
@@ -122,6 +128,18 @@ private function buildModel(string $name, array $columns)
122
128
unset($ columns ['softdeletestz ' ]);
123
129
}
124
130
131
+ if (isset ($ columns ['relationships ' ])) {
132
+ if (is_array ($ columns ['relationships ' ])) {
133
+ foreach ($ columns ['relationships ' ] as $ type => $ relationships ) {
134
+ foreach (explode (', ' , $ relationships ) as $ reference ) {
135
+ $ model ->addRelationship (self ::$ relationships [strtolower ($ type )], trim ($ reference ));
136
+ }
137
+ }
138
+ }
139
+
140
+ unset($ columns ['relationships ' ]);
141
+ }
142
+
125
143
if (!isset ($ columns ['id ' ])) {
126
144
$ column = $ this ->buildColumn ('id ' , 'id ' );
127
145
$ model ->addColumn ($ column );
@@ -130,44 +148,48 @@ private function buildModel(string $name, array $columns)
130
148
foreach ($ columns as $ name => $ definition ) {
131
149
$ column = $ this ->buildColumn ($ name , $ definition );
132
150
$ model ->addColumn ($ column );
151
+
152
+ if ($ column ->name () !== 'id ' && $ column ->dataType () === 'id ' ) {
153
+ if ($ column ->attributes ()) {
154
+ $ model ->addRelationship ('belongsTo ' , $ column ->attributes ()[0 ] . ': ' . $ column ->name ());
155
+ } else {
156
+ $ model ->addRelationship ('belongsTo ' , $ column ->name ());
157
+ }
158
+ }
133
159
}
134
160
135
161
return $ model ;
136
162
}
137
163
138
- private function buildColumn (string $ name , $ definition )
164
+ private function buildColumn (string $ name , string $ definition )
139
165
{
140
166
$ data_type = 'string ' ;
141
167
$ modifiers = [];
142
168
143
- if ($ name === 'relationships ' && is_array ($ definition )) {
144
- $ attributes = $ definition ;
145
- } else {
146
- $ tokens = explode (' ' , $ definition );
147
- foreach ($ tokens as $ token ) {
148
- $ parts = explode (': ' , $ token );
149
- $ value = $ parts [0 ];
150
-
151
- if ($ value === 'id ' ) {
152
- $ data_type = 'id ' ;
153
- if (isset ($ parts [1 ])) {
154
- $ attributes = [$ parts [1 ]];
155
- }
156
- } elseif (isset (self ::$ dataTypes [strtolower ($ value )])) {
157
- $ attributes = $ parts [1 ] ?? null ;
158
- $ data_type = self ::$ dataTypes [strtolower ($ value )];
159
- if (!empty ($ attributes )) {
160
- $ attributes = explode (', ' , $ attributes );
161
- }
169
+ $ tokens = explode (' ' , $ definition );
170
+ foreach ($ tokens as $ token ) {
171
+ $ parts = explode (': ' , $ token );
172
+ $ value = $ parts [0 ];
173
+
174
+ if ($ value === 'id ' ) {
175
+ $ data_type = 'id ' ;
176
+ if (isset ($ parts [1 ])) {
177
+ $ attributes = [$ parts [1 ]];
178
+ }
179
+ } elseif (isset (self ::$ dataTypes [strtolower ($ value )])) {
180
+ $ attributes = $ parts [1 ] ?? null ;
181
+ $ data_type = self ::$ dataTypes [strtolower ($ value )];
182
+ if (!empty ($ attributes )) {
183
+ $ attributes = explode (', ' , $ attributes );
162
184
}
185
+ }
163
186
164
- if (isset (self ::$ modifiers [strtolower ($ value )])) {
165
- $ modifierAttributes = $ parts [1 ] ?? null ;
166
- if (empty ($ modifierAttributes )) {
167
- $ modifiers [] = self ::$ modifiers [strtolower ($ value )];
168
- } else {
169
- $ modifiers [] = [self ::$ modifiers [strtolower ($ value )] => $ modifierAttributes ];
170
- }
187
+ if (isset (self ::$ modifiers [strtolower ($ value )])) {
188
+ $ modifierAttributes = $ parts [1 ] ?? null ;
189
+ if (empty ($ modifierAttributes )) {
190
+ $ modifiers [] = self ::$ modifiers [strtolower ($ value )];
191
+ } else {
192
+ $ modifiers [] = [self ::$ modifiers [strtolower ($ value )] => $ modifierAttributes ];
171
193
}
172
194
}
173
195
}
0 commit comments