Passed
Push — master ( f04a33...94f699 )
by Alexander
02:20
created

FieldAttributes::replaceIndividualToken()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

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