Passed
Pull Request — master (#275)
by Alexander
03:23
created

InputField::getInputName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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\Base\InputData\InputDataTrait;
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 InputDataTrait;
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 9
        );
88 9
        return $new;
89
    }
90
91 344
    final protected function getInputAttributes(): array
92
    {
93 344
        $attributes = $this->inputAttributes;
94
95 344
        $this->prepareIdInInputAttributes($attributes);
96
97 344
        $this->prepareInputAttributes($attributes);
98
99 344
        return $attributes;
100
    }
101
102 23
    protected function prepareInputAttributes(array &$attributes): void
103
    {
104 23
    }
105
106 344
    final protected function prepareIdInInputAttributes(array &$attributes): void
107
    {
108
        /** @var mixed $idFromTag */
109 344
        $idFromTag = $attributes['id'] ?? null;
110 344
        if ($idFromTag !== null) {
111 2
            $this->inputIdFromTag = (string) $idFromTag;
112
        }
113
114 344
        if ($this->setInputId) {
115 342
            if ($this->inputId !== null) {
116 3
                $attributes['id'] = $this->inputId;
117 339
            } elseif ($idFromTag === null) {
118 338
                $attributes['id'] = $this->getInputData()->getId();
119
            }
120
        }
121
    }
122
123 131
    final protected function renderLabel(Label $label): string
124
    {
125 131
        $label = $label->inputData($this->getInputData());
126
127 131
        if ($this->setInputId === false) {
128 2
            $label = $label->useInputId(false);
129
        }
130
131 131
        if ($this->inputId !== null) {
132 3
            $label = $label->forId($this->inputId);
133 128
        } elseif ($this->inputIdFromTag !== null) {
134 1
            $label = $label->forId($this->inputIdFromTag);
135
        }
136
137 131
        return $label->render();
138
    }
139
140 344
    final protected function renderHint(Hint $hint): string
141
    {
142 344
        return $hint
143 344
            ->inputData($this->getInputData())
144 344
            ->render();
145
    }
146
147 344
    final protected function renderError(Error $error): string
148
    {
149 344
        return $error
150 344
            ->inputData($this->getInputData())
151 344
            ->render();
152
    }
153
}
154