Passed
Pull Request — master (#160)
by Wilmer
03:13
created

FieldAttributes   F

Complexity

Total Complexity 122

Size/Duplication

Total Lines 910
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 228
dl 0
loc 910
ccs 254
cts 254
cp 1
rs 2
c 1
b 0
f 0
wmc 122

52 Methods

Rating   Name   Duplication   Size   Complexity  
A errorAttributes() 0 5 1
A container() 0 5 1
A getAriaDescribedBy() 0 10 3
A containerName() 0 5 1
A hint() 0 5 1
A required() 0 5 1
A labelAttributes() 0 5 1
A labelClass() 0 5 1
A ariaDescribedBy() 0 5 1
A defaultValues() 0 5 1
A containerId() 0 5 1
A hintAttributes() 0 5 1
A error() 0 5 1
A inputClass() 0 5 1
A errorClass() 0 5 1
A hintClass() 0 5 1
A label() 0 5 1
A placeholder() 0 5 1
A errorMessageCallback() 0 5 1
A template() 0 5 1
A containerAttributes() 0 5 1
A hintTag() 0 5 1
A labelFor() 0 5 1
A getButtonsAttributes() 0 10 4
A errorTag() 0 5 1
A validClass() 0 5 1
A getAttributes() 0 10 4
A invalidClass() 0 5 1
A containerClass() 0 5 1
A readonly() 0 5 1
A getLabelClass() 0 10 4
A getHintClass() 0 10 4
A getLabel() 0 10 4
A getErrorAttributes() 0 10 4
A getPlaceholder() 0 10 4
A getContainerClass() 0 10 4
A getLabelAttributes() 0 10 4
A getDefaultValue() 0 4 1
A getHintTag() 0 10 5
A getHint() 0 10 4
A getError() 0 10 4
A getTemplate() 0 10 5
A getErrorClass() 0 10 4
A getContainer() 0 10 3
A getValidClass() 0 10 4
A getErrorMessageCallback() 0 13 4
A getErrorTag() 0 10 5
A type() 0 5 1
A getHintAttributes() 0 10 4
A getInputClass() 0 10 4
A getInvalidClass() 0 10 4
A getContainerAttributes() 0 13 4

How to fix   Complexity   

Complex Class

Complex classes like FieldAttributes often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FieldAttributes, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Widget\Attribute;
6
7
abstract class FieldAttributes extends WidgetAttributes
8
{
9
    private ?bool $ariaDescribedBy = null;
10
    private ?bool $container = null;
11
    private array $containerAttributes = [];
12
    private string $containerClass = '';
13
    private array $defaultValues = [];
14
    private ?string $error = '';
15
    private array $errorAttributes = [];
16
    private string $errorClass = '';
17
    private array $errorMessageCallback = [];
18
    private string $errorTag = '';
19
    private ?string $hint = '';
20
    private array $hintAttributes = [];
21
    private string $hintClass = '';
22
    private string $hintTag = '';
23
    private string $inputClass = '';
24
    private ?string $label = '';
25
    private array $labelAttributes = [];
26
    private string $labelClass = '';
27
    private string $invalidClass = '';
28
    private string $validClass = '';
29
    private ?string $placeholder = null;
30
    private string $template = '';
31
    private string $type = '';
32
33
    /**
34
     * Set aria-describedby attribute.
35
     *
36
     * @param bool $value Whether to set aria-describedby attribute.
37
     *
38
     * @return static
39
     *
40
     * @link https://www.w3.org/TR/WCAG20-TECHS/ARIA1.html
41
     */
42 1
    public function ariaDescribedBy(bool $value): self
43
    {
44 1
        $new = clone $this;
45 1
        $new->ariaDescribedBy = $value;
46 1
        return $new;
47
    }
48
49
    /**
50
     * Enable, disabled container for field.
51
     *
52
     * @param bool $value Is the container disabled or not.
53
     *
54
     * @return static
55
     */
56 3
    public function container(bool $value): self
57
    {
58 3
        $new = clone $this;
59 3
        $new->container = $value;
60 3
        return $new;
61
    }
62
63
    /**
64
     * Set container attributes.
65
     *
66
     * @param array $values Attribute values indexed by attribute names.
67
     *
68
     * ```php
69
     * ['class' => 'test-class']
70
     * ```
71
     *
72
     * @return static
73
     *
74
     * @psalm-param array<string, string> $values
75
     */
76 3
    public function containerAttributes(array $values): self
77
    {
78 3
        $new = clone $this;
79 3
        $new->containerAttributes = array_merge($new->containerAttributes, $values);
80 3
        return $new;
81
    }
82
83
    /**
84
     * Set container css class.
85
     *
86
     * @return static
87
     */
88 2
    public function containerClass(string $value): self
89
    {
90 2
        $new = clone $this;
91 2
        $new->containerClass = $value;
92 2
        return $new;
93
    }
94
95
    /**
96
     * Set the ID of the container field.
97
     *
98
     * @param string|null $id
99
     *
100
     * @return static
101
     */
102 1
    public function containerId(?string $id): self
103
    {
104 1
        $new = clone $this;
105 1
        $new->containerAttributes['id'] = $id;
106 1
        return $new;
107
    }
108
109
    /**
110
     * Set the name of the container field.
111
     *
112
     * @param string|null $id
113
     *
114
     * @return static
115
     */
116 1
    public function containerName(?string $id): self
117
    {
118 1
        $new = clone $this;
119 1
        $new->containerAttributes['name'] = $id;
120 1
        return $new;
121
    }
122
123
    /**
124
     * Set default values for field widget.
125
     *
126
     * @param array $values The default values indexed by field type.
127
     *
128
     * ```php
129
     * [
130
     *     Text::class => [
131
     *         'label' => 'label-test',
132
     *     ],
133
     * ];
134
     *
135
     * @return static
136
     */
137 42
    public function defaultValues(array $values): self
138
    {
139 42
        $new = clone $this;
140 42
        $new->defaultValues = $values;
141 42
        return $new;
142
    }
143
144
    /**
145
     * Set error message for the field.
146
     *
147
     * @param string|null $value the error message.
148
     *
149
     * @return static The field widget instance.
150
     */
151 2
    public function error(?string $value): self
152
    {
153 2
        $new = clone $this;
154 2
        $new->error = $value;
155 2
        return $new;
156
    }
157
158
    /**
159
     * Set error attributes.
160
     *
161
     * @param array $values Attribute values indexed by attribute names.
162
     *
163
     * ```php
164
     * ['class' => 'test-class']
165
     * ```
166
     *
167
     * @return static The field widget instance.
168
     */
169 2
    public function errorAttributes(array $values): self
170
    {
171 2
        $new = clone $this;
172 2
        $new->errorAttributes = $values;
173 2
        return $new;
174
    }
175
176
    /**
177
     * Set error css class.
178
     *
179
     * @return static
180
     */
181 6
    public function errorClass(string $value): self
182
    {
183 6
        $new = clone $this;
184 6
        $new->errorClass = $value;
185 6
        return $new;
186
    }
187
188
    /**
189
     * Callback that will be called to obtain an error message.
190
     *
191
     * The signature of the callback must be:
192
     *
193
     * ```php
194
     * [$FormModel, function()]
195
     * ```
196
     *
197
     * @param array $value
198
     *
199
     * @return static
200
     */
201 1
    public function errorMessageCallback(array $value): self
202
    {
203 1
        $new = clone $this;
204 1
        $new->errorMessageCallback = $value;
205 1
        return $new;
206
    }
207
208
    /**
209
     * The tag name of the container element.
210
     *
211
     * Empty to render error messages without container {@see Html::tag()}.
212
     *
213
     * @param string $value
214
     *
215
     * @return static
216
     */
217 2
    public function errorTag(string $value): self
218
    {
219 2
        $new = clone $this;
220 2
        $new->errorTag = $value;
221 2
        return $new;
222
    }
223
224
    /**
225
     * Set hint message for the field.
226
     *
227
     * @return static
228
     */
229 1
    public function hint(?string $value): self
230
    {
231 1
        $new = clone $this;
232 1
        $new->hint = $value;
233 1
        return $new;
234
    }
235
236
    /**
237
     * Set hint attributes.
238
     *
239
     * @param array $values Attribute values indexed by attribute names.
240
     *
241
     * ```php
242
     * ['class' => 'test-class']
243
     * ```
244
     *
245
     * @return static The field widget instance.
246
     */
247 1
    public function hintAttributes(array $values): self
248
    {
249 1
        $new = clone $this;
250 1
        $new->hintAttributes = $values;
251 1
        return $new;
252
    }
253
254
    /**
255
     * Set hint css class.
256
     *
257
     * @return static
258
     */
259 5
    public function hintClass(string $value): self
260
    {
261 5
        $new = clone $this;
262 5
        $new->hintClass = $value;
263 5
        return $new;
264
    }
265
266
    /**
267
     * Set hint tag name.
268
     *
269
     * @return static
270
     */
271 1
    public function hintTag(string $value): self
272
    {
273 1
        $new = clone $this;
274 1
        $new->hintTag = $value;
275 1
        return $new;
276
    }
277
278
    /**
279
     * Set input css class.
280
     *
281
     * @return static
282
     */
283 1
    public function inputClass(string $value): self
284
    {
285 1
        $new = clone $this;
286 1
        $new->inputClass = $value;
287 1
        return $new;
288
    }
289
290
    /**
291
     * Set invalid css class.
292
     *
293
     * @return static
294
     */
295 5
    public function invalidClass(string $value): self
296
    {
297 5
        $new = clone $this;
298 5
        $new->invalidClass = $value;
299 5
        return $new;
300
    }
301
302
    /**
303
     * Set label message for the field.
304
     *
305
     * @return static
306
     */
307 5
    public function label(?string $value): self
308
    {
309 5
        $new = clone $this;
310 5
        $new->label = $value;
311 5
        return $new;
312
    }
313
314
    /**
315
     * Set label attributes.
316
     *
317
     * @param array $values Attribute values indexed by attribute names.
318
     *
319
     * ```php
320
     * ['class' => 'test-class']
321
     * ```
322
     *
323
     * @return static The field widget instance.
324
     */
325 1
    public function labelAttributes(array $values): self
326
    {
327 1
        $new = clone $this;
328 1
        $new->labelAttributes = $values;
329 1
        return $new;
330
    }
331
332
    /**
333
     * Set the label css class.
334
     *
335
     * @return static
336
     */
337 3
    public function labelClass(string $value): self
338
    {
339 3
        $new = clone $this;
340 3
        $new->labelClass = $value;
341 3
        return $new;
342
    }
343
344
    /**
345
     * The id of a labelable form-related element in the same document as the tag label element.
346
     *
347
     * The first element in the document with an id matching the value of the for attribute is the labeled control for
348
     * this label element, if it is a labelable element.
349
     *
350
     * @param string|null $value The id of a labelable form-related element in the same document as the tag label
351
     * element. If null, the attribute will be removed.
352
     *
353
     * @return static
354
     */
355 1
    public function labelFor(?string $value): self
356
    {
357 1
        $new = clone $this;
358 1
        $new->labelAttributes['for'] = $value;
359 1
        return $new;
360
    }
361
362
    /**
363
     * It allows defining placeholder.
364
     *
365
     * @param string $value
366
     *
367
     * @return static
368
     *
369
     * @link https://html.spec.whatwg.org/multipage/input.html#the-placeholder-attribute
370
     */
371 8
    public function placeholder(string $value): self
372
    {
373 8
        $new = clone $this;
374 8
        $new->placeholder = $value;
375 8
        return $new;
376
    }
377
378
    /**
379
     * A Boolean attribute which, if present, means this field cannot be edited by the user.
380
     * Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement.value
381
     * property.
382
     *
383
     * @param bool $value
384
     *
385
     * @return static
386
     *
387
     * @link https://html.spec.whatwg.org/multipage/input.html#the-readonly-attribute
388
     */
389 10
    public function readonly(bool $value = true): self
390
    {
391 10
        $new = clone $this;
392 10
        $new->attributes['readonly'] = $value;
393 10
        return $new;
394
    }
395
396
    /**
397
     * If it is required to fill in a value in order to submit the form.
398
     *
399
     * @return static
400
     *
401
     * @link https://www.w3.org/TR/html52/sec-forms.html#the-required-attribute
402
     */
403 15
    public function required(): self
404
    {
405 15
        $new = clone $this;
406 15
        $new->attributes['required'] = true;
407 15
        return $new;
408
    }
409
410
    /**
411
     * Set layout template for render a field.
412
     *
413
     * @param string $value
414
     *
415
     * @return static
416
     */
417 1
    public function template(string $value): self
418
    {
419 1
        $new = clone $this;
420 1
        $new->template = $value;
421 1
        return $new;
422
    }
423
424
    /**
425
     * Set the value valid css class.
426
     *
427
     * @param string $value is the valid css class.
428
     *
429
     * @return static
430
     */
431 5
    public function validClass(string $value): self
432
    {
433 5
        $new = clone $this;
434 5
        $new->validClass = $value;
435 5
        return $new;
436
    }
437
438
    /**
439
     * Return aria described by field.
440
     *
441
     * if aria described by is not set, and aria described by default is set, then return aria described by default.
442
     *
443
     * @return bool|null
444
     */
445 369
    protected function getAriaDescribedBy(): ?bool
446
    {
447 369
        $ariaDescribedBy = $this->ariaDescribedBy;
448 369
        $ariaDescribedByDefault = $this->getDefaultValue($this->type, 'ariaDescribedBy');
449
450 369
        if ($ariaDescribedBy === null && is_bool($ariaDescribedByDefault)) {
451 1
            $ariaDescribedBy = $ariaDescribedByDefault;
452
        }
453
454 369
        return $ariaDescribedBy;
455
    }
456
457
    /**
458
     * Return attributes for field.
459
     *
460
     * if attributes is empty string, and attributes default value is not empty string, then return attributes default
461
     * value.
462
     *
463
     * @return array
464
     */
465 381
    protected function getAttributes(): array
466
    {
467 381
        $attributes = $this->attributes;
468 381
        $attributesDefault = $this->getDefaultValue($this->type, 'attributes');
469
470 381
        if ($attributes === [] && (is_array($attributesDefault) && $attributesDefault !== [])) {
471 1
            $attributes = $attributesDefault;
472
        }
473
474 381
        return $attributes;
475
    }
476
477
    /**
478
     * Return attributes button for the field.
479
     *
480
     * if attributes button is empty string, and attributes button default value is not empty string, then return
481
     * attributes default value.
482
     *
483
     * @param string $index The index of the attributes button.
484
     *
485
     * @return array
486
     */
487 34
    protected function getButtonsAttributes(string $index): array
488
    {
489 34
        $buttonAttributes = $this->attributes;
490 34
        $butonDefaultAttributes = $this->getDefaultValue($index, 'attributes');
491
492 34
        if ($buttonAttributes === [] && (is_array($butonDefaultAttributes) && $butonDefaultAttributes !== [])) {
493 2
            $buttonAttributes = $butonDefaultAttributes;
494
        }
495
496 34
        return $buttonAttributes;
497
    }
498
499
    /**
500
     * Return enabled, disabled container for field.
501
     *
502
     * if container is not set, and container default value is bool, then return container default value.
503
     *
504
     * @return bool
505
     */
506 397
    protected function getContainer(): ?bool
507
    {
508 397
        $container = $this->container;
509 397
        $containerDefault = $this->getDefaultValue($this->type, 'container');
510
511 397
        if ($container === null && is_bool($containerDefault)) {
512 3
            $container = $containerDefault;
513
        }
514
515 397
        return $container ?? true;
516
    }
517
518
    /**
519
     * Return attributes for container for field.
520
     *
521
     * if attributes container is empty array, and attributes container default value is not empty array, then return
522
     * attributes container default value.
523
     *
524
     * @return array
525
     */
526 397
    protected function getContainerAttributes(): array
527
    {
528 397
        $containerAttributes = $this->containerAttributes;
529 397
        $containerDefaultAttributes = $this->getDefaultValue($this->type, 'containerAttributes');
530
531
        if (
532 397
            $containerAttributes === []
533 397
            && (is_array($containerDefaultAttributes) && $containerDefaultAttributes !== [])
534
        ) {
535 3
            $containerAttributes = $containerDefaultAttributes;
536
        }
537
538 397
        return $containerAttributes;
539
    }
540
541
    /**
542
     * Return class for container field.
543
     *
544
     * if container class is empty string, and container class default value is not empty string, then return container
545
     * class default value.
546
     *
547
     * @return string
548
     */
549 397
    protected function getContainerClass(): string
550
    {
551 397
        $containerClass = $this->containerClass;
552 397
        $containerDefaultClass = $this->getDefaultValue($this->type, 'containerClass');
553
554 397
        if ($containerClass === '' && (is_string($containerDefaultClass) && $containerDefaultClass !== '')) {
555 2
            $containerClass = $containerDefaultClass;
556
        }
557
558 397
        return $containerClass;
559
    }
560
561
    /**
562
     * Return error message for the field.
563
     *
564
     * if error message is empty string, and error message default value is not empty string, then return error message
565
     * default value.
566
     *
567
     * @return string
568
     */
569 350
    protected function getError(): ?string
570
    {
571 350
        $error = $this->error;
572 350
        $errorDefault = $this->getDefaultValue($this->type, 'error');
573
574 350
        if ($error === '' && (is_string($errorDefault) && $errorDefault !== '')) {
575 1
            $error = $errorDefault;
576
        }
577
578 350
        return $error;
579
    }
580
581
    /**
582
     * Return error attribute for the field.
583
     *
584
     * if error attribute is empty string, and error attribute default value is not empty string, then return error
585
     * attribute default value.
586
     *
587
     * @return array
588
     */
589 349
    protected function getErrorAttributes(): array
590
    {
591 349
        $errorAttributes = $this->errorAttributes;
592 349
        $errorAttributesDefault = $this->getDefaultValue($this->type, 'errorAttributes');
593
594 349
        if ($errorAttributes === [] && (is_array($errorAttributesDefault) && $errorAttributesDefault !== [])) {
595 2
            $errorAttributes = $errorAttributesDefault;
596
        }
597
598 349
        return $errorAttributes;
599
    }
600
601
    /**
602
     * Return error class for the field.
603
     *
604
     * if error class is empty string, and error class default value is not empty string, then return error class
605
     * default value.
606
     *
607
     * @return string
608
     */
609 349
    protected function getErrorClass(): string
610
    {
611 349
        $errorClass = $this->errorClass;
612 349
        $errorClassDefault = $this->getDefaultValue($this->type, 'errorClass');
613
614 349
        if ($errorClass === '' && (is_string($errorClassDefault) && $errorClassDefault !== '')) {
615 4
            $errorClass = $errorClassDefault;
616
        }
617
618 349
        return $errorClass;
619
    }
620
621
    /**
622
     * Return error message callback for the field.
623
     *
624
     * if error message callback is empty array, and error message callback default value is not empty array, then
625
     * return error message callback default value.
626
     */
627 349
    protected function getErrorMessageCallback(): array
628
    {
629 349
        $errorMessageCallback = $this->errorMessageCallback;
630 349
        $errorMessageCallbackDefault = $this->getDefaultValue($this->type, 'errorMessageCallback');
631
632
        if (
633 349
            $errorMessageCallback === []
634 349
            && (is_array($errorMessageCallbackDefault) && $errorMessageCallbackDefault !== [])
635
        ) {
636 1
            $errorMessageCallback = $errorMessageCallbackDefault;
637
        }
638
639 349
        return $errorMessageCallback;
640
    }
641
642
    /**
643
     * Return error tag for the field.
644
     *
645
     * if error tag is empty string, and error tag default value is not empty string, then return error tag default
646
     * value.
647
     *
648
     * @return string
649
     */
650 349
    protected function getErrorTag(): string
651
    {
652 349
        $errorTag = $this->errorTag;
653 349
        $errorTagDefault = $this->getDefaultValue($this->type, 'errorTag');
654
655 349
        if ($errorTag === '' && (is_string($errorTagDefault) && $errorTagDefault !== '')) {
656 2
            $errorTag = $errorTagDefault;
657
        }
658
659 349
        return $errorTag === '' ? 'div' : $errorTag;
660
    }
661
662
    /**
663
     * Return hint for field.
664
     *
665
     * if hint is empty string, and hint default value is not empty string, then return hint default value.
666
     *
667
     * @return string
668
     */
669 350
    protected function getHint(): ?string
670
    {
671 350
        $hint = $this->hint;
672 350
        $hintDefault = $this->getDefaultValue($this->type, 'hint') ?? '';
673
674 350
        if ($hint === '' && (is_string($hintDefault) && $hintDefault !== '')) {
675 3
            $hint = $hintDefault;
676
        }
677
678 350
        return $hint;
679
    }
680
681
    /**
682
     * Return hint attributes for field.
683
     *
684
     * if hint attributes is empty array, and hint default value is not empty array, then return hint default value.
685
     *
686
     * @return array
687
     */
688 350
    protected function getHintAttributes(): array
689
    {
690 350
        $hintAttributes = $this->hintAttributes;
691 350
        $hintDefault = $this->getDefaultValue($this->type, 'hintAttributes') ?? [];
692
693 350
        if ($hintAttributes === [] && (is_array($hintDefault) && $hintDefault !== [])) {
694 1
            $hintAttributes = $hintDefault;
695
        }
696
697 350
        return $hintAttributes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $hintAttributes could return the type boolean|string which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
698
    }
699
700
    /**
701
     * Return hint class for field.
702
     *
703
     * if hint class is empty string, and hint default value is not empty string, then return hint default value.
704
     *
705
     * @return string
706
     */
707 350
    protected function getHintClass(): string
708
    {
709 350
        $hintClass = $this->hintClass;
710 350
        $hintDefault = $this->getDefaultValue($this->type, 'hintClass') ?? '';
711
712 350
        if ($hintClass === '' && (is_string($hintDefault) && $hintDefault !== '')) {
713 1
            $hintClass = $hintDefault;
714
        }
715
716 350
        return $hintClass;
717
    }
718
719
    /**
720
     * Return hint tag for field.
721
     *
722
     * if hint tag is empty string, and hint default value is not empty string, then return hint default value.
723
     *
724
     * @return string
725
     */
726 350
    protected function getHintTag(): string
727
    {
728 350
        $hintTag = $this->hintTag;
729 350
        $hintDefault = $this->getDefaultValue($this->type, 'hintTag') ?? '';
730
731 350
        if ($hintTag === '' && (is_string($hintDefault) && $hintDefault !== '')) {
732 1
            $hintTag = $hintDefault;
733
        }
734
735 350
        return $hintTag === '' ? 'div' : $hintTag;
736
    }
737
738
    /**
739
     * Return input class for field.
740
     *
741
     * if input class is empty string, and input class default value is not empty string, then return input class
742
     * default value.
743
     *
744
     * @return string
745
     */
746 403
    protected function getInputClass(): string
747
    {
748 403
        $inputClass = $this->inputClass;
749 403
        $inputDefaultClass = $this->getDefaultValue($this->type, 'inputClass');
750
751 403
        if ($inputClass === '' && (is_string($inputDefaultClass) && $inputDefaultClass !== '')) {
752 1
            $inputClass = $inputDefaultClass;
753
        }
754
755 403
        return $inputClass;
756
    }
757
758
    /**
759
     * Return invalid class for field.
760
     *
761
     * if invalid class is empty string, and invalid class default value is not empty string, then return invalid class
762
     * default value.
763
     *
764
     * @return string
765
     */
766 369
    protected function getInvalidClass(): string
767
    {
768 369
        $invalidClass = $this->invalidClass;
769 369
        $invalidDefaultClass = $this->getDefaultValue($this->type, 'invalidClass');
770
771 369
        if ($invalidClass === '' && (is_string($invalidDefaultClass) && $invalidDefaultClass !== '')) {
772 2
            $invalidClass = $invalidDefaultClass;
773
        }
774
775 369
        return $invalidClass;
776
    }
777
778
    /**
779
     * Return label for field.
780
     *
781
     * if label is empty string, and label default value is not empty string, then return label default value.
782
     *
783
     * @return string|null
784
     */
785 319
    protected function getLabel(): ?string
786
    {
787 319
        $label = $this->label;
788 319
        $labelDefault = $this->getDefaultValue($this->type, 'label') ?? '';
789
790 319
        if ($label === '' && (is_string($labelDefault) && $labelDefault !== '')) {
791 1
            $label = $labelDefault;
792
        }
793
794 319
        return $label;
795
    }
796
797
    /**
798
     * Return label attributes for field.
799
     *
800
     * if label attributes is empty array, and label attributes default value is not empty array, then return label
801
     * attributes default value.
802
     *
803
     * @return array
804
     */
805 319
    protected function getLabelAttributes(): array
806
    {
807 319
        $labelAttributes = $this->labelAttributes;
808 319
        $labelAttributesDefault = $this->getDefaultValue($this->type, 'labelAttributes') ?? [];
809
810 319
        if ($labelAttributes === [] && (is_array($labelAttributesDefault) && $labelAttributesDefault !== [])) {
811 1
            $labelAttributes = $labelAttributesDefault;
812
        }
813
814 319
        return $labelAttributes;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $labelAttributes could return the type boolean|string which is incompatible with the type-hinted return array. Consider adding an additional type-check to rule them out.
Loading history...
815
    }
816
817
    /**
818
     * Return label css class for field.
819
     *
820
     * if label css class is empty string, and label css class default value is not null, then return label css class
821
     * default value.
822
     *
823
     * @return string
824
     */
825 319
    protected function getLabelClass(): string
826
    {
827 319
        $labelClass = $this->labelClass;
828 319
        $labelClassDefault = $this->getDefaultValue($this->type, 'labelClass') ?? '';
829
830 319
        if ($labelClass === '' && (is_string($labelClassDefault) && $labelClassDefault !== '')) {
831 1
            $labelClass = $labelClassDefault;
832
        }
833
834 319
        return $labelClass;
835
    }
836
837
    /**
838
     * Return placeholder for field.
839
     *
840
     * if placeholder is empty string, and placeholder default value is not empty string, then return placeholder
841
     * default value.
842
     *
843
     * @return string
844
     */
845 369
    protected function getPlaceholder(): ?string
846
    {
847 369
        $placeholder = $this->placeholder;
848 369
        $placeholderDefault = $this->getDefaultValue($this->type, 'placeholder') ?? '';
849
850 369
        if ($placeholder === null && (is_string($placeholderDefault) && $placeholderDefault !== '')) {
851 1
            $placeholder = $placeholderDefault;
852
        }
853
854 369
        return $placeholder;
855
    }
856
857
    /**
858
     * Return template for field.
859
     *
860
     * if template is empty string, and template default value is not empty string, then return template default value.
861
     *
862
     * @return string
863
     */
864 351
    protected function getTemplate(): string
865
    {
866 351
        $template = $this->template;
867 351
        $templateDefault = $this->getDefaultValue($this->type, 'template') ?? '';
868
869 351
        if ($template === '' && (is_string($templateDefault) && $templateDefault !== '')) {
870 1
            $template = $templateDefault;
871
        }
872
873 351
        return $template === '' ? "{label}\n{input}\n{hint}\n{error}" : $template;
874
    }
875
876
    /**
877
     * Return valid class for field.
878
     *
879
     * if valid class is empty string, and valid class default value is not empty string, then return valid class
880
     * default value.
881
     *
882
     * @return string
883
     */
884 369
    protected function getValidClass(): string
885
    {
886 369
        $validClass = $this->validClass;
887 369
        $validDefaultClass = $this->getDefaultValue($this->type, 'validClass') ?? '';
888
889 369
        if ($validClass === '' && (is_string($validDefaultClass) && $validDefaultClass !== '')) {
890 1
            $validClass = $validDefaultClass;
891
        }
892
893 369
        return $validClass;
894
    }
895
896
    /**
897
     * Set type class of the field.
898
     *
899
     * @param string $value The type class of the field.
900
     *
901
     * @return static
902
     */
903 418
    protected function type(string $type): self
904
    {
905 418
        $new = clone $this;
906 418
        $new->type = $type;
907 418
        return $new;
908
    }
909
910
    /**
911
     * @return array|bool|string|null
912
     */
913 415
    private function getDefaultValue(string $type, string $key)
914
    {
915
        /** @var array|string|null */
916 415
        return $this->defaultValues[$type][$key] ?? null;
917
    }
918
}
919