Skip to content

Commit 2a9751c

Browse files
committed
feat: various filed fixes
1 parent 98f096f commit 2a9751c

File tree

8 files changed

+127
-114
lines changed

8 files changed

+127
-114
lines changed

src/Commands/BaseCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private function saveSchemaFile()
203203
foreach ($this->config->fields as $field) {
204204
$fileFields[] = [
205205
'name' => $field->name,
206-
'dbType' => $field->dbInput,
206+
'dbType' => $field->dbType,
207207
'htmlType' => $field->htmlType,
208208
'validations' => $field->validations,
209209
'searchable' => $field->isSearchable,
@@ -358,7 +358,7 @@ private function getFieldsFromConsole()
358358
$relation = '';
359359
}
360360

361-
$this->config->fields[] = GeneratorFieldsInputUtil::processFieldInput(
361+
$this->config->fields[] = GeneratorField::parseFieldFromConsoleInput(
362362
$fieldInputStr,
363363
$validations
364364
);

src/Common/GeneratorField.php

Lines changed: 112 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,66 +9,76 @@ class GeneratorField
99
{
1010
/** @var string */
1111
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 = [];
1615

17-
public array $htmlValues;
16+
public string $htmlType;
17+
public array $htmlValues = [];
1818

19-
public string $migrationText;
20-
public string $foreignKeyText;
19+
public string $description;
2120
public string $validations = '';
22-
2321
public bool $isSearchable = true;
2422
public bool $isFillable = true;
2523
public bool $isPrimary = false;
2624
public bool $inForm = true;
2725
public bool $inIndex = true;
2826
public bool $inView = true;
27+
28+
public string $migrationText = '';
29+
public string $foreignKeyText = '';
30+
2931
public bool $isNotNull = false;
3032

3133
public int $numberDecimalPoints = 2;
3234

3335
/**
34-
* @param Column $column
3536
* @param $dbInput
3637
*/
37-
public function parseDBType($dbInput, $column = null)
38+
public function parseDBType($dbInput)
3839
{
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;
4344
}
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+
4464
$this->prepareMigrationText();
4565
}
4666

4767
public function parseHtmlInput($htmlInput)
4868
{
49-
$this->htmlValues = [];
50-
5169
if (empty($htmlInput)) {
5270
$this->htmlType = 'text';
53-
5471
return;
5572
}
5673

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;
6276
return;
6377
}
6478

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));
7282
}
7383

7484
public function parseOptions($options)
@@ -103,54 +113,98 @@ public function parseOptions($options)
103113

104114
private function prepareMigrationText()
105115
{
106-
$inputsArr = explode(':', $this->dbInput);
107116
$this->migrationText = '$table->';
117+
$this->migrationText .= $this->dbType . "('" . $this->name . "'";
108118

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+
}
112123

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;
123135
}
124136
}
125137

126-
$this->migrationText .= ')';
127-
$this->foreignKeyText = '';
138+
$this->migrationText .= ")";
139+
140+
if (!count($this->dbExtraFunctions)) {
141+
$this->migrationText .= ";";
142+
return;
143+
}
128144

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') {
139156
$this->foreignKeyText .= "->onUpdate('cascade')->onDelete('cascade')";
140157
}
141158
}
142159
$this->foreignKeyText .= ';';
143160
} else {
144-
$this->migrationText .= '->'.$functionName;
161+
$this->migrationText .= '->' . $functionName;
145162
$this->migrationText .= '(';
146-
$this->migrationText .= implode(', ', $inputParams);
163+
$this->migrationText .= implode(', ', $dbExtraFunctionArr);
147164
$this->migrationText .= ')';
148165
}
149166
}
150167

151168
$this->migrationText .= ';';
152169
}
153170

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+
154208
public static function parseFieldFromFile($fieldInput): self
155209
{
156210
$field = new self();
@@ -180,7 +234,7 @@ public function getTitle()
180234
public function variables()
181235
{
182236
return [
183-
'fieldName' => $this->name,
237+
'fieldName' => $this->name,
184238
'fieldTitle' => $this->getTitle(),
185239
];
186240
}

src/Generators/FactoryGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private function generateFields(): string
8585
$rule = $rules[$field->name];
8686
}
8787

