1
|
|
|
<?php |
2
|
|
|
namespace Prateekkarki\Laragen\Models\Types; |
3
|
|
|
|
4
|
|
|
use Illuminate\Support\Str; |
5
|
|
|
use Prateekkarki\Laragen\Models\TypeResolver; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The LaragenType abstract class. This class cannot be instantiated. It's implementations are used to create new types. |
9
|
|
|
* Column instances are created from types that are implementations of this class. |
10
|
|
|
* |
11
|
|
|
* @method integer getSize() |
12
|
|
|
* @method integer getDataType() |
13
|
|
|
* @method array getPivotColumns() |
14
|
|
|
* @method void setSize() |
15
|
|
|
* @method void setIsRequired() |
16
|
|
|
* @method void setIsUnique() |
17
|
|
|
* @method void setIsDisplay() |
18
|
|
|
*/ |
19
|
|
|
abstract class LaragenType |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Denotes if the column is unique. |
23
|
|
|
* |
24
|
|
|
* @var bool |
25
|
|
|
*/ |
26
|
|
|
protected $isUnique; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Denotes if the column is required. |
30
|
|
|
* |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
protected $isRequired; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Denotes if the column is used as title. Can be in case of select or checkboxes. |
37
|
|
|
* |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
protected $isDisplay; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The data type of column used in the database. |
44
|
|
|
* Stores name of a column creation method of \Illuminate\Database\Schema\Blueprint |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $dataType; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The stub to be used in form generation of column. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $formType; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Defines additional stubs to be used for |
59
|
|
|
* Defines stubs for relational types to be used in models generation |
60
|
|
|
* |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
protected $stubs = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The size of column in the database. |
67
|
|
|
* |
68
|
|
|
* @var int |
69
|
|
|
*/ |
70
|
|
|
protected $size = 0; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Validation rule for the column to be used in Request file. |
74
|
|
|
* |
75
|
|
|
* @var string|null |
76
|
|
|
*/ |
77
|
|
|
protected $validationRule = null; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Name of the module the column belongs to. |
81
|
|
|
* |
82
|
|
|
* @var string |
83
|
|
|
*/ |
84
|
|
|
protected $moduleName; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Name of the column. |
88
|
|
|
* e.g for 'short_description' => 'string|max:512', 'short_description' is the columnName. |
89
|
|
|
* |
90
|
|
|
* @var string |
91
|
|
|
*/ |
92
|
|
|
protected $columnName; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Special type assigned to the column e.g parent, related |
96
|
|
|
* |
97
|
|
|
* @var string|null |
98
|
|
|
*/ |
99
|
|
|
protected $typeOption; |
100
|
|
|
|
101
|
|
|
public function __construct($moduleName, $columnName, $optionString) |
102
|
|
|
{ |
103
|
|
|
$this->moduleName = $moduleName; |
104
|
|
|
$this->columnName = $columnName; |
105
|
|
|
|
106
|
|
|
$optionArray = is_string($optionString) ? explode('|', $optionString) : []; |
107
|
|
|
$typePieces = array_shift($optionArray); |
108
|
|
|
$type = explode(':', $typePieces); |
109
|
|
|
$this->typeOption = is_array($type) && count($type) >= 2 ? $type[1] : null; |
110
|
|
|
|
111
|
|
|
if (in_array(TypeResolver::COLUMN_UNIQUE, $optionArray)) { |
112
|
|
|
$this->setsIsUnique(true); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
if (in_array(TypeResolver::COLUMN_REQUIRED, $optionArray)) { |
115
|
|
|
$this->setIsRequired(true); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
if (in_array(TypeResolver::COLUMN_DISPLAY, $optionArray)) { |
118
|
|
|
$this->setIsDisplay(true); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function __call($method, $params) { |
123
|
|
|
$var = lcfirst(substr($method, 3)); |
124
|
|
|
|
125
|
|
|
if (strncasecmp($method, "get", 3) === 0) { |
126
|
|
|
return property_exists($this, $var) ? $this->$var : ""; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (strncasecmp($method, "set", 3) === 0 && isset($params[0])) { |
130
|
|
|
$this->$var = $params[0]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return property_exists($this, $method) ? $this->$method : ""; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getSchema() |
137
|
|
|
{ |
138
|
|
|
$schema = '$table->'.$this->getDataType()."('{$this->getColumn()}'"; |
139
|
|
|
$schema .= $this->getSize() ? ", {$this->getSize()})" : ")"; |
140
|
|
|
$schema .= $this->isUnique() ? "->unique()" : ""; |
|
|
|
|
141
|
|
|
$schema .= $this->isRequired() ? "" : "->nullable()"; |
|
|
|
|
142
|
|
|
$schema .= ";"; |
143
|
|
|
|
144
|
|
|
return $schema; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function getFilteredColumns($options = [], $columnsOnly = false) |
148
|
|
|
{ |
149
|
|
|
$filteredTypes = []; |
150
|
|
|
$options = is_array($options) ? $options : [$options]; |
151
|
|
|
foreach($this->getPivotColumns() as $type){ |
152
|
|
|
foreach ($options as $option) { |
153
|
|
|
if($type->$option()){ |
154
|
|
|
$filteredTypes[] = $columnsOnly ? $type->getColumn() : $type; |
155
|
|
|
break; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
return $filteredTypes; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function getFormOptions() { |
163
|
|
|
$options = ""; |
164
|
|
|
$options .= $this->isRequired() ? 'required="required" ' : ''; |
165
|
|
|
return $options; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function getForeignKey() |
169
|
|
|
{ |
170
|
|
|
return $this->columnName . "_id"; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function getRelatedModel() |
174
|
|
|
{ |
175
|
|
|
return $this->getChildModel(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function getRelatedModule() |
179
|
|
|
{ |
180
|
|
|
return Str::plural(strtolower(Str::snake($this->getRelatedModel()))); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function getRelatedModelLowercase() |
184
|
|
|
{ |
185
|
|
|
return strtolower($this->getRelatedModel()); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function getChildModel() |
189
|
|
|
{ |
190
|
|
|
return ucfirst(Str::camel(Str::singular($this->typeOption ?? $this->columnName ))); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function getParentModel() |
194
|
|
|
{ |
195
|
|
|
return ucfirst(Str::camel(Str::singular($this->moduleName))); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function getParentModule() |
199
|
|
|
{ |
200
|
|
|
return $this->moduleName; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function getParentModelLowercase() |
204
|
|
|
{ |
205
|
|
|
return Str::singular($this->moduleName); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function getStub($type) |
209
|
|
|
{ |
210
|
|
|
return isset($this->stubs[$type]) ? $this->stubs[$type] : false; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function getTextRows() { |
214
|
|
|
if (!$this->size) { |
215
|
|
|
return 4; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return floor($this->getsize() / 120); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function getDisplay() |
222
|
|
|
{ |
223
|
|
|
return Str::title(str_replace("_", " ", $this->columnName)); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function getColumn() |
227
|
|
|
{ |
228
|
|
|
return $this->columnName; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function getColumnKey() |
232
|
|
|
{ |
233
|
|
|
return $this->columnName; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
protected function setOptions($optionType, $optionParam) { |
237
|
|
|
switch ($optionType) { |
238
|
|
|
case 'max': |
239
|
|
|
$this->setSize($optionParam); |
|
|
|
|
240
|
|
|
break; |
241
|
|
|
|
242
|
|
|
default: |
243
|
|
|
$this->$optionType = $optionParam; |
244
|
|
|
break; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function getTabs($number) |
249
|
|
|
{ |
250
|
|
|
$schema = ""; |
251
|
|
|
for ($i = 0; $i < $number; $i++) { |
252
|
|
|
$schema .= " "; |
253
|
|
|
} |
254
|
|
|
return $schema; |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|