Passed
Push — main ( 1646d4...1d29da )
by Garbuz
02:32
created

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