/
GeneratorField.php
151 lines (127 loc) · 4.59 KB
/
GeneratorField.php
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
<?php
namespace InfyOm\Generator\Common;
use Illuminate\Support\Str;
class GeneratorField
{
/** @var string */
public $name;
public $dbInput;
public $htmlInput;
public $htmlType;
public $fieldType;
public $description;
/** @var array */
public $htmlValues;
/** @var string */
public $migrationText;
public $foreignKeyText;
public $validations;
/** @var bool */
public $isSearchable = true;
public $isFillable = true;
public $isPrimary = false;
public $inForm = true;
public $isNotNull = false;
public $inIndex = true;
public function parseDBType($dbInput)
{
$this->dbInput = $dbInput;
$this->prepareMigrationText();
}
public function parseHtmlInput($htmlInput)
{
$this->htmlInput = $htmlInput;
$this->htmlValues = [];
if (empty($htmlInput)) {
$this->htmlType = 'text';
return;
}
$inputsArr = explode(',', $htmlInput);
$this->htmlType = array_shift($inputsArr);
if (count($inputsArr) > 0) {
$this->htmlValues = $inputsArr;
}
}
public function parseOptions($options)
{
$options = strtolower($options);
$optionsArr = explode(',', $options);
if (in_array('s', $optionsArr)) {
$this->isSearchable = false;
}
if (in_array('p', $optionsArr)) {
// if field is primary key, then its not searchable, fillable, not in index & form
$this->isPrimary = true;
$this->isSearchable = false;
$this->isFillable = false;
$this->inForm = false;
$this->inIndex = false;
}
if (in_array('f', $optionsArr)) {
$this->isFillable = false;
}
if (in_array('if', $optionsArr)) {
$this->inForm = false;
}
if (in_array('ii', $optionsArr)) {
$this->inIndex = false;
}
}
private function prepareMigrationText()
{
$inputsArr = explode(':', $this->dbInput);
$this->migrationText = '$table->';
$fieldTypeParams = explode(',', array_shift($inputsArr));
$this->fieldType = array_shift($fieldTypeParams);
$this->migrationText .= $this->fieldType."('".$this->name."'";
if ($this->fieldType == 'enum') {
$this->migrationText .= ', [';
foreach ($fieldTypeParams as $param) {
$this->migrationText .= "'".$param."',";
}
$this->migrationText = substr($this->migrationText, 0, strlen($this->migrationText) - 1);
$this->migrationText .= ']';
} else {
foreach ($fieldTypeParams as $param) {
$this->migrationText .= ', '.$param;
}
}
$this->migrationText .= ')';
foreach ($inputsArr as $input) {
$inputParams = explode(',', $input);
$functionName = array_shift($inputParams);
if ($functionName == 'foreign') {
$foreignTable = array_shift($inputParams);
$foreignField = array_shift($inputParams);
$this->foreignKeyText .= "\$table->foreign('".$this->name."')->references('".$foreignField."')->on('".$foreignTable."');";
} else {
$this->migrationText .= '->'.$functionName;
$this->migrationText .= '(';
$this->migrationText .= implode(', ', $inputParams);
$this->migrationText .= ')';
}
}
$this->migrationText .= ';';
}
public static function parseFieldFromFile($fieldInput)
{
$field = new self();
$field->name = $fieldInput['name'];
$field->parseDBType($fieldInput['dbType']);
$field->parseHtmlInput(isset($fieldInput['htmlType']) ? $fieldInput['htmlType'] : '');
$field->validations = isset($fieldInput['validations']) ? $fieldInput['validations'] : '';
$field->isSearchable = isset($fieldInput['searchable']) ? $fieldInput['searchable'] : false;
$field->isFillable = isset($fieldInput['fillable']) ? $fieldInput['fillable'] : true;
$field->isPrimary = isset($fieldInput['primary']) ? $fieldInput['primary'] : false;
$field->inForm = isset($fieldInput['inForm']) ? $fieldInput['inForm'] : true;
$field->inIndex = isset($fieldInput['inIndex']) ? $fieldInput['inIndex'] : true;
return $field;
}
public function __get($key)
{
if ($key == 'fieldTitle') {
return Str::title(str_replace('_', ' ', $this->name));
}
return $this->$key;
}
}