Test Failed
Push — main ( e653bf...1d729b )
by Garbuz
03:20
created

FieldAbstract::init()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6.1666

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 22
ccs 10
cts 12
cp 0.8333
rs 9.2222
cc 6
nc 4
nop 1
crap 6.1666
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 39
    public function init(array $arguments): FieldInterface
55
    {
56 39
        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 38
        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 37
        $this->column = $arguments[0];
63 37
        if (isset($arguments[1]) && is_string($arguments[1])) {
64 37
            $this->setLabel($arguments[1]);
65
        }
66 37
        $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 37
        return $this;
76
    }
77
78
    /**
79
     * @param string $label
80
     * @return FieldInterface
81
     */
82 37
    public function setLabel(string $label = ''): FieldInterface
83
    {
84 37
        $this->label = $label;
85 37
        return $this;
86
    }
87
88
    /**
89
     * @param string $type
90
     * @return FieldInterface
91
     */
92 37
    public function setType(string $type): FieldInterface
93
    {
94 37
        $this->filter->setType($type);
95 37
        return $this;
96
    }
97
98
    /**
99
     * @param int $light
100
     * @return FieldInterface
101
     */
102 37
    public function setLight(int $light): FieldInterface
103
    {
104 37
        $this->filter->setLight($light);
105 37
        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 17
    public function default($value = null): FieldInterface
203
    {
204 17
        $this->default = $value;
205 17
        return $this;
206
    }
207
208
    /**
209
     * @param string $table
210
     * @param string $field
211
     * @param bool $hasMany
212
     * @return FieldInterface
213
     * @throws Exception
214
     */
215 2
    public function references(string $table, string $field, bool $hasMany = true): FieldInterface
216
    {
217 2
        if (mb_strlen(trim($table)) == 0 || mb_strlen(trim($field)) == 0) {
218 1
            throw new Exception('References should be a string and not null');
219
        }
220 1
        $this->references = ['table' => $table, 'field' => $field, 'hasmany' => $hasMany];
221 1
        return $this;
222
    }
223
224
    /**
225
     * @return FieldInterface
226
     */
227 1
    public function referencesDisabled(): FieldInterface
228
    {
229 1
        $this->references = null;
230 1
        return $this;
231
    }
232
233
    /**
234
     * @param bool $index
235
     * @return FieldInterface
236
     */
237 17
    public function index(bool $index = true): FieldInterface
238
    {
239 17
        $this->index = $index;
240 17
        return $this;
241
    }
242
243
    /**
244
     * @param bool $nullable
245
     * @return FieldInterface
246
     */
247 17
    public function nullable(bool $nullable = true): FieldInterface
248
    {
249 17
        $this->filter->nullable($nullable);
250 17
        return $this;
251
    }
252
253
    /**
254
     * @param bool $fillable
255
     * @return FieldInterface
256
     */
257 17
    public function fillable(bool $fillable = true): FieldInterface
258
    {
259 17
        $this->fillable = $fillable;
260 17
        return $this;
261
    }
262
263
    /**
264
     * @param bool $unique
265
     * @return FieldInterface
266
     */
267 17
    public function unique(bool $unique = true): FieldInterface
268
    {
269 17
        $this->filter->unique($unique);
270 17
        return $this;
271
    }
272
273
    /**
274
     * @param bool $hidden
275
     * @return FieldInterface
276
     */
277 17
    public function hidden(bool $hidden = true): FieldInterface
278
    {
279 17
        $this->hidden = $hidden;
280 17
        return $this;
281
    }
282
283
    /**
284
     * @return string
285
     */
286 2
    public function getLabel(): string
287
    {
288 2
        return $this->label;
289
    }
290
291
    /**
292
     * @return string
293
     */
294 2
    public function getColumn(): string
295
    {
296 2
        return $this->column;
297
    }
298
299
    /**
300
     * @return string
301
     */
302 1
    public function getType(): string
303
    {
304 1
        return $this->filter->getType();
305
    }
306
307
    /**
308
     * @return int
309
     */
310 1
    public function getLight(): int
311
    {
312 1
        return $this->filter->getLight();
313
    }
314
315
    /**
316
     * @return string|null
317
     */
318 1
    public function getMask(): ?string
319
    {
320 1
        return $this->filter->getMask();
321
    }
322
323
    /**
324
     * @return string|null
325
     */
326 1
    public function getReferencesTable(): ?string
327
    {
328 1
        return $this->references['table'] ?? null;
329
    }
330
331
    /**
332
     * @return string|null
333
     */
334 1
    public function getReferencesField(): ?string
335
    {
336 1
        return $this->references['field'] ?? null;
337
    }
338
339
    /**
340
     * @return bool
341
     */
342 1
    public function getReferencesMany(): bool
343
    {
344 1
        return $this->references['hasmany'] ?? true;
345
    }
346
347
    /**
348
     * @return string|null
349
     */
350 1
    public function getPlaceholder(): ?string
351
    {
352 1
        return $this->placeholder;
353
    }
354
355
    /**
356
     * @return mixed
357
     */
358 1
    public function getDefault()
359
    {
360 1
        return $this->default;
361
    }
362
363
    /**
364
     * @return bool
365
     */
366 1
    public function getFillable(): bool
367
    {
368 1
        return $this->fillable;
369
    }
370
371
    /**
372
     * @return bool
373
     */
374 1
    public function getHidden(): bool
375
    {
376 1
        return $this->fillable;
377
    }
378
379
    /**
380
     * @return bool
381
     */
382 1
    public function getRequired(): bool
383
    {
384 1
        return $this->filter->getRequired();
385
    }
386
387
    /**
388
     * @return int|null
389
     */
390 1
    public function getMax(): ?int
391
    {
392 1
        return $this->filter->getMax();
393
    }
394
395
    /**
396
     * @return int|null
397
     */
398 1
    public function getMin(): ?int
399
    {
400 1
        return $this->filter->getMin();
401
    }
402
403
    /**
404
     * @return bool
405
     */
406 1
    public function isIndex(): bool
407
    {
408 1
        return $this->index;
409
    }
410
411
    /**
412
     * Method called before saving data
413
     * @return Closure
414
     */
415 1
    public function saving(): Closure
416
    {
417 1
        return $this->saving;
418
    }
419
420
    /**
421
     * Method called after saving data
422
     * @return Closure
423
     */
424 1
    public function saved(): Closure
425
    {
426 1
        return $this->saved;
427
    }
428
429
    /**
430
     * The method called to convert data from the database for display
431
     * @return Closure
432
     */
433 1
    public function view(): Closure
434
    {
435 1
        return $this->view;
436
    }
437
438
    /**
439
     * The method called to convert data from the database for display in grid
440
     * @return Closure
441
     */
442 1
    public function viewGrid(): Closure
443
    {
444 1
        return $this->viewGrid;
445
    }
446
447
    /**
448
     * @return bool
449
     */
450 1
    public function getNullable(): bool
451
    {
452 1
        return $this->filter->getNullable();
453
    }
454
455
    /**
456
     * @return bool
457
     */
458 1
    public function getUnique(): bool
459
    {
460 1
        return $this->filter->getUnique();
461
    }
462
}
463