1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GarbuzIvan\LaravelGeneratorPackage\Form\Fields; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Exception; |
9
|
|
|
use GarbuzIvan\LaravelGeneratorPackage\Contracts\FieldInterface; |
10
|
|
|
use GarbuzIvan\LaravelGeneratorPackage\Form\Filter; |
11
|
|
|
use GarbuzIvan\LaravelGeneratorPackage\Form\Form; |
12
|
|
|
|
13
|
|
|
abstract class FieldAbstract implements FieldInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected string $column; |
19
|
|
|
protected string $label = ''; |
20
|
|
|
protected ?string $placeholder = null; |
21
|
|
|
protected $default = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Filter |
25
|
|
|
*/ |
26
|
|
|
protected Filter $filter; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
protected bool $index = false; |
32
|
|
|
protected bool $fillable = true; |
33
|
|
|
protected bool $hidden = false; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array|null |
37
|
|
|
*/ |
38
|
|
|
protected ?array $references = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Closure |
42
|
|
|
*/ |
43
|
|
|
protected Closure $saving; |
44
|
|
|
protected Closure $saved; |
45
|
|
|
protected Closure $view; |
46
|
|
|
protected Closure $viewGrid; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* FieldAbstract init. |
50
|
|
|
* @param array $arguments |
51
|
|
|
* @throws Exception |
52
|
|
|
*/ |
53
|
38 |
|
public function init(array $arguments): FieldInterface |
54
|
|
|
{ |
55
|
38 |
|
if (!isset($arguments[0]) || !is_string($arguments[0])) { |
56
|
1 |
|
throw new Exception('The first argument of the field initialization must be a string'); |
57
|
|
|
} |
58
|
37 |
|
if (!preg_match('~^([a-z0-9_]+)$~isuU', $arguments[0])) { |
59
|
1 |
|
throw new Exception('The field mask column must be ([a-z0-9 _]+)'); |
60
|
|
|
} |
61
|
36 |
|
$this->column = $arguments[0]; |
62
|
36 |
|
if (isset($arguments[1]) && is_string($arguments[1])) { |
63
|
36 |
|
$this->setLabel($arguments[1]); |
64
|
|
|
} |
65
|
36 |
|
$this->filter = app(Filter::class); |
66
|
|
|
// default save Closure |
67
|
|
|
$this->saved = $this->saving = function(Form $data) { |
68
|
|
|
return $data; |
69
|
|
|
}; |
70
|
|
|
// default view Closure |
71
|
|
|
$this->view = $this->viewGrid = function(string $column, Form $data) { |
72
|
|
|
return $data->$column; |
73
|
|
|
}; |
74
|
36 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $label |
79
|
|
|
* @return FieldInterface |
80
|
|
|
*/ |
81
|
36 |
|
public function setLabel(string $label = ''): FieldInterface |
82
|
|
|
{ |
83
|
36 |
|
$this->label = $label; |
84
|
36 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $type |
89
|
|
|
* @return FieldInterface |
90
|
|
|
*/ |
91
|
36 |
|
public function setType(string $type): FieldInterface |
92
|
|
|
{ |
93
|
36 |
|
$this->filter->setType($type); |
94
|
36 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param int $light |
99
|
|
|
* @return FieldInterface |
100
|
|
|
*/ |
101
|
36 |
|
public function setLight(int $light): FieldInterface |
102
|
|
|
{ |
103
|
36 |
|
$this->filter->setLight($light); |
104
|
36 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string|null $mask |
109
|
|
|
* @return FieldInterface |
110
|
|
|
*/ |
111
|
1 |
|
public function setMask(?string $mask = null): FieldInterface |
112
|
|
|
{ |
113
|
1 |
|
$this->filter->setMask($mask); |
114
|
1 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string|null $placeholder |
119
|
|
|
* @return FieldInterface |
120
|
|
|
*/ |
121
|
16 |
|
public function setPlaceholder(?string $placeholder = null): FieldInterface |
122
|
|
|
{ |
123
|
16 |
|
$this->placeholder = $placeholder; |
124
|
16 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Method called before saving data |
129
|
|
|
* @param Closure $closure |
130
|
|
|
* @return FieldInterface |
131
|
|
|
*/ |
132
|
1 |
|
public function setSaving(Closure $closure): FieldInterface |
133
|
|
|
{ |
134
|
1 |
|
$this->saving = $closure; |
135
|
1 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Method called after saving data |
140
|
|
|
* @param Closure $closure |
141
|
|
|
* @return FieldInterface |
142
|
|
|
*/ |
143
|
1 |
|
public function setSaved(Closure $closure): FieldInterface |
144
|
|
|
{ |
145
|
1 |
|
$this->saved = $closure; |
146
|
1 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* The method called to convert data from the database for display |
151
|
|
|
* @param Closure $closure |
152
|
|
|
* @return FieldInterface |
153
|
|
|
*/ |
154
|
1 |
|
public function setView(Closure $closure): FieldInterface |
155
|
|
|
{ |
156
|
1 |
|
$this->view = $closure; |
157
|
1 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* The method called to convert data from the database for display in grid |
162
|
|
|
* @param Closure $closure |
163
|
|
|
* @return FieldInterface |
164
|
|
|
*/ |
165
|
1 |
|
public function setViewGrid(Closure $closure): FieldInterface |
166
|
|
|
{ |
167
|
1 |
|
$this->viewGrid = $closure; |
168
|
1 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param bool $required |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
16 |
|
public function required(bool $required = true): FieldInterface |
176
|
|
|
{ |
177
|
16 |
|
$this->filter->required($required); |
178
|
16 |
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param int|null $light |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
1 |
|
public function max(?int $light = null): FieldInterface |
186
|
|
|
{ |
187
|
1 |
|
$this->filter->max($light); |
188
|
1 |
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param int|null $light |
193
|
|
|
* @return $this |
194
|
|
|
*/ |
195
|
1 |
|
public function min(?int $light = null): FieldInterface |
196
|
|
|
{ |
197
|
1 |
|
$this->filter->min($light); |
198
|
1 |
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
1 |
|
public function default($value): FieldInterface |
202
|
|
|
{ |
203
|
1 |
|
$this->default = $value; |
204
|
1 |
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @param string $table |
209
|
|
|
* @param string $field |
210
|
|
|
* @param bool $hasMany |
211
|
|
|
* @return FieldInterface |
212
|
|
|
* @throws Exception |
213
|
|
|
*/ |
214
|
2 |
|
public function references(string $table, string $field, bool $hasMany = true): FieldInterface |
215
|
|
|
{ |
216
|
2 |
|
if (mb_strlen(trim($table)) == 0 || mb_strlen(trim($field)) == 0) { |
217
|
1 |
|
throw new Exception('References should be a string and not null'); |
218
|
|
|
} |
219
|
1 |
|
$this->references = ['table' => $table, 'field' => $field, 'hasmany' => $hasMany]; |
220
|
1 |
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return FieldInterface |
225
|
|
|
*/ |
226
|
1 |
|
public function referencesDisabled(): FieldInterface |
227
|
|
|
{ |
228
|
1 |
|
$this->references = null; |
229
|
1 |
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param bool $index |
234
|
|
|
* @return FieldInterface |
235
|
|
|
*/ |
236
|
16 |
|
public function index(bool $index = true): FieldInterface |
237
|
|
|
{ |
238
|
16 |
|
$this->index = $index; |
239
|
16 |
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param bool $nullable |
244
|
|
|
* @return FieldInterface |
245
|
|
|
*/ |
246
|
16 |
|
public function nullable(bool $nullable = true): FieldInterface |
247
|
|
|
{ |
248
|
16 |
|
$this->filter->nullable($nullable); |
249
|
16 |
|
return $this; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param bool $fillable |
254
|
|
|
* @return FieldInterface |
255
|
|
|
*/ |
256
|
16 |
|
public function fillable(bool $fillable = true): FieldInterface |
257
|
|
|
{ |
258
|
16 |
|
$this->fillable = $fillable; |
259
|
16 |
|
return $this; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* @param bool $unique |
264
|
|
|
* @return FieldInterface |
265
|
|
|
*/ |
266
|
1 |
|
public function unique(bool $unique = true): FieldInterface |
267
|
|
|
{ |
268
|
1 |
|
$this->filter->unique($unique); |
269
|
1 |
|
return $this; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param bool $hidden |
274
|
|
|
* @return FieldInterface |
275
|
|
|
*/ |
276
|
16 |
|
public function hidden(bool $hidden = true): FieldInterface |
277
|
|
|
{ |
278
|
16 |
|
$this->hidden = $hidden; |
279
|
16 |
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return string |
284
|
|
|
*/ |
285
|
2 |
|
public function getLabel(): string |
286
|
|
|
{ |
287
|
2 |
|
return $this->label; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @return string |
292
|
|
|
*/ |
293
|
2 |
|
public function getColumn(): string |
294
|
|
|
{ |
295
|
2 |
|
return $this->column; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
1 |
|
public function getType(): string |
302
|
|
|
{ |
303
|
1 |
|
return $this->filter->getType(); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @return int |
308
|
|
|
*/ |
309
|
1 |
|
public function getLight(): int |
310
|
|
|
{ |
311
|
1 |
|
return $this->filter->getLight(); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @return string|null |
316
|
|
|
*/ |
317
|
1 |
|
public function getMask(): ?string |
318
|
|
|
{ |
319
|
1 |
|
return $this->filter->getMask(); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @return string|null |
324
|
|
|
*/ |
325
|
1 |
|
public function getReferencesTable(): ?string |
326
|
|
|
{ |
327
|
1 |
|
return $this->references['table'] ?? null; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @return string|null |
332
|
|
|
*/ |
333
|
1 |
|
public function getReferencesField(): ?string |
334
|
|
|
{ |
335
|
1 |
|
return $this->references['field'] ?? null; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @return bool |
340
|
|
|
*/ |
341
|
1 |
|
public function getReferencesMany(): bool |
342
|
|
|
{ |
343
|
1 |
|
return $this->references['hasmany'] ?? true; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* @return string|null |
348
|
|
|
*/ |
349
|
1 |
|
public function getPlaceholder(): ?string |
350
|
|
|
{ |
351
|
1 |
|
return $this->placeholder; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @return mixed |
356
|
|
|
*/ |
357
|
1 |
|
public function getDefault() |
358
|
|
|
{ |
359
|
1 |
|
return $this->default; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @return bool |
364
|
|
|
*/ |
365
|
1 |
|
public function getFillable(): bool |
366
|
|
|
{ |
367
|
1 |
|
return $this->fillable; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* @return bool |
372
|
|
|
*/ |
373
|
1 |
|
public function getHidden(): bool |
374
|
|
|
{ |
375
|
1 |
|
return $this->fillable; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* @return bool |
380
|
|
|
*/ |
381
|
1 |
|
public function getRequired(): bool |
382
|
|
|
{ |
383
|
1 |
|
return $this->filter->getRequired(); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* @return int|null |
388
|
|
|
*/ |
389
|
1 |
|
public function getMax(): ?int |
390
|
|
|
{ |
391
|
1 |
|
return $this->filter->getMax(); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @return int|null |
396
|
|
|
*/ |
397
|
1 |
|
public function getMin(): ?int |
398
|
|
|
{ |
399
|
1 |
|
return $this->filter->getMin(); |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* @return bool |
404
|
|
|
*/ |
405
|
1 |
|
public function isIndex(): bool |
406
|
|
|
{ |
407
|
1 |
|
return $this->index; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Method called before saving data |
412
|
|
|
* @return Closure |
413
|
|
|
*/ |
414
|
1 |
|
public function saving(): Closure |
415
|
|
|
{ |
416
|
1 |
|
return $this->saving; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Method called after saving data |
421
|
|
|
* @return Closure |
422
|
|
|
*/ |
423
|
1 |
|
public function saved(): Closure |
424
|
|
|
{ |
425
|
1 |
|
return $this->saved; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* The method called to convert data from the database for display |
430
|
|
|
* @return Closure |
431
|
|
|
*/ |
432
|
1 |
|
public function view(): Closure |
433
|
|
|
{ |
434
|
1 |
|
return $this->view; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* The method called to convert data from the database for display in grid |
439
|
|
|
* @return Closure |
440
|
|
|
*/ |
441
|
1 |
|
public function viewGrid(): Closure |
442
|
|
|
{ |
443
|
1 |
|
return $this->viewGrid; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
/** |
447
|
|
|
* @return bool |
448
|
|
|
*/ |
449
|
1 |
|
public function getNullable(): bool |
450
|
|
|
{ |
451
|
1 |
|
return $this->filter->getNullable(); |
452
|
|
|
} |
453
|
|
|
|
454
|
|
|
/** |
455
|
|
|
* @return bool |
456
|
|
|
*/ |
457
|
1 |
|
public function getUnique(): bool |
458
|
|
|
{ |
459
|
1 |
|
return $this->filter->getUnique(); |
460
|
|
|
} |
461
|
|
|
} |
462
|
|
|
|