Passed
Push — main ( 3c2c89...317075 )
by Garbuz
02:54
created

FieldAbstract::referencesDisabled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
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 $table
209
     * @param string $field
210
     * @param bool $hasMany
211
     * @return FieldInterface
212
     * @throws Exception
213
     */
214 1
    public function references(string $table, string $field, bool $hasMany = true): FieldInterface
215
    {
216 1
        if (mb_strlen(trim($table)) == 0 || mb_strlen(trim($field)) == 0) {
217
            throw new Exception('References should be a string and not null');
218
        }
219
        $this->references = ['table' => $table, 'field' => $field, 'hasmany' => $hasMany];
220 1
        return $this;
221 1
    }
222 1
223 1
    /**
224
     * @return FieldInterface
225
     */
226
    public function referencesDisabled(): FieldInterface
227 1
    {
228 1
        $this->references = null;
229
        return $this;
230
    }
231
232
    /**
233
     * @param bool $index
234
     * @return FieldInterface
235
     */
236
    public function index(bool $index = true): FieldInterface
237
    {
238
        $this->index = $index;
239
        return $this;
240
    }
241
242
    /**
243
     * @param bool $fillable
244
     * @return FieldInterface
245 1
     */
246
    public function fillable(bool $fillable = true): FieldInterface
247 1
    {
248 1
        $this->fillable = $fillable;
249
        return $this;
250
    }
251
252
    /**
253
     * @param bool $hidden
254
     * @return FieldInterface
255 1
     */
256
    public function hidden(bool $hidden = true): FieldInterface
257 1
    {
258 1
        $this->hidden = $hidden;
259
        return $this;
260
    }
261
262
    /**
263
     * @return string
264 2
     */
265
    public function getLabel(): string
266 2
    {
267
        return $this->label;
268
    }
269
270
    /**
271
     * @return string
272 2
     */
273
    public function getColumn(): string
274 2
    {
275
        return $this->column;
276
    }
277
278
    /**
279
     * @return string
280 1
     */
281
    public function getType(): string
282 1
    {
283
        return $this->filter->getType();
284
    }
285
286
    /**
287
     * @return int
288 1
     */
289
    public function getLight(): int
290 1
    {
291
        return $this->filter->getLight();
292
    }
293
294
    /**
295
     * @return string|null
296 1
     */
297
    public function getMask(): ?string
298 1
    {
299
        return $this->filter->getMask();
300
    }
301
302
    /**
303
     * @return string|null
304 1
     */
305
    public function getReferencesTable(): ?string
306 1
    {
307
        return $this->references['table'] ?? null;
308
    }
309
310
    /**
311
     * @return string|null
312 1
     */
313
    public function getReferencesField(): ?string
314 1
    {
315
        return $this->references['field'] ?? null;
316
    }
317
318
    /**
319
     * @return bool
320 1
     */
321
    public function getReferencesMany(): bool
322 1
    {
323
        return $this->references['hasmany'] ?? true;
324
    }
325
326
    /**
327
     * @return string|null
328 1
     */
329
    public function getPlaceholder(): ?string
330 1
    {
331
        return $this->placeholder;
332
    }
333
334
    /**
335
     * @return string|null
336
     */
337
    public function getValueDefault(): mixed
338
    {
339
        return $this->default;
340
    }
341
342
    /**
343
     * @return bool
344 1
     */
345
    public function getFillable(): bool
346 1
    {
347
        return $this->fillable;
348
    }
349
350
    /**
351
     * @return bool
352 1
     */
353
    public function getHidden(): bool
354 1
    {
355
        return $this->fillable;
356
    }
357
358
    /**
359
     * @return bool
360
     */
361
    public function getRequired(): bool
362
    {
363
        return $this->filter->getRequired();
364
    }
365
366
    /**
367
     * @return int|null
368
     */
369
    public function getMax(): ?int
370
    {
371
        return $this->filter->getMax();
372
    }
373
374
    /**
375
     * @return int|null
376
     */
377
    public function getMin(): ?int
378
    {
379
        return $this->filter->getMin();
380
    }
381
382
    /**
383
     * @return bool
384
     */
385
    public function isIndex(): bool
386
    {
387
        return $this->index;
388
    }
389
390
    /**
391
     * Method called before saving data
392
     * @return Closure
393 1
     */
394
    public function saving(): Closure
395 1
    {
396
        return $this->saving;
397
    }
398
399
    /**
400
     * Method called after saving data
401
     * @return Closure
402 1
     */
403
    public function saved(): Closure
404 1
    {
405
        return $this->saved;
406
    }
407
408
    /**
409
     * The method called to convert data from the database for display
410
     * @return Closure
411 1
     */
412
    public function view(): Closure
413 1
    {
414
        return $this->view;
415
    }
416
417
    /**
418
     * The method called to convert data from the database for display in grid
419
     * @return Closure
420 1
     */
421
    public function viewGrid(): Closure
422 1
    {
423
        return $this->viewGrid;
424
    }
425
426
}
427