Passed
Pull Request — master (#192)
by Sergei
13:59
created

InputField::replaceInputAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
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 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 4
    final public function inputId(?string $inputId): static
37
    {
38 4
        $new = clone $this;
39 4
        $new->inputId = $inputId;
40 4
        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 = array_merge($new->inputAttributes, $attributes);
54 7
        return $new;
55
    }
56
57 1
    final public function replaceInputAttributes(array $attributes): static
58
    {
59 1
        $new = clone $this;
60 1
        $new->inputAttributes = $attributes;
61 1
        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 17
    final public function inputClass(?string ...$class): static
70
    {
71 17
        $new = clone $this;
72 17
        Html::addCssClass(
73 17
            $new->inputAttributes,
74 17
            array_filter($class, static fn ($c) => $c !== null),
75
        );
76 17
        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 7
    final public function replaceInputClass(?string ...$class): static
85
    {
86 7
        $new = clone $this;
87 7
        $new->inputAttributes['class'] = array_filter($class, static fn ($c) => $c !== null);
88 7
        return $new;
89
    }
90
91 318
    final protected function getInputName(): string
92
    {
93 318
        return HtmlForm::getInputName($this->getFormModel(), $this->formAttribute);
94
    }
95
96 317
    final protected function getInputAttributes(): array
97
    {
98 317
        $attributes = $this->inputAttributes;
99
100 317
        $this->prepareIdInInputAttributes($attributes);
101
102 317
        $this->prepareInputAttributes($attributes);
103
104 317
        return $attributes;
105
    }
106
107
    protected function prepareInputAttributes(array &$attributes): void
108
    {
109
    }
110
111 317
    final protected function prepareIdInInputAttributes(array &$attributes): void
112
    {
113
        /** @var mixed $idFromTag */
114 317
        $idFromTag = $attributes['id'] ?? null;
115 317
        if ($idFromTag !== null) {
116 2
            $this->inputIdFromTag = (string) $idFromTag;
117
        }
118
119 317
        if ($this->setInputId) {
120 315
            if ($this->inputId !== null) {
121 3
                $attributes['id'] = $this->inputId;
122 312
            } elseif ($idFromTag === null) {
123 311
                $attributes['id'] = $this->getInputId();
124
            }
125
        }
126
    }
127
128 116
    final protected function renderLabel(Label $label): string
129
    {
130 116
        $label = $label->formAttribute($this->getFormModel(), $this->formAttribute);
131
132 116
        if ($this->setInputId === false) {
133 2
            $label = $label->useInputId(false);
134
        }
135
136 116
        if ($this->inputId !== null) {
137 3
            $label = $label->forId($this->inputId);
138 113
        } elseif ($this->inputIdFromTag !== null) {
139 1
            $label = $label->forId($this->inputIdFromTag);
140
        }
141
142 116
        return $label->render();
143
    }
144
145 317
    final protected function renderHint(Hint $hint): string
146
    {
147
        return $hint
148 317
            ->formAttribute($this->getFormModel(), $this->formAttribute)
149 317
            ->render();
150
    }
151
152 317
    final protected function renderError(Error $error): string
153
    {
154
        return $error
155 317
            ->formAttribute($this->getFormModel(), $this->formAttribute)
156 317
            ->render();
157
    }
158
}
159