|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yeelight\Generators; |
|
4
|
|
|
|
|
5
|
|
|
use Yeelight\Generators\Migrations\SchemaParser; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class RepositoryEloquentGenerator |
|
9
|
|
|
* |
|
10
|
|
|
* @category Yeelight |
|
11
|
|
|
* |
|
12
|
|
|
* @package Yeelight\Generators |
|
13
|
|
|
* |
|
14
|
|
|
* @author Sheldon Lee <[email protected]> |
|
15
|
|
|
* |
|
16
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
|
17
|
|
|
* |
|
18
|
|
|
* @link https://www.yeelight.com |
|
19
|
|
|
*/ |
|
20
|
|
|
class RepositoryEloquentGenerator extends Generator |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Get stub name. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $stub = 'repository/eloquent'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get root namespace. |
|
31
|
|
|
* |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getRootNamespace() |
|
35
|
|
|
{ |
|
36
|
|
|
return parent::getRootNamespace() . |
|
37
|
|
|
parent::getConfigGeneratorClassPath($this->getPathConfigNode()); |
|
|
|
|
|
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get generator path config node. |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getPathConfigNode() |
|
46
|
|
|
{ |
|
47
|
|
|
return 'repositories'; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Get destination path for generated file. |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getPath() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->getBasePath() . |
|
58
|
|
|
'/' . |
|
59
|
|
|
parent::getConfigGeneratorClassPath($this->getPathConfigNode(), true) . |
|
|
|
|
|
|
60
|
|
|
'/' . |
|
61
|
|
|
$this->getName() . |
|
62
|
|
|
'RepositoryEloquent.php'; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get base path of destination file. |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getBasePath() |
|
71
|
|
|
{ |
|
72
|
|
|
return config('repository.generator.basePath', app_path()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get array replacements. |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getReplacements() |
|
81
|
|
|
{ |
|
82
|
|
|
$repository = parent::getRootNamespace().parent::getConfigGeneratorClassPath('interfaces').'\\'.$this->name.'Repository;'; |
|
|
|
|
|
|
83
|
|
|
$repository = str_replace( |
|
84
|
|
|
[ |
|
85
|
|
|
'\\', |
|
86
|
|
|
'/', |
|
87
|
|
|
], |
|
88
|
|
|
'\\', |
|
89
|
|
|
$repository |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
return array_merge( |
|
93
|
|
|
parent::getReplacements(), |
|
94
|
|
|
[ |
|
95
|
|
|
'fillable' => $this->getFillable(), |
|
96
|
|
|
'searchable' => $this->getSearchable(), |
|
97
|
|
|
'use_validator' => $this->getValidatorUse(), |
|
98
|
|
|
'validator' => $this->getValidatorMethod(), |
|
99
|
|
|
'use_presenter' => $this->getPresenterUse(), |
|
100
|
|
|
'root_namespace' => parent::getRootNamespace(), |
|
101
|
|
|
'presenter' => $this->getPresenterMethod(), |
|
102
|
|
|
'repository' => $repository, |
|
103
|
|
|
'model' => isset($this->options['model']) ? $this->options['model'] : '', |
|
104
|
|
|
] |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Get the fillable attributes. |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getFillable() |
|
114
|
|
|
{ |
|
115
|
|
|
if (!$this->fillable) { |
|
|
|
|
|
|
116
|
|
|
return '[]'; |
|
117
|
|
|
} |
|
118
|
|
|
$results = '['.PHP_EOL; |
|
119
|
|
|
|
|
120
|
|
|
foreach ($this->getSchemaParser()->toArray() as $column => $value) { |
|
121
|
|
|
$results .= "\t\t'{$column}',".PHP_EOL; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return $results."\t".']'; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Get the Searchable attributes. |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getSearchable() |
|
133
|
|
|
{ |
|
134
|
|
|
if (!$this->fields) { |
|
|
|
|
|
|
135
|
|
|
return '[]'; |
|
136
|
|
|
} |
|
137
|
|
|
$results = '['.PHP_EOL; |
|
138
|
|
|
|
|
139
|
|
|
foreach ($this->fields as $column => $field) { |
|
140
|
|
|
$results .= $this->getSearchAbleFromField($field); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $results."\t".']'; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
private function getSearchAbleFromField($field) |
|
147
|
|
|
{ |
|
148
|
|
|
switch ($field['type']) { |
|
149
|
|
|
case 'string': |
|
150
|
|
|
case 'text': |
|
151
|
|
|
case 'char': |
|
152
|
|
|
case 'mediumText': |
|
153
|
|
|
case 'longText': |
|
154
|
|
|
case 'enum': |
|
155
|
|
|
case 'json': |
|
156
|
|
|
case 'jsonb': |
|
157
|
|
|
case 'binary': |
|
158
|
|
|
case 'ipAddress': |
|
159
|
|
|
case 'macAddress': |
|
160
|
|
|
return "\t\t'{$field['name']}' => 'like'," . PHP_EOL; |
|
161
|
|
|
case 'integer': |
|
162
|
|
|
case 'tinyInteger': |
|
163
|
|
|
case 'smallInteger': |
|
164
|
|
|
case 'mediumInteger': |
|
165
|
|
|
case 'bigInteger': |
|
166
|
|
|
case 'unsignedTinyInteger': |
|
167
|
|
|
case 'unsignedSmallInteger': |
|
168
|
|
|
case 'unsignedMediumInteger': |
|
169
|
|
|
case 'unsignedInteger': |
|
170
|
|
|
case 'unsignedBigInteger': |
|
171
|
|
|
case 'date': |
|
172
|
|
|
case 'time': |
|
173
|
|
|
case 'dateTime': |
|
174
|
|
|
case 'timestamp': |
|
175
|
|
|
case 'dateTimeTz': |
|
176
|
|
|
case 'timeTz': |
|
177
|
|
|
case 'timestampTz': |
|
178
|
|
|
case 'nullableTimestamps': |
|
179
|
|
|
case 'float': |
|
180
|
|
|
case 'decimal': |
|
181
|
|
|
case 'double': |
|
182
|
|
|
case 'boolean': |
|
183
|
|
|
return "\t\t'{$field['name']}'," . PHP_EOL; |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Get schema parser. |
|
189
|
|
|
* |
|
190
|
|
|
* @return SchemaParser |
|
191
|
|
|
*/ |
|
192
|
|
|
public function getSchemaParser() |
|
193
|
|
|
{ |
|
194
|
|
|
return new SchemaParser($this->fillable); |
|
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* GetValidatorUse |
|
199
|
|
|
* |
|
200
|
|
|
* @return string |
|
201
|
|
|
*/ |
|
202
|
|
|
public function getValidatorUse() |
|
203
|
|
|
{ |
|
204
|
|
|
$validator = $this->getValidator(); |
|
205
|
|
|
|
|
206
|
|
|
return "use {$validator};"; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* GetValidator |
|
211
|
|
|
* |
|
212
|
|
|
* @return string |
|
213
|
|
|
*/ |
|
214
|
|
|
public function getValidator() |
|
215
|
|
|
{ |
|
216
|
|
|
$validatorGenerator = new ValidatorGenerator( |
|
217
|
|
|
[ |
|
218
|
|
|
'name' => $this->name, |
|
|
|
|
|
|
219
|
|
|
'rules' => $this->rules, |
|
|
|
|
|
|
220
|
|
|
'force' => $this->force, |
|
|
|
|
|
|
221
|
|
|
] |
|
222
|
|
|
); |
|
223
|
|
|
|
|
224
|
|
|
$validator = $validatorGenerator->getRootNamespace().'\\'.$validatorGenerator->getName(); |
|
225
|
|
|
|
|
226
|
|
|
return str_replace([ |
|
227
|
|
|
'\\', |
|
228
|
|
|
'/', |
|
229
|
|
|
], '\\', $validator).'Validator'; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* GetValidatorMethod |
|
234
|
|
|
* |
|
235
|
|
|
* @return string |
|
236
|
|
|
*/ |
|
237
|
|
|
public function getValidatorMethod() |
|
238
|
|
|
{ |
|
239
|
|
|
if ($this->validator != 'yes') { |
|
|
|
|
|
|
240
|
|
|
return ''; |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
$class = $this->getClass(); |
|
244
|
|
|
|
|
245
|
|
|
return '/**'.PHP_EOL.' * Specify Validator class name'.PHP_EOL.' *'.PHP_EOL.' * @return mixed'.PHP_EOL.' */'.PHP_EOL.' public function validator()'.PHP_EOL.' {'.PHP_EOL.PHP_EOL.' return '.$class.'Validator::class;'.PHP_EOL.' }'.PHP_EOL; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* GetPresenterUse |
|
250
|
|
|
* |
|
251
|
|
|
* @return string |
|
252
|
|
|
*/ |
|
253
|
|
|
public function getPresenterUse() |
|
254
|
|
|
{ |
|
255
|
|
|
$presenter = $this->getPresenter(); |
|
256
|
|
|
|
|
257
|
|
|
return "use {$presenter};"; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
/** |
|
261
|
|
|
* GetPresenter |
|
262
|
|
|
* |
|
263
|
|
|
* @return string |
|
264
|
|
|
*/ |
|
265
|
|
|
public function getPresenter() |
|
266
|
|
|
{ |
|
267
|
|
|
$presenterGenerator = new PresenterGenerator( |
|
268
|
|
|
[ |
|
269
|
|
|
'name' => $this->name, |
|
|
|
|
|
|
270
|
|
|
'rules' => $this->rules, |
|
|
|
|
|
|
271
|
|
|
'force' => $this->force, |
|
|
|
|
|
|
272
|
|
|
] |
|
273
|
|
|
); |
|
274
|
|
|
|
|
275
|
|
|
$presenter = $presenterGenerator->getRootNamespace().'\\'.$presenterGenerator->getName(); |
|
276
|
|
|
|
|
277
|
|
|
return str_replace([ |
|
278
|
|
|
'\\', |
|
279
|
|
|
'/', |
|
280
|
|
|
], '\\', $presenter).'Presenter'; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* GetPresenterMethod |
|
285
|
|
|
* |
|
286
|
|
|
* @return string |
|
287
|
|
|
*/ |
|
288
|
|
|
public function getPresenterMethod() |
|
289
|
|
|
{ |
|
290
|
|
|
if ($this->presenter != 'yes') { |
|
|
|
|
|
|
291
|
|
|
return ''; |
|
292
|
|
|
} |
|
293
|
|
|
|
|
294
|
|
|
$class = $this->getClass(); |
|
295
|
|
|
|
|
296
|
|
|
return '/**'.PHP_EOL.' * Specify Presenter class name'.PHP_EOL.' *'.PHP_EOL.' * @return mixed'.PHP_EOL.' */'.PHP_EOL.' public function presenter()'.PHP_EOL.' {'.PHP_EOL.PHP_EOL.' return '.$class.'Presenter::class;'.PHP_EOL.' }'.PHP_EOL; |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|