Passed
Pull Request — master (#160)
by Wilmer
02:39
created

FieldAttributes::addValidatorAttributeHtml()   C

Complexity

Conditions 12
Paths 49

Size

Total Lines 48
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 22
nc 49
nop 4
dl 0
loc 48
ccs 23
cts 23
cp 1
crap 12
rs 6.9666
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A FieldAttributes::errorClass() 0 5 1
A FieldAttributes::errorMessageCallback() 0 5 1
A FieldAttributes::errorTag() 0 5 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    /**
501
     * Return enabled, disabled container for field.
502
     *
503
     * if container is not set, and container default value is bool, then return container default value.
504
     *
505
     * @return bool
506
     */
507 397
    protected function getContainer(): ?bool
508
    {
509 397
        $container = $this->container;
510 397
        $containerDefault = $this->getDefaultValue($this->type, 'container');
511
512 397
        if ($container === null && is_bool($containerDefault)) {
513 3
            $container = $containerDefault;
514
        }
515
516 397
        return $container ?? true;
517
    }
518
519
    /**
520
     * Return attributes for container for field.
521
     *
522
     * if attributes container is empty array, and attributes container default value is not empty array, then return
523
     * attributes container default value.
524
     *
525
     * @return array
526
     */
527 397
    protected function getContainerAttributes(): array
528
    {
529 397
        $containerAttributes = $this->containerAttributes;
530 397
        $containerDefaultAttributes = $this->getDefaultValue($this->type, 'containerAttributes');
531
532
        if (
533 397
            $containerAttributes === []
534 397
            && (is_array($containerDefaultAttributes) && $containerDefaultAttributes !== [])
535
        ) {
536 3
            $containerAttributes = $containerDefaultAttributes;
537
        }
538
539 397
        return $containerAttributes;
540
    }
541
542
    /**
543
     * Return class for container field.
544
     *
545
     * if container class is empty string, and container class default value is not empty string, then return container
546
     * class default value.
547
     *
548
     * @return string
549
     */
550 397
    protected function getContainerClass(): string
551
    {
552 397
        $containerClass = $this->containerClass;
553 397
        $containerDefaultClass = $this->getDefaultValue($this->type, 'containerClass');
554
555 397
        if ($containerClass === '' && (is_string($containerDefaultClass) && $containerDefaultClass !== '')) {
556 2
            $containerClass = $containerDefaultClass;
557
        }
558
559 397
        return $containerClass;
560
    }
561
562
    /**
563
     * Return error message for the field.
564
     *
565
     * if error message is empty string, and error message default value is not empty string, then return error message
566
     * default value.
567
     *
568
     * @return string
569
     */
570 350
    protected function getError(): ?string
571
    {
572 350
        $error = $this->error;
573 350
        $errorDefault = $this->getDefaultValue($this->type, 'error');
574
575 350
        if ($error === '' && (is_string($errorDefault) && $errorDefault !== '')) {
576 1
            $error = $errorDefault;
577
        }
578
579 350
        return $error;
580
    }
581
582
    /**
583
     * Return error attribute for the field.
584
     *
585
     * if error attribute is empty string, and error attribute default value is not empty string, then return error
586
     * attribute default value.
587
     *
588
     * @return array
589
     */
590 349
    protected function getErrorAttributes(): array
591
    {
592 349
        $errorAttributes = $this->errorAttributes;
593 349
        $errorAttributesDefault = $this->getDefaultValue($this->type, 'errorAttributes');
594
595 349
        if ($errorAttributes === [] && (is_array($errorAttributesDefault) && $errorAttributesDefault !== [])) {
596 2
            $errorAttributes = $errorAttributesDefault;
597
        }
598
599 349
        return $errorAttributes;
600
    }
601
602
    /**
603
     * Return error class for the field.
604
     *
605
     * if error class is empty string, and error class default value is not empty string, then return error class
606
     * default value.
607
     *
608
     * @return string
609
     */
610 349
    protected function getErrorClass(): string
611
    {
612 349
        $errorClass = $this->errorClass;
613 349
        $errorClassDefault = $this->getDefaultValue($this->type, 'errorClass');
614
615 349
        if ($errorClass === '' && (is_string($errorClassDefault) && $errorClassDefault !== '')) {
616 4
            $errorClass = $errorClassDefault;
617
        }
618
619 349
        return $errorClass;
620
    }
621
622
    /**
623
     * Return error message callback for the field.
624
     *
625
     * if error message callback is empty array, and error message callback default value is not empty array, then
626
     * return error message callback default value.
627
     */
628 349
    protected function getErrorMessageCallback(): array
629
    {
630 349
        $errorMessageCallback = $this->errorMessageCallback;
631 349
        $errorMessageCallbackDefault = $this->getDefaultValue($this->type, 'errorMessageCallback');
632
633
        if (
634 349
            $errorMessageCallback === []
635 349
            && (is_array($errorMessageCallbackDefault) && $errorMessageCallbackDefault !== [])
636
        ) {
637 1
            $errorMessageCallback = $errorMessageCallbackDefault;
638
        }
639
640 349
        return $errorMessageCallback;
641
    }
642
643
644
    /**
645
     * Return error tag for the field.
646
     *
647
     * if error tag is empty string, and error tag default value is not empty string, then return error tag default
648
     * value.
649
     *
650
     * @return string
651
     */
652 349
    protected function getErrorTag(): string
653
    {
654 349
        $errorTag = $this->errorTag;
655 349
        $errorTagDefault = $this->getDefaultValue($this->type, 'errorTag');
656
657 349
        if ($errorTag === '' && (is_string($errorTagDefault) && $errorTagDefault !== '')) {
658 2
            $errorTag = $errorTagDefault;
659
        }
660
661 349
        return $errorTag === '' ? 'div' : $errorTag;
662
    }
663
664
665
    /**
666
     * Return hint for field.
667
     *
668
     * if hint is empty string, and hint default value is not empty string, then return hint default value.
669
     *
670
     * @return string
671
     */
672 350
    protected function getHint(): ?string
673
    {
674 350
        $hint = $this->hint;
675 350
        $hintDefault = $this->getDefaultValue($this->type, 'hint') ?? '';
676
677 350
        if ($hint === '' && (is_string($hintDefault) && $hintDefault !== '')) {
678 3
            $hint = $hintDefault;
679
        }
680
681 350
        return $hint;
682
    }
683
684
    /**
685
     * Return hint attributes for field.
686
     *
687
     * if hint attributes is empty array, and hint default value is not empty array, then return hint default value.
688
     *
689
     * @return array
690
     */
691 350
    protected function getHintAttributes(): array
692
    {
693 350
        $hintAttributes = $this->hintAttributes;
694 350
        $hintDefault = $this->getDefaultValue($this->type, 'hintAttributes') ?? [];
695
696 350
        if ($hintAttributes === [] && (is_array($hintDefault) && $hintDefault !== []) ) {
697 1
            $hintAttributes = $hintDefault;
698
        }
699
700 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...
701
    }
702
703
    /**
704
     * Return hint class for field.
705
     *
706
     * if hint class is empty string, and hint default value is not empty string, then return hint default value.
707
     *
708
     * @return string
709
     */
710 350
    protected function getHintClass(): string
711
    {
712 350
        $hintClass = $this->hintClass;
713 350
        $hintDefault = $this->getDefaultValue($this->type, 'hintClass') ?? '';
714
715 350
        if ($hintClass === '' && (is_string($hintDefault) && $hintDefault !== '')) {
716 1
            $hintClass = $hintDefault;
717
        }
718
719 350
        return $hintClass;
720
    }
721
722
    /**
723
     * Return hint tag for field.
724
     *
725
     * if hint tag is empty string, and hint default value is not empty string, then return hint default value.
726
     *
727
     * @return string
728
     */
729 350
    protected function getHintTag(): string
730
    {
731 350
        $hintTag = $this->hintTag;
732 350
        $hintDefault = $this->getDefaultValue($this->type, 'hintTag') ?? '';
733
734 350
        if ($hintTag === '' && (is_string($hintDefault) && $hintDefault !== '')) {
735 1
            $hintTag = $hintDefault;
736
        }
737
738 350
        return $hintTag === '' ? 'div' : $hintTag;
739
    }
740
741
    /**
742
     * Return input class for field.
743
     *
744
     * if input class is empty string, and input class default value is not empty string, then return input class
745
     * default value.
746
     *
747
     * @return string
748
     */
749 403
    protected function getInputClass(): string
750
    {
751 403
        $inputClass = $this->inputClass;
752 403
        $inputDefaultClass = $this->getDefaultValue($this->type, 'inputClass');
753
754 403
        if ($inputClass === '' && (is_string($inputDefaultClass) && $inputDefaultClass !== '')) {
755 1
            $inputClass = $inputDefaultClass;
756
        }
757
758 403
        return $inputClass;
759
    }
760
761
    /**
762
     * Return invalid class for field.
763
     *
764
     * if invalid class is empty string, and invalid class default value is not empty string, then return invalid class
765
     * default value.
766
     *
767
     * @return string
768
     */
769 369
    protected function getInvalidClass(): string
770
    {
771 369
        $invalidClass = $this->invalidClass;
772 369
        $invalidDefaultClass = $this->getDefaultValue($this->type, 'invalidClass');
773
774 369
        if ($invalidClass === '' && (is_string($invalidDefaultClass) && $invalidDefaultClass !== '')) {
775 2
            $invalidClass = $invalidDefaultClass;
776
        }
777
778 369
        return $invalidClass;
779
    }
780
781
    /**
782
     * Return label for field.
783
     *
784
     * if label is empty string, and label default value is not empty string, then return label default value.
785
     *
786
     * @return string|null
787
     */
788 319
    protected function getLabel(): ?string
789
    {
790 319
        $label = $this->label;
791 319
        $labelDefault = $this->getDefaultValue($this->type, 'label') ?? '';
792
793 319
        if ($label === '' && (is_string($labelDefault) && $labelDefault !== '')) {
794 1
            $label = $labelDefault;
795
        }
796
797 319
        return $label;
798
    }
799
800
    /**
801
     * Return label attributes for field.
802
     *
803
     * if label attributes is empty array, and label attributes default value is not empty array, then return label
804
     * attributes default value.
805
     *
806
     * @return array
807
     */
808 319
    protected function getLabelAttributes(): array
809
    {
810 319
        $labelAttributes = $this->labelAttributes;
811 319
        $labelAttributesDefault = $this->getDefaultValue($this->type, 'labelAttributes') ?? [];
812
813 319
        if ($labelAttributes === [] && (is_array($labelAttributesDefault) && $labelAttributesDefault !== [])) {
814 1
            $labelAttributes = $labelAttributesDefault;
815
        }
816
817 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...
818
    }
819
820
    /**
821
     * Return label css class for field.
822
     *
823
     * if label css class is empty string, and label css class default value is not null, then return label css class
824
     * default value.
825
     *
826
     * @return string
827
     */
828 319
    protected function getLabelClass(): string
829
    {
830 319
        $labelClass = $this->labelClass;
831 319
        $labelClassDefault = $this->getDefaultValue($this->type, 'labelClass') ?? '';
832
833 319
        if ($labelClass === '' && (is_string($labelClassDefault) && $labelClassDefault !== '')) {
834 1
            $labelClass = $labelClassDefault;
835
        }
836
837 319
        return $labelClass;
838
    }
839
840
    /**
841
     * Return placeholder for field.
842
     *
843
     * if placeholder is empty string, and placeholder default value is not empty string, then return placeholder
844
     * default value.
845
     *
846
     * @return string
847
     */
848 369
    protected function getPlaceholder(): ?string
849
    {
850 369
        $placeholder = $this->placeholder;
851 369
        $placeholderDefault = $this->getDefaultValue($this->type, 'placeholder') ?? '';
852
853 369
        if ($placeholder === null && (is_string($placeholderDefault) && $placeholderDefault !== '')) {
854 1
            $placeholder = $placeholderDefault;
855
        }
856
857 369
        return $placeholder;
858
    }
859
860
    /**
861
     * Return template for field.
862
     *
863
     * if template is empty string, and template default value is not empty string, then return template default value.
864
     *
865
     * @return string
866
     */
867 351
    protected function getTemplate(): string
868
    {
869 351
        $template = $this->template;
870 351
        $templateDefault = $this->getDefaultValue($this->type, 'template') ?? '';
871
872 351
        if ($template === '' && (is_string($templateDefault) && $templateDefault !== '')) {
873 1
            $template = $templateDefault;
874
        }
875
876 351
        return $template === '' ? "{label}\n{input}\n{hint}\n{error}" : $template;
877
    }
878
879
    /**
880
     * Return valid class for field.
881
     *
882
     * if valid class is empty string, and valid class default value is not empty string, then return valid class
883
     * default value.
884
     *
885
     * @return string
886
     */
887 369
    protected function getValidClass(): string
888
    {
889 369
        $validClass = $this->validClass;
890 369
        $validDefaultClass = $this->getDefaultValue($this->type, 'validClass') ?? '';
891
892 369
        if ($validClass === '' && (is_string($validDefaultClass) && $validDefaultClass !== '')) {
893 1
            $validClass = $validDefaultClass;
894
        }
895
896 369
        return $validClass;
897
    }
898
899
    /**
900
     * Set type class of the field.
901
     *
902
     * @param string $value The type class of the field.
903
     *
904
     * @return static
905
     */
906 418
    protected function type(string $type): self
907
    {
908 418
        $new = clone $this;
909 418
        $new->type = $type;
910 418
        return $new;
911
    }
912
913
    /**
914
     * @return array|bool|string|null
915
     */
916 415
    private function getDefaultValue(string $type, string $key)
917
    {
918
        /** @var array|string|null */
919 415
        $defaultValue = $this->defaultValues[$type][$key] ?? null;
920 415
        return $defaultValue;
921
    }
922
}
923