Passed
Pull Request — master (#192)
by Alexander
28:18 queued 25:44
created

InputField::renderHint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
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 3
cts 3
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 $setInputIdAttribute = true;
20
21
    protected array $inputTagAttributes = [];
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->inputTagAttributes['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 setInputIdAttribute(bool $value): static
44
    {
45 3
        $new = clone $this;
46 3
        $new->setInputIdAttribute = $value;
47 3
        return $new;
48
    }
49
50 7
    final public function inputTagAttributes(array $attributes): static
51
    {
52 7
        $new = clone $this;
53 7
        $new->inputTagAttributes = $attributes;
54 7
        return $new;
55
    }
56
57
    /**
58
     * Add one or more CSS classes to the input tag.
59
     *
60
     * @param string|null ...$class One or many CSS classes.
61
     */
62 15
    final public function inputClass(?string ...$class): static
63
    {
64 15
        $new = clone $this;
65 15
        Html::addCssClass(
66 15
            $new->inputTagAttributes,
67 15
            array_filter($class, static fn ($c) => $c !== null),
68
        );
69 15
        return $new;
70
    }
71
72
    /**
73
     * Replace input tag CSS classes with a new set of classes.
74
     *
75
     * @param string|null ...$class One or many CSS classes.
76
     */
77 7
    final public function replaceInputClass(?string ...$class): static
78
    {
79 7
        $new = clone $this;
80 7
        $new->inputTagAttributes['class'] = array_filter($class, static fn ($c) => $c !== null);
81 7
        return $new;
82
    }
83
84 294
    final protected function getInputName(): string
85
    {
86 294
        return HtmlForm::getInputName($this->getFormModel(), $this->attribute);
87
    }
88
89 293
    final protected function getInputTagAttributes(): array
90
    {
91 293
        $attributes = $this->inputTagAttributes;
92
93 293
        $this->prepareIdInInputTagAttributes($attributes);
94
95 293
        $this->prepareInputTagAttributes($attributes);
96
97 293
        return $attributes;
98
    }
99
100
    protected function prepareInputTagAttributes(array &$attributes): void
101
    {
102
    }
103
104 293
    final protected function prepareIdInInputTagAttributes(array &$attributes): void
105
    {
106
        /** @var mixed $idFromTag */
107 293
        $idFromTag = $attributes['id'] ?? null;
108 293
        if ($idFromTag !== null) {
109 2
            $this->inputIdFromTag = (string) $idFromTag;
110
        }
111
112 293
        if ($this->setInputIdAttribute) {
113 291
            if ($this->inputId !== null) {
114 3
                $attributes['id'] = $this->inputId;
115 288
            } elseif ($idFromTag === null) {
116 287
                $attributes['id'] = $this->getInputId();
117
            }
118
        }
119
    }
120
121 110
    final protected function renderLabel(Label $label): string
122
    {
123 110
        $label = $label->attribute($this->getFormModel(), $this->attribute);
124
125 110
        if ($this->setInputIdAttribute === false) {
126 2
            $label = $label->useInputIdAttribute(false);
127
        }
128
129 110
        if ($this->inputId !== null) {
130 3
            $label = $label->forId($this->inputId);
131 107
        } elseif ($this->inputIdFromTag !== null) {
132 1
            $label = $label->forId($this->inputIdFromTag);
133
        }
134
135 110
        return $label->render();
136
    }
137
138 293
    final protected function renderHint(Hint $hint): string
139
    {
140
        return $hint
141 293
            ->attribute($this->getFormModel(), $this->attribute)
142 293
            ->render();
143
    }
144
145 293
    final protected function renderError(Error $error): string
146
    {
147
        return $error
148 293
            ->attribute($this->getFormModel(), $this->attribute)
149 293
            ->render();
150
    }
151
}
152