InputField::prepareIdInInputAttributes()   A
last analyzed

Complexity

Conditions 5
Paths 8

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 5
rs 9.6111
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Base;
6
7
use Yiisoft\Form\Field\Base\InputData\InputDataWithCustomNameAndValueTrait;
8
use Yiisoft\Form\Field\Part\Error;
9
use Yiisoft\Form\Field\Part\Hint;
10
use Yiisoft\Form\Field\Part\Label;
11
use Yiisoft\Html\Html;
12
13
abstract class InputField extends PartsField
14
{
15
    use InputDataWithCustomNameAndValueTrait;
16
17
    protected ?string $inputId = null;
18
    protected ?string $inputIdFromTag = null;
19
    protected bool $setInputId = true;
20
21
    protected array $inputAttributes = [];
22
23
    /**
24
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the ID
25
     * attribute of a form element in the same document.
26
     *
27
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
28
     */
29 3
    final public function form(?string $value): static
30
    {
31 3
        $new = clone $this;
32 3
        $new->inputAttributes['form'] = $value;
33 3
        return $new;
34
    }
35
36 5
    final public function inputId(?string $inputId): static
37
    {
38 5
        $new = clone $this;
39 5
        $new->inputId = $inputId;
40 5
        return $new;
41
    }
42
43 3
    final public function setInputId(bool $value): static
44
    {
45 3
        $new = clone $this;
46 3
        $new->setInputId = $value;
47 3
        return $new;
48
    }
49
50 7
    final public function inputAttributes(array $attributes): static
51
    {
52 7
        $new = clone $this;
53 7
        $new->inputAttributes = $attributes;
54 7
        return $new;
55
    }
56
57 3
    final public function addInputAttributes(array $attributes): static
58
    {
59 3
        $new = clone $this;
60 3
        $new->inputAttributes = array_merge($new->inputAttributes, $attributes);
61 3
        return $new;
62
    }
63
64
    /**
65
     * Replace input tag CSS classes with a new set of classes.
66
     *
67
     * @param string|null ...$class One or many CSS classes.
68
     */
69 12
    final public function inputClass(?string ...$class): static
70
    {
71 12
        $new = clone $this;
72 12
        $new->inputAttributes['class'] = array_filter($class, static fn ($c) => $c !== null);
73 12
        return $new;
74
    }
75
76
    /**
77
     * Add one or more CSS classes to the input tag.
78
     *
79
     * @param string|null ...$class One or many CSS classes.
80
     */
81 9
    final public function addInputClass(?string ...$class): static
82
    {
83 9
        $new = clone $this;
84 9
        Html::addCssClass($new->inputAttributes, $class);
85 9
        return $new;
86
    }
87
88 421
    final protected function getInputAttributes(): array
89
    {
90 421
        $attributes = $this->inputAttributes;
91
92 421
        $this->prepareIdInInputAttributes($attributes);
93
94 421
        $this->prepareInputAttributes($attributes);
95
96 421
        return $attributes;
97
    }
98
99 23
    protected function prepareInputAttributes(array &$attributes): void
100
    {
101 23
    }
102
103 421
    final protected function prepareIdInInputAttributes(array &$attributes): void
104
    {
105 421
        $idFromTag = $attributes['id'] ?? null;
106 421
        if ($idFromTag !== null) {
107 4
            $this->inputIdFromTag = (string) $idFromTag;
108
        }
109
110 421
        if ($this->setInputId) {
111 419
            if ($this->inputId !== null) {
112 4
                $attributes['id'] = $this->inputId;
113 415
            } elseif ($idFromTag === null) {
114 412
                $attributes['id'] = $this->getInputData()->getId();
115
            }
116
        }
117
    }
118
119 253
    final protected function renderLabel(Label $label): string
120
    {
121 253
        $label = $label->inputData($this->getInputData());
122
123 253
        if ($this->setInputId === false) {
124 2
            $label = $label->useInputId(false);
125
        }
126
127 253
        if ($this->inputId !== null) {
128 4
            $label = $label->forId($this->inputId);
129 249
        } elseif ($this->inputIdFromTag !== null) {
130 3
            $label = $label->forId($this->inputIdFromTag);
131
        }
132
133 253
        return $label->render();
134
    }
135
136 428
    final protected function renderHint(Hint $hint): string
137
    {
138 428
        return $hint
139 428
            ->inputData($this->getInputData())
140 428
            ->render();
141
    }
142
143 428
    final protected function renderError(Error $error): string
144
    {
145 428
        return $error
146 428
            ->inputData($this->getInputData())
147 428
            ->render();
148
    }
149
}
150