Passed
Push — main ( 3dd4e9...e653bf )
by Garbuz
13:58
created

FieldAbstract::getMax()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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