Test Failed
Pull Request — master (#192)
by Sergei
02:42
created

InputField::setInputId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field\Base;
6
7
use Yiisoft\Form\Field\Part\Error;
8
use Yiisoft\Form\Field\Part\Hint;
9
use Yiisoft\Form\Field\Part\Label;
10
use Yiisoft\Form\Helper\HtmlForm;
11
use Yiisoft\Html\Html;
12
13
abstract class InputField extends PartsField
14
{
15
    use FormAttributeTrait;
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
    final public function form(?string $value): static
30
    {
31
        $new = clone $this;
32
        $new->inputAttributes['form'] = $value;
33
        return $new;
34
    }
35
36
    final public function inputId(?string $inputId): static
37
    {
38
        $new = clone $this;
39
        $new->inputId = $inputId;
40
        return $new;
41
    }
42
43
    final public function setInputId(bool $value): static
44
    {
45
        $new = clone $this;
46
        $new->setInputId = $value;
47
        return $new;
48
    }
49
50
    final public function inputAttributes(array $attributes): static
51
    {
52
        $new = clone $this;
53
        $new->inputAttributes = array_merge($new->inputAttributes, $attributes);
54
        return $new;
55
    }
56
57
    final public function replaceInputAttributes(array $attributes): static
58
    {
59
        $new = clone $this;
60
        $new->inputAttributes = $attributes;
61
        return $new;
62
    }
63
64
    /**
65
     * Add one or more CSS classes to the input tag.
66
     *
67
     * @param string|null ...$class One or many CSS classes.
68
     */
69
    final public function inputClass(?string ...$class): static
70
    {
71
        $new = clone $this;
72
        Html::addCssClass(
73
            $new->inputAttributes,
74
            array_filter($class, static fn ($c) => $c !== null),
75
        );
76
        return $new;
77
    }
78
79
    /**
80
     * Replace input tag CSS classes with a new set of classes.
81
     *
82
     * @param string|null ...$class One or many CSS classes.
83
     */
84
    final public function replaceInputClass(?string ...$class): static
85
    {
86
        $new = clone $this;
87
        $new->inputAttributes['class'] = array_filter($class, static fn ($c) => $c !== null);
88
        return $new;
89
    }
90
91
    final protected function getInputName(): string
92
    {
93
        return HtmlForm::getInputName($this->getFormModel(), $this->formAttribute);
94
    }
95
96
    final protected function getInputAttributes(): array
97
    {
98
        $attributes = $this->inputAttributes;
99
100
        $this->prepareIdInInputAttributes($attributes);
101
102
        $this->prepareInputAttributes($attributes);
103
104
        return $attributes;
105
    }
106
107
    protected function prepareInputAttributes(array &$attributes): void
108
    {
109
    }
110
111
    final protected function prepareIdInInputAttributes(array &$attributes): void
112
    {
113
        /** @var mixed $idFromTag */
114
        $idFromTag = $attributes['id'] ?? null;
115
        if ($idFromTag !== null) {
116
            $this->inputIdFromTag = (string) $idFromTag;
117
        }
118
119
        if ($this->setInputId) {
120
            if ($this->inputId !== null) {
121
                $attributes['id'] = $this->inputId;
122
            } elseif ($idFromTag === null) {
123
                $attributes['id'] = $this->getInputId();
124
            }
125
        }
126
    }
127
128
    final protected function renderLabel(Label $label): string
129
    {
130
        $label = $label->formAttribute($this->getFormModel(), $this->formAttribute);
131
132
        if ($this->setInputId === false) {
133
            $label = $label->useInputId(false);
134
        }
135
136
        if ($this->inputId !== null) {
137
            $label = $label->forId($this->inputId);
138
        } elseif ($this->inputIdFromTag !== null) {
139
            $label = $label->forId($this->inputIdFromTag);
140
        }
141
142
        return $label->render();
143
    }
144
145
    final protected function renderHint(Hint $hint): string
146
    {
147
        return $hint
148
            ->formAttribute($this->getFormModel(), $this->formAttribute)
149
            ->render();
150
    }
151
152
    final protected function renderError(Error $error): string
153
    {
154
        return $error
155
            ->formAttribute($this->getFormModel(), $this->formAttribute)
156
            ->render();
157
    }
158
}
159