Passed
Push — main ( 25ff95...174661 )
by Garbuz
13:35
created

FieldAbstract::getCast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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