Passed
Push — main ( 7e6183...0ce457 )
by Garbuz
03:08
created

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