1
|
|
|
<?php
|
2
|
|
|
namespace Prateekkarki\Laragen\Models\Types;
|
3
|
|
|
|
4
|
|
|
use Illuminate\Support\Str;
|
5
|
|
|
use Prateekkarki\Laragen\Models\TypeResolver;
|
6
|
|
|
|
7
|
|
|
abstract class LaragenType
|
8
|
|
|
{
|
9
|
|
|
protected $uniqueFlag;
|
10
|
|
|
protected $requiredFlag;
|
11
|
|
|
protected $isDisplay;
|
12
|
|
|
protected $dataType;
|
13
|
|
|
protected $formType;
|
14
|
|
|
protected $stubs = [];
|
15
|
|
|
protected $size = false;
|
16
|
|
|
protected $validationRule = null;
|
17
|
|
|
protected $moduleName;
|
18
|
|
|
protected $columnName;
|
19
|
|
|
protected $optionString;
|
20
|
|
|
|
21
|
|
|
public function __construct($moduleName, $columnName, $optionString)
|
22
|
|
|
{
|
23
|
|
|
$this->moduleName = $moduleName;
|
24
|
|
|
$this->columnName = $columnName;
|
25
|
|
|
$this->optionString = $optionString;
|
26
|
|
|
|
27
|
|
|
$this->optionArray = is_string($optionString) ? explode('|', $optionString) : [];
|
|
|
|
|
28
|
|
|
$typePieces = array_shift($this->optionArray);
|
29
|
|
|
$type = explode(':', $typePieces);
|
30
|
|
|
$this->typeOption = is_array($type) && count($type) >= 2 ? $type[1] : false;
|
|
|
|
|
31
|
|
|
|
32
|
|
|
if(in_array(TypeResolver::COLUMN_UNIQUE, $this->optionArray)){
|
33
|
|
|
$this->setUnique();
|
34
|
|
|
}
|
35
|
|
|
if(in_array(TypeResolver::COLUMN_REQUIRED, $this->optionArray)){
|
36
|
|
|
$this->setRequired();
|
37
|
|
|
}
|
38
|
|
|
if(in_array("*", $this->optionArray)){
|
39
|
|
|
$this->setIsDisplay();
|
40
|
|
|
}
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
|
function __call($method, $params) {
|
|
|
|
|
44
|
|
|
$var = lcfirst(substr($method, 3));
|
45
|
|
|
|
46
|
|
|
if (strncasecmp($method, "get", 3) === 0) {
|
47
|
|
|
return property_exists($this, $var) ? $this->$var : "";
|
48
|
|
|
}
|
49
|
|
|
|
50
|
|
|
if (strncasecmp($method, "set", 3) === 0 && isset($params[0])) {
|
51
|
|
|
$this->$var = $params[0];
|
52
|
|
|
}
|
53
|
|
|
|
54
|
|
|
return property_exists($this, $method) ? $this->$method : "";
|
55
|
|
|
}
|
56
|
|
|
|
57
|
|
|
public function isRelational()
|
58
|
|
|
{
|
59
|
|
|
return $this->relationalType;
|
60
|
|
|
}
|
61
|
|
|
|
62
|
|
|
public function getSchema()
|
63
|
|
|
{
|
64
|
|
|
$schema = '$table->'.$this->getDataType()."('{$this->getColumn()}'";
|
65
|
|
|
$schema .= $this->getSize() ? ", {$this->getSize()})" : ")";
|
|
|
|
|
66
|
|
|
$schema .= $this->isUnique() ? "->unique()" : "";
|
67
|
|
|
$schema .= $this->isRequired() ? "" : "->nullable()";
|
68
|
|
|
$schema .= ";";
|
69
|
|
|
|
70
|
|
|
return $schema;
|
71
|
|
|
}
|
72
|
|
|
|
73
|
|
|
public function getValidationLine()
|
74
|
|
|
{
|
75
|
|
|
$validationSegments = [];
|
76
|
|
|
$modelname = strtolower(Str::camel(str_singular($this->moduleName)));
|
|
|
|
|
77
|
|
|
|
78
|
|
|
$validationSegments[] = $this->isRequired() ? 'required' : 'nullable';
|
79
|
|
|
$validationSegments[] = $this->getValidationRule() ?? $this->getDataType();
|
80
|
|
|
$rules = implode('|', $validationSegments);
|
81
|
|
|
|
82
|
|
|
if ($this->isUnique()) {
|
83
|
|
|
$validationLine = '($this->'.$modelname.') ? \'';
|
84
|
|
|
$validationLine .= $rules . '|unique:'.$this->moduleName.','.$this->getColumn().','.'\''.'.$this->'.$modelname.'->id : \'';
|
85
|
|
|
$validationLine .= $rules . '|unique:'.$this->moduleName.'\'';
|
86
|
|
|
} else{
|
87
|
|
|
$validationLine = "'{$rules}'";
|
88
|
|
|
}
|
89
|
|
|
return $validationLine;
|
90
|
|
|
}
|
91
|
|
|
|
92
|
|
|
public function getFormOptions() {
|
93
|
|
|
$options = "";
|
94
|
|
|
$options .= $this->isRequired() ? 'required="required" ' : '';
|
95
|
|
|
return $options;
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
|
99
|
|
|
public function getForeignKey()
|
100
|
|
|
{
|
101
|
|
|
return $this->columnName . "_id";
|
102
|
|
|
}
|
103
|
|
|
|
104
|
|
|
public function getFilteredColumns($options = [], $columnsOnly = false)
|
105
|
|
|
{
|
106
|
|
|
$filteredTypes = [];
|
107
|
|
|
$options = is_array($options) ? $options : [$options];
|
108
|
|
|
foreach($this->getPivotColumns() as $type){
|
|
|
|
|
109
|
|
|
foreach ($options as $option) {
|
110
|
|
|
if($type->$option()){
|
111
|
|
|
$filteredTypes[] = $columnsOnly ? $type->getColumn() : $type;
|
112
|
|
|
break;
|
113
|
|
|
}
|
114
|
|
|
}
|
115
|
|
|
}
|
116
|
|
|
return $filteredTypes;
|
117
|
|
|
}
|
118
|
|
|
|
119
|
|
|
public function getRelatedModel()
|
120
|
|
|
{
|
121
|
|
|
return $this->getChildModel();
|
122
|
|
|
}
|
123
|
|
|
|
124
|
|
|
public function getRelatedModule()
|
125
|
|
|
{
|
126
|
|
|
return Str::snake(Str::plural($this->getRelatedModel()));
|
127
|
|
|
}
|
128
|
|
|
|
129
|
|
|
public function getRelatedModelLowercase()
|
130
|
|
|
{
|
131
|
|
|
return strtolower($this->getRelatedModel());
|
132
|
|
|
}
|
133
|
|
|
|
134
|
|
|
public function getChildModel()
|
135
|
|
|
{
|
136
|
|
|
return ucfirst(Str::camel(Str::singular($this->typeOption ?: $this->columnName )));
|
|
|
|
|
137
|
|
|
}
|
138
|
|
|
|
139
|
|
|
public function getParentModel()
|
140
|
|
|
{
|
141
|
|
|
return ucfirst(Str::camel(Str::singular($this->moduleName)));
|
142
|
|
|
}
|
143
|
|
|
|
144
|
|
|
public function getParentModule()
|
145
|
|
|
{
|
146
|
|
|
return $this->moduleName;
|
147
|
|
|
}
|
148
|
|
|
|
149
|
|
|
public function getParentModelLowercase()
|
150
|
|
|
{
|
151
|
|
|
return Str::singular($this->moduleName);
|
152
|
|
|
}
|
153
|
|
|
|
154
|
|
|
public function getStub($type)
|
155
|
|
|
{
|
156
|
|
|
return isset($this->stubs[$type]) ? $this->stubs[$type] : false;
|
157
|
|
|
}
|
158
|
|
|
|
159
|
|
|
|
160
|
|
|
public function getTextRows() {
|
161
|
|
|
if (!$this->size)
|
162
|
|
|
return 4;
|
163
|
|
|
|
164
|
|
|
return floor($this->getsize() / 120);
|
|
|
|
|
165
|
|
|
}
|
166
|
|
|
public function isUnique() {
|
167
|
|
|
return $this->uniqueFlag;
|
168
|
|
|
}
|
169
|
|
|
|
170
|
|
|
public function isRequired() {
|
171
|
|
|
return $this->requiredFlag;
|
172
|
|
|
}
|
173
|
|
|
|
174
|
|
|
public function optionArray() {
|
175
|
|
|
return $this->optionArray;
|
176
|
|
|
}
|
177
|
|
|
|
178
|
|
|
public function getDisplay()
|
179
|
|
|
{
|
180
|
|
|
return Str::title(str_replace("_", " ", $this->columnName));
|
181
|
|
|
}
|
182
|
|
|
|
183
|
|
|
public function getColumn()
|
184
|
|
|
{
|
185
|
|
|
return $this->columnName;
|
186
|
|
|
}
|
187
|
|
|
|
188
|
|
|
public function getColumnKey()
|
189
|
|
|
{
|
190
|
|
|
return $this->columnName;
|
191
|
|
|
}
|
192
|
|
|
|
193
|
|
|
public function getDataType() {
|
194
|
|
|
return $this->dataType;
|
195
|
|
|
}
|
196
|
|
|
|
197
|
|
|
public function getValidationRule() {
|
198
|
|
|
return $this->validationRule;
|
199
|
|
|
}
|
200
|
|
|
|
201
|
|
|
protected function setUnique($set = true) {
|
202
|
|
|
$this->uniqueFlag = ($set === true) ? true : false;
|
203
|
|
|
}
|
204
|
|
|
|
205
|
|
|
protected function setRequired($set = true) {
|
206
|
|
|
$this->requiredFlag = ($set === true) ? true : false;
|
207
|
|
|
}
|
208
|
|
|
|
209
|
|
|
protected function setIsDisplay($set = true) {
|
210
|
|
|
$this->isDisplay = ($set === true) ? true : false;
|
211
|
|
|
}
|
212
|
|
|
|
213
|
|
|
protected function setSize($size = null) {
|
214
|
|
|
$this->size = $size;
|
215
|
|
|
}
|
216
|
|
|
|
217
|
|
|
protected function setOptions($optionType, $optionParam) {
|
218
|
|
|
switch ($optionType) {
|
219
|
|
|
case 'max':
|
220
|
|
|
$this->setSize($optionParam);
|
221
|
|
|
break;
|
222
|
|
|
|
223
|
|
|
default:
|
224
|
|
|
$this->$optionType = $optionParam;
|
225
|
|
|
break;
|
226
|
|
|
}
|
227
|
|
|
}
|
228
|
|
|
|
229
|
|
|
public function getTabs($number)
|
230
|
|
|
{
|
231
|
|
|
$schema = "";
|
232
|
|
|
for ($i = 0; $i < $number; $i++) {
|
233
|
|
|
$schema .= " ";
|
234
|
|
|
}
|
235
|
|
|
return $schema;
|
236
|
|
|
}
|
237
|
|
|
}
|
238
|
|
|
|