88-
switch (strtolower($field->fieldType)) {
88+
switch (strtolower($field->dbType)) {
8989
case 'integer':
9090
case 'unsignedinteger':
9191
case 'smallinteger':

src/Generators/ModelGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private function generateRules(): array
186186
$rule[] = 'nullable';
187187
}
188188

189-
switch ($field->fieldType) {
189+
switch ($field->dbType) {
190190
case 'integer':
191191
$rule[] = 'integer';
192192
break;
@@ -202,7 +202,7 @@ private function generateRules(): array
202202
$rule[] = 'string';
203203

204204
// Enforce a maximum string length if possible.
205-
foreach (explode(':', $field->dbInput) as $key => $value) {
205+
foreach (explode(':', $field->dbType) as $key => $value) {
206206
if (preg_match('/string,(\d+)/', $value, $matches)) {
207207
$rule[] = 'max:'.$matches[1];
208208
}
@@ -263,7 +263,7 @@ public function generateCasts(): array
263263

264264
$rule = "'".$field->name."' => ";
265265

266-
switch (strtolower($field->fieldType)) {
266+
switch (strtolower($field->dbType)) {
267267
case 'integer':
268268
case 'increments':
269269
case 'smallinteger':

src/Generators/SwaggerGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static function generateTypes(array $inputFields): array
1212

1313
/** @var GeneratorField $field */
1414
foreach ($inputFields as $field) {
15-
$fieldData = self::getFieldType($field->fieldType);
15+
$fieldData = self::getFieldType($field->dbType);
1616

1717
if (empty($fieldData['fieldType'])) {
1818
continue;

src/Utils/GeneratorFieldsInputUtil.php

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace InfyOm\Generator\Utils;
44

5-
use InfyOm\Generator\Common\GeneratorField;
6-
75
class GeneratorFieldsInputUtil
86
{
97
public static function validateFieldInput($fieldInputStr): bool
@@ -17,45 +15,6 @@ public static function validateFieldInput($fieldInputStr): bool
1715
return true;
1816
}
1917

20-
public static function processFieldInput(string $fieldInput, string $validations): GeneratorField
21-
{
22-
/*
23-
* Field Input Format: field_name <space> db_type <space> html_type(optional) <space> options(optional)
24-
* Options are to skip the field from certain criteria like searchable, fillable, not in form, not in index
25-
* Searchable (s), Fillable (f), In Form (if), In Index (ii)
26-
* Sample Field Inputs
27-
*
28-
* title string text
29-
* body text textarea
30-
* name string,20 text
31-
* post_id integer:unsigned:nullable
32-
* post_id integer:unsigned:nullable:foreign,posts,id
33-
* password string text if,ii,s - options will skip field from being added in form, in index and searchable
34-
*/
35-
36-
$fieldInputsArr = explode(' ', $fieldInput);
37-
38-
$field = new GeneratorField();
39-
$field->name = $fieldInputsArr[0];
40-
$field->parseDBType($fieldInputsArr[1]);
41-
42-
if (count($fieldInputsArr) > 2) {
43-
$field->parseHtmlInput($fieldInputsArr[2]);
44-
}
45-
46-
if (count($fieldInputsArr) > 3) {
47-
$field->parseOptions($fieldInputsArr[3]);
48-
}
49-
50-
$field->validations = $validations;
51-
52-
if (str_contains($field->validations, 'required')) {
53-
$field->isNotNull = true;
54-
}
55-
56-
return $field;
57-
}
58-
5918
/**
6019
* Prepare string of associative array.
6120
*/

src/Utils/HTMLFieldGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public static function generateHTML(GeneratorField $field, $templateType): strin
2020
case 'select':
2121
case 'enum':
2222
$viewName = 'select';
23-
$radioLabels = GeneratorFieldsInputUtil::prepareKeyValueArrFromLabelValueStr($field->htmlValues);
23+
$keyValues = GeneratorFieldsInputUtil::prepareKeyValueArrFromLabelValueStr($field->htmlValues);
2424

2525
$variables = [
26-
'selectValues' => GeneratorFieldsInputUtil::prepareKeyValueArrayStr($radioLabels),
26+
'selectValues' => GeneratorFieldsInputUtil::prepareKeyValueArrayStr($keyValues),
2727
];
2828
break;
2929
case 'checkbox':
@@ -35,10 +35,10 @@ public static function generateHTML(GeneratorField $field, $templateType): strin
3535
$variables['checkboxVal'] = $checkboxValue;
3636
break;
3737
case 'radio':
38-
$radioLabels = GeneratorFieldsInputUtil::prepareKeyValueArrFromLabelValueStr($field->htmlValues);
38+
$keyValues = GeneratorFieldsInputUtil::prepareKeyValueArrFromLabelValueStr($field->htmlValues);
3939

4040
$radioButtons = [];
41-
foreach ($radioLabels as $label => $value) {
41+
foreach ($keyValues as $label => $value) {
4242
$radioButtons[] = view($templateType.'.fields.radio', [
4343
'label' => $label,
4444
'value' => $value,

0 commit comments

Comments
 (0)