Passed
Push — master ( d2333f...209898 )
by Sergei
07:55 queued 05:10
created

InputField::inputAttributes()   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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
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 6
    final public function inputAttributes(array $attributes): static
51
    {
52 6
        $new = clone $this;
53 6
        $new->inputAttributes = $attributes;
54 6
        return $new;
55
    }
56
57 2
    final public function addInputAttributes(array $attributes): static
58
    {
59 2
        $new = clone $this;
60 2
        $new->inputAttributes = array_merge($new->inputAttributes, $attributes);
61 2
        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 9
    final public function inputClass(?string ...$class): static
70
    {
71 9
        $new = clone $this;
72 9
        $new->inputAttributes['class'] = array_filter($class, static fn ($c) => $c !== null);
73 9
        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(
85 9
            $new->inputAttributes,
86 9
            array_filter($class, static fn ($c) => $c !== null),
87
        );
88 9
        return $new;
89
    }
90
91 337
    final protected function getInputName(): string
92
    {
93 337
        return HtmlForm::getInputName($this->getFormModel(), $this->formAttribute);
94
    }
95
96 336
    final protected function getInputAttributes(): array
97
    {
98 336
        $attributes = $this->inputAttributes;
99
100 336
        $this->prepareIdInInputAttributes($attributes);
101
102 336
        $this->prepareInputAttributes($attributes);
103
104 336
        return $attributes;
105
    }
106
107 18
    protected function prepareInputAttributes(array &$attributes): void
108
    {
109 18
    }
110
111 336
    final protected function prepareIdInInputAttributes(array &$attributes): void
112
    {
113
        /** @var mixed $idFromTag */
114 336
        $idFromTag = $attributes['id'] ?? null;
115 336
        if ($idFromTag !== null) {
116 2
            $this->inputIdFromTag = (string) $idFromTag;
117
        }
118
119 336
        if ($this->setInputId) {
120 334
            if ($this->inputId !== null) {
121 3
                $attributes['id'] = $this->inputId;
122 331
            } elseif ($idFromTag === null) {
123 330
                $attributes['id'] = $this->getInputId();
124
            }
125
        }
126
    }
127
128 123
    final protected function renderLabel(Label $label): string
129
    {
130 123
        $label = $label->formAttribute($this->getFormModel(), $this->formAttribute);
131
132 123
        if ($this->setInputId === false) {
133 2
            $label = $label->useInputId(false);
134
        }
135
136 123
        if ($this->inputId !== null) {
137 3
            $label = $label->forId($this->inputId);
138 120
        } elseif ($this->inputIdFromTag !== null) {
139 1
            $label = $label->forId($this->inputIdFromTag);
140
        }
141
142 123
        return $label->render();
143
    }
144
145 336
    final protected function renderHint(Hint $hint): string
146
    {
147
        return $hint
148 336
            ->formAttribute($this->getFormModel(), $this->formAttribute)
149 336
            ->render();
150
    }
151
152 336
    final protected function renderError(Error $error): string
153
    {
154
        return $error
155 336
            ->formAttribute($this->getFormModel(), $this->formAttribute)
156 336
            ->render();
157
    }
158
}
159