Passed
Push — main ( 5926a8...a239d4 )
by Garbuz
03:10
created

FieldAbstract::getLight()   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
     * @throws Exception
52
     */
53 15
    public function init(array $arguments): FieldInterface
54
    {
55 15
        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 14
        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 13
        $this->column = $arguments[0];
62 13
        if (isset($arguments[1]) && is_string($arguments[1])) {
63 13
            $this->setLabel($arguments[1]);
64
        }
65 13
        $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 13
        return $this;
75
    }
76
77
    /**
78
     * @param string $label
79
     * @return FieldInterface
80
     */
81 13
    public function setLabel(string $label = ''): FieldInterface
82
    {
83 13
        $this->label = $label;
84 13
        return $this;
85
    }
86
87
    /**
88
     * @param string $type
89
     * @return FieldInterface
90
     */
91 13
    public function setType(string $type): FieldInterface
92
    {
93 13
        $this->filter->setType($type);
94 13
        return $this;
95
    }
96
97
    /**
98
     * @param int $light
99
     * @return FieldInterface
100
     */
101 13
    public function setLight(int $light): FieldInterface
102
    {
103 13
        $this->filter->setLight($light);
104 13
        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 1
    public function setPlaceholder(?string $placeholder = null): FieldInterface
122
    {
123 1
        $this->placeholder = $placeholder;
124 1
        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
    public function required(bool $required = true): FieldInterface
176
    {
177
        $this->filter->required($required);
178
        return $this;
179
    }
180
181
    /**
182
     * @param int|null $light
183
     * @return $this
184
     */
185
    public function max(?int $light = null): FieldInterface
186
    {
187
        $this->filter->max($light);
188
        return $this;
189
    }
190
191
    /**
192
     * @param int|null $light
193
     * @return $this
194
     */
195
    public function min(?int $light = null): FieldInterface
196
    {
197
        $this->filter->min($light);
198
        return $this;
199
    }
200
201
    public function default($value): FieldInterface
202
    {
203
        $this->default = $value;
204
        return $this;
205
    }
206
207
    /**
208
     * @param string|null $table
209
     * @param string|null $field
210
     * @param bool $hasMany
211
     * @return FieldInterface
212
     * @throws Exception
213
     */
214 1
    public function references(?string $table = null, ?string $field = null, bool $hasMany = true): FieldInterface
215
    {
216 1
        if ((!is_string($table) && !is_null($table)) || (!is_string($field) && !is_null($field))) {
0 ignored issues
show
introduced by
The condition is_null($table) is always true.
Loading history...
introduced by
The condition is_null($field) is always true.
Loading history...
217
            throw new Exception('References should be a string or null');
218
        }
219
        if (
220 1
            is_null($table)
221 1
            || is_null($field)
222 1
            || mb_strlen($table) == 0
223 1
            || mb_strlen($field) == 0
224
        ) {
225
            $this->references = null;
226
        }
227 1
        $this->references = ['table' => $table, 'field' => $field, 'hasmany' => $hasMany];
228 1
        return $this;
229
    }
230
231
    /**
232
     * @param bool $index
233
     * @return FieldInterface
234
     */
235
    public function index(bool $index = true): FieldInterface
236
    {
237
        $this->index = $index;
238
        return $this;
239
    }
240
241
    /**
242
     * @param bool $fillable
243
     * @return FieldInterface
244
     */
245 1
    public function fillable(bool $fillable = true): FieldInterface
246
    {
247 1
        $this->fillable = $fillable;
248 1
        return $this;
249
    }
250
251
    /**
252
     * @param bool $hidden
253
     * @return FieldInterface
254
     */
255 1
    public function hidden(bool $hidden = true): FieldInterface
256
    {
257 1
        $this->hidden = $hidden;
258 1
        return $this;
259
    }
260
261
    /**
262
     * @return string
263
     */
264 2
    public function getLabel(): string
265
    {
266 2
        return $this->label;
267
    }
268
269
    /**
270
     * @return string
271
     */
272 2
    public function getColumn(): string
273
    {
274 2
        return $this->column;
275
    }
276
277
    /**
278
     * @return string
279
     */
280 1
    public function getType(): string
281
    {
282 1
        return $this->filter->getType();
283
    }
284
285
    /**
286
     * @return int
287
     */
288 1
    public function getLight(): int
289
    {
290 1
        return $this->filter->getLight();
291
    }
292
293
    /**
294
     * @return string|null
295
     */
296 1
    public function getMask(): ?string
297
    {
298 1
        return $this->filter->getMask();
299
    }
300
301
    /**
302
     * @return string|null
303
     */
304 1
    public function getReferencesTable(): ?string
305
    {
306 1
        return $this->references['table'] ?? null;
307
    }
308
309
    /**
310
     * @return string|null
311
     */
312 1
    public function getReferencesField(): ?string
313
    {
314 1
        return $this->references['field'] ?? null;
315
    }
316
317
    /**
318
     * @return bool
319
     */
320 1
    public function getReferencesMany(): bool
321
    {
322 1
        return $this->references['hasmany'] ?? true;
323
    }
324
325
    /**
326
     * @return string|null
327
     */
328 1
    public function getPlaceholder(): ?string
329
    {
330 1
        return $this->placeholder;
331
    }
332
333
    /**
334
     * @return string|null
335
     */
336
    public function getValueDefault(): mixed
337
    {
338
        return $this->default;
339
    }
340
341
    /**
342
     * @return bool
343
     */
344 1
    public function getFillable(): bool
345
    {
346 1
        return $this->fillable;
347
    }
348
349
    /**
350
     * @return bool
351
     */
352 1
    public function getHidden(): bool
353
    {
354 1
        return $this->fillable;
355
    }
356
357
    /**
358
     * @return bool
359
     */
360
    public function getRequired(): bool
361
    {
362
        return $this->filter->getRequired();
363
    }
364
365
    /**
366
     * @return int|null
367
     */
368
    public function getMax(): ?int
369
    {
370
        return $this->filter->getMax();
371
    }
372
373
    /**
374
     * @return int|null
375
     */
376
    public function getMin(): ?int
377
    {
378
        return $this->filter->getMin();
379
    }
380
381
    /**
382
     * @return bool
383
     */
384
    public function isIndex(): bool
385
    {
386
        return $this->index;
387
    }
388
389
    /**
390
     * Method called before saving data
391
     * @return Closure
392
     */
393 1
    public function saving(): Closure
394
    {
395 1
        return $this->saving;
396
    }
397
398
    /**
399
     * Method called after saving data
400
     * @return Closure
401
     */
402 1
    public function saved(): Closure
403
    {
404 1
        return $this->saved;
405
    }
406
407
    /**
408
     * The method called to convert data from the database for display
409
     * @return Closure
410
     */
411 1
    public function view(): Closure
412
    {
413 1
        return $this->view;
414
    }
415
416
    /**
417
     * The method called to convert data from the database for display in grid
418
     * @return Closure
419
     */
420 1
    public function viewGrid(): Closure
421
    {
422 1
        return $this->viewGrid;
423
    }
424
425
}
426