@@ -9,66 +9,76 @@ class GeneratorField
9
9
{
10
10
/** @var string */
11
11
public string $ name ;
12
- public string $ dbInput ;
13
- public $ htmlType ;
14
- public $ fieldType ;
15
- public string $ description ;
12
+ public string $ dbType ;
13
+ public array $ dbTypeParams = [];
14
+ public array $ dbExtraFunctions = [];
16
15
17
- public array $ htmlValues ;
16
+ public string $ htmlType ;
17
+ public array $ htmlValues = [];
18
18
19
- public string $ migrationText ;
20
- public string $ foreignKeyText ;
19
+ public string $ description ;
21
20
public string $ validations = '' ;
22
-
23
21
public bool $ isSearchable = true ;
24
22
public bool $ isFillable = true ;
25
23
public bool $ isPrimary = false ;
26
24
public bool $ inForm = true ;
27
25
public bool $ inIndex = true ;
28
26
public bool $ inView = true ;
27
+
28
+ public string $ migrationText = '' ;
29
+ public string $ foreignKeyText = '' ;
30
+
29
31
public bool $ isNotNull = false ;
30
32
31
33
public int $ numberDecimalPoints = 2 ;
32
34
33
35
/**
34
- * @param Column $column
35
36
* @param $dbInput
36
37
*/
37
- public function parseDBType ($ dbInput, $ column = null )
38
+ public function parseDBType ($ dbInput )
38
39
{
39
- $ this -> dbInput = $ dbInput ;
40
- if (! is_null ( $ column )) {
41
- $ this ->dbInput = ( $ column -> getLength () > 0 ) ? $ this -> dbInput . ' , ' . $ column -> getLength () : $ this -> dbInput ;
42
- $ this -> dbInput = (! $ column -> getNotnull ()) ? $ this -> dbInput . ' :nullable ' : $ this -> dbInput ;
40
+ if (!Str:: contains ( $ dbInput, ' : ' )) {
41
+ $ this -> dbType = $ dbInput ;
42
+ $ this ->prepareMigrationText () ;
43
+ return ;
43
44
}
45
+
46
+ $ dbInputArr = explode (': ' , $ dbInput );
47
+ $ dbType = (string )array_shift ($ dbInputArr );
48
+
49
+ if (Str::contains ($ dbType , ', ' )) {
50
+ $ dbTypeArr = explode (', ' , $ dbType );
51
+ $ this ->dbType = (string )array_shift ($ dbTypeArr );
52
+ $ this ->dbTypeParams = $ dbTypeArr ;
53
+ } else {
54
+ $ this ->dbType = $ dbType ;
55
+ }
56
+
57
+ $ this ->dbExtraFunctions = $ dbInputArr ;
58
+
59
+ // if (!is_null($column)) {
60
+ // $this->dbType = ($column->getLength() > 0) ? $this->dbType.','.$column->getLength() : $this->dbType;
61
+ // $this->dbType = (!$column->getNotnull()) ? $this->dbType.':nullable' : $this->dbType;
62
+ // }
63
+
44
64
$ this ->prepareMigrationText ();
45
65
}
46
66
47
67
public function parseHtmlInput ($ htmlInput )
48
68
{
49
- $ this ->htmlValues = [];
50
-
51
69
if (empty ($ htmlInput )) {
52
70
$ this ->htmlType = 'text ' ;
53
-
54
71
return ;
55
72
}
56
73
57
- if (Str::contains ($ htmlInput , 'selectTable ' )) {
58
- $ inputsArr = explode (': ' , $ htmlInput );
59
- $ this ->htmlType = array_shift ($ inputsArr );
60
- $ this ->htmlValues = $ inputsArr ;
61
-
74
+ if (!Str::contains ($ htmlInput , ': ' )) {
75
+ $ this ->htmlType = $ htmlInput ;
62
76
return ;
63
77
}
64
78
65
- $ inputsArr = explode (', ' , $ htmlInput );
66
-
67
- $ this ->htmlType = array_shift ($ inputsArr );
68
-
69
- if (count ($ inputsArr ) > 0 ) {
70
- $ this ->htmlValues = $ inputsArr ;
71
- }
79
+ $ htmlInputArr = explode (': ' , $ htmlInput );
80
+ $ this ->htmlType = (string )array_shift ($ htmlInputArr );
81
+ $ this ->htmlValues = explode (', ' , implode (': ' , $ htmlInputArr ));
72
82
}
73
83
74
84
public function parseOptions ($ options )
@@ -103,54 +113,98 @@ public function parseOptions($options)
103
113
104
114
private function prepareMigrationText ()
105
115
{
106
- $ inputsArr = explode (': ' , $ this ->dbInput );
107
116
$ this ->migrationText = '$table-> ' ;
117
+ $ this ->migrationText .= $ this ->dbType . "(' " . $ this ->name . "' " ;
108
118
109
- $ fieldTypeParams = explode (', ' , array_shift ($ inputsArr ));
110
- $ this ->fieldType = array_shift ($ fieldTypeParams );
111
- $ this ->migrationText .= $ this ->fieldType ."(' " .$ this ->name ."' " ;
119
+ if (!count ($ this ->dbTypeParams ) and !count ($ this ->dbExtraFunctions )) {
120
+ $ this ->migrationText .= "); " ;
121
+ return ;
122
+ }
112
123
113
- if ($ this ->fieldType == 'enum ' ) {
114
- $ this ->migrationText .= ', [ ' ;
115
- foreach ($ fieldTypeParams as $ param ) {
116
- $ this ->migrationText .= "' " .$ param ."', " ;
117
- }
118
- $ this ->migrationText = substr ($ this ->migrationText , 0 , strlen ($ this ->migrationText ) - 1 );
119
- $ this ->migrationText .= '] ' ;
120
- } else {
121
- foreach ($ fieldTypeParams as $ param ) {
122
- $ this ->migrationText .= ', ' .$ param ;
124
+ if (count ($ this ->dbTypeParams )) {
125
+ // if ($this->dbType === 'enum') {
126
+ // $this->migrationText .= ', [';
127
+ // foreach ($fieldTypeParams as $param) {
128
+ // $this->migrationText .= "'".$param."',";
129
+ // }
130
+ // $this->migrationText = substr($this->migrationText, 0, strlen($this->migrationText) - 1);
131
+ // $this->migrationText .= ']';
132
+ // }
133
+ foreach ($ this ->dbTypeParams as $ dbTypeParam ) {
134
+ $ this ->migrationText .= ', ' . $ dbTypeParam ;
123
135
}
124
136
}
125
137
126
- $ this ->migrationText .= ') ' ;
127
- $ this ->foreignKeyText = '' ;
138
+ $ this ->migrationText .= ") " ;
139
+
140
+ if (!count ($ this ->dbExtraFunctions )) {
141
+ $ this ->migrationText .= "; " ;
142
+ return ;
143
+ }
128
144
129
- foreach ($ inputsArr as $ input ) {
130
- $ inputParams = explode (', ' , $ input );
131
- $ functionName = array_shift ($ inputParams );
132
- if ($ functionName == 'foreign ' ) {
133
- $ foreignTable = array_shift ($ inputParams );
134
- $ foreignField = array_shift ($ inputParams );
135
- $ this ->foreignKeyText .= "\$table->foreign(' " .$ this ->name ."')->references(' " .$ foreignField ."')->on(' " .$ foreignTable ."') " ;
136
- if (count ($ inputParams )) {
137
- $ cascade = array_shift ($ inputParams );
138
- if ($ cascade == 'cascade ' ) {
145
+ $ this ->foreignKeyText = '' ;
146
+ foreach ($ this ->dbExtraFunctions as $ dbExtraFunction ) {
147
+ $ dbExtraFunctionArr = explode (', ' , $ dbExtraFunction );
148
+ $ functionName = (string )array_shift ($ dbExtraFunctionArr );
149
+ if ($ functionName === 'foreign ' ) {
150
+ $ foreignTable = array_shift ($ dbExtraFunctionArr );
151
+ $ foreignField = array_shift ($ dbExtraFunctionArr );
152
+ $ this ->foreignKeyText .= "\$table->foreign(' " . $ this ->name . "')->references(' " . $ foreignField . "')->on(' " . $ foreignTable . "') " ;
153
+ if (count ($ dbExtraFunctionArr )) {
154
+ $ cascade = array_shift ($ dbExtraFunctionArr );
155
+ if ($ cascade === 'cascade ' ) {
139
156
$ this ->foreignKeyText .= "->onUpdate('cascade')->onDelete('cascade') " ;
140
157
}
141
158
}
142
159
$ this ->foreignKeyText .= '; ' ;
143
160
} else {
144
- $ this ->migrationText .= '-> ' . $ functionName ;
161
+ $ this ->migrationText .= '-> ' . $ functionName ;
145
162
$ this ->migrationText .= '( ' ;
146
- $ this ->migrationText .= implode (', ' , $ inputParams );
163
+ $ this ->migrationText .= implode (', ' , $ dbExtraFunctionArr );
147
164
$ this ->migrationText .= ') ' ;
148
165
}
149
166
}
150
167
151
168
$ this ->migrationText .= '; ' ;
152
169
}
153
170
171
+ public static function parseFieldFromConsoleInput (string $ fieldInput , string $ validations = '' ): self
172
+ {
173
+ /*
174
+ * Field Input Format: field_name <space> db_type <space> html_type(optional) <space> options(optional)
175
+ * Options are to skip the field from certain criteria like searchable, fillable, not in form, not in index
176
+ * Searchable (s), Fillable (f), In Form (if), In Index (ii)
177
+ * Sample Field Inputs
178
+ *
179
+ * title string text
180
+ * body text textarea
181
+ * name string,20 text
182
+ * post_id integer:unsigned:nullable
183
+ * post_id unsignedinteger:nullable:foreign,posts,id
184
+ * password string text if,ii,s - options will skip field from being added in form, in index and searchable
185
+ */
186
+
187
+ $ field = new self ();
188
+ $ fieldInputArr = explode (' ' , $ fieldInput );
189
+ $ field ->name = $ fieldInputArr [0 ];
190
+
191
+ $ field ->parseDBType ($ fieldInputArr [1 ]);
192
+
193
+ $ field ->parseHtmlInput ($ fieldInputArr [2 ]);
194
+
195
+ if (count ($ fieldInputArr ) > 3 ) {
196
+ $ field ->parseOptions ($ fieldInputArr [3 ]);
197
+ }
198
+
199
+ $ field ->validations = $ validations ;
200
+
201
+ if (str_contains ($ field ->validations , 'required ' )) {
202
+ $ field ->isNotNull = true ;
203
+ }
204
+
205
+ return $ field ;
206
+ }
207
+
154
208
public static function parseFieldFromFile ($ fieldInput ): self
155
209
{
156
210
$ field = new self ();
@@ -180,7 +234,7 @@ public function getTitle()
180
234
public function variables ()
181
235
{
182
236
return [
183
- 'fieldName ' => $ this ->name ,
237
+ 'fieldName ' => $ this ->name ,
184
238
'fieldTitle ' => $ this ->getTitle (),
185
239
];
186
240
}
0 commit comments