Passed
Push — main ( 9b059d...25ff95 )
by Garbuz
03:13
created

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