Passed
Pull Request — master (#192)
by Alexander
05:43 queued 02:47
created

InputField::renderError()   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
12
abstract class InputField extends PartsField
13
{
14
    use FormAttributeTrait;
15
16
    protected ?string $inputId = null;
17
    protected ?string $inputIdFromTag = null;
18
    protected bool $setInputIdAttribute = true;
19
20
    protected array $inputTagAttributes = [];
21
22
    /**
23
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the ID
24
     * attribute of a form element in the same document.
25
     *
26
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fae-form
27
     */
28 3
    final public function form(?string $value): static
29
    {
30 3
        $new = clone $this;
31 3
        $new->inputTagAttributes['form'] = $value;
32 3
        return $new;
33
    }
34
35 4
    final public function inputId(?string $inputId): static
36
    {
37 4
        $new = clone $this;
38 4
        $new->inputId = $inputId;
39 4
        return $new;
40
    }
41
42 3
    final public function setInputIdAttribute(bool $value): static
43
    {
44 3
        $new = clone $this;
45 3
        $new->setInputIdAttribute = $value;
46 3
        return $new;
47
    }
48
49 7
    final public function inputTagAttributes(array $attributes): static
50
    {
51 7
        $new = clone $this;
52 7
        $new->inputTagAttributes = $attributes;
53 7
        return $new;
54
    }
55
56 279
    final protected function getInputName(): string
57
    {
58 279
        return HtmlForm::getInputName($this->getFormModel(), $this->attribute);
59
    }
60
61 251
    final protected function getInputTagAttributes(): array
62
    {
63 251
        $attributes = $this->inputTagAttributes;
64
65 251
        $this->prepareIdInInputTagAttributes($attributes);
66
67 251
        $this->prepareInputTagAttributes($attributes);
68
69 251
        return $attributes;
70
    }
71
72
    protected function prepareInputTagAttributes(array &$attributes): void
73
    {
74
    }
75
76 251
    final protected function prepareIdInInputTagAttributes(array &$attributes): void
77
    {
78
        /** @var mixed $idFromTag */
79 251
        $idFromTag = $attributes['id'] ?? null;
80 251
        if ($idFromTag !== null) {
81 2
            $this->inputIdFromTag = (string) $idFromTag;
82
        }
83
84 251
        if ($this->setInputIdAttribute) {
85 249
            if ($this->inputId !== null) {
86 3
                $attributes['id'] = $this->inputId;
87 246
            } elseif ($idFromTag === null) {
88 245
                $attributes['id'] = $this->getInputId();
89
            }
90
        }
91
    }
92
93 96
    final protected function renderLabel(Label $label): string
94
    {
95 96
        $label = $label->attribute($this->getFormModel(), $this->attribute);
96
97 96
        if ($this->setInputIdAttribute === false) {
98 2
            $label = $label->useInputIdAttribute(false);
99
        }
100
101 96
        if ($this->inputId !== null) {
102 3
            $label = $label->forId($this->inputId);
103 93
        } elseif ($this->inputIdFromTag !== null) {
104 1
            $label = $label->forId($this->inputIdFromTag);
105
        }
106
107 96
        return $label->render();
108
    }
109
110 278
    final protected function renderHint(Hint $hint): string
111
    {
112
        return $hint
113 278
            ->attribute($this->getFormModel(), $this->attribute)
114 278
            ->render();
115
    }
116
117 278
    final protected function renderError(Error $error): string
118
    {
119
        return $error
120 278
            ->attribute($this->getFormModel(), $this->attribute)
121 278
            ->render();
122
    }
123
}
124