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

InputField   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 91.49%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
c 1
b 0
f 0
dl 0
loc 110
ccs 43
cts 47
cp 0.9149
rs 10
wmc 18

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getInputTagAttributes() 0 9 1
A inputId() 0 5 1
A renderHint() 0 5 1
A inputTagAttributes() 0 5 1
A prepareIdInInputTagAttributes() 0 13 5
A renderLabel() 0 15 4
A setInputIdAttribute() 0 5 1
A form() 0 5 1
A renderError() 0 5 1
A getInputName() 0 3 1
A prepareInputTagAttributes() 0 2 1
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
    final public function form(string $value): static
29
    {
30
        $new = clone $this;
31
        $new->inputTagAttributes['form'] = $value;
32
        return $new;
33
    }
34
35 3
    final public function inputId(?string $inputId): static
36
    {
37 3
        $new = clone $this;
38 3
        $new->inputId = $inputId;
39 3
        return $new;
40
    }
41
42 2
    final public function setInputIdAttribute(bool $value): static
43
    {
44 2
        $new = clone $this;
45 2
        $new->setInputIdAttribute = $value;
46 2
        return $new;
47
    }
48
49 6
    final public function inputTagAttributes(array $attributes): static
50
    {
51 6
        $new = clone $this;
52 6
        $new->inputTagAttributes = $attributes;
53 6
        return $new;
54
    }
55
56 175
    final protected function getInputName(): string
57
    {
58 175
        return HtmlForm::getInputName($this->getFormModel(), $this->attribute);
59
    }
60
61 148
    final protected function getInputTagAttributes(): array
62
    {
63 148
        $attributes = $this->inputTagAttributes;
64
65 148
        $this->prepareIdInInputTagAttributes($attributes);
66
67 148
        $this->prepareInputTagAttributes($attributes);
68
69 148
        return $attributes;
70
    }
71
72
    protected function prepareInputTagAttributes(array &$attributes): void
73
    {
74
    }
75
76 148
    final protected function prepareIdInInputTagAttributes(array &$attributes): void
77
    {
78
        /** @var mixed $idFromTag */
79 148
        $idFromTag = $attributes['id'] ?? null;
80 148
        if ($idFromTag !== null) {
81 2
            $this->inputIdFromTag = (string) $idFromTag;
82
        }
83
84 148
        if ($this->setInputIdAttribute) {
85 146
            if ($this->inputId !== null) {
86 3
                $attributes['id'] = $this->inputId;
87 143
            } elseif ($idFromTag === null) {
88 142
                $attributes['id'] = $this->getInputId();
89
            }
90
        }
91
    }
92
93 89
    final protected function renderLabel(Label $label): string
94
    {
95 89
        $label = $label->attribute($this->getFormModel(), $this->attribute);
96
97 89
        if ($this->setInputIdAttribute === false) {
98 2
            $label = $label->useInputIdAttribute(false);
99
        }
100
101 89
        if ($this->inputId !== null) {
102 3
            $label = $label->forId($this->inputId);
103 86
        } elseif ($this->inputIdFromTag !== null) {
104 1
            $label = $label->forId($this->inputIdFromTag);
105
        }
106
107 89
        return $label->render();
108
    }
109
110 175
    final protected function renderHint(Hint $hint): string
111
    {
112
        return $hint
113 175
            ->attribute($this->getFormModel(), $this->attribute)
114 175
            ->render();
115
    }
116
117 175
    final protected function renderError(Error $error): string
118
    {
119
        return $error
120 175
            ->attribute($this->getFormModel(), $this->attribute)
121 175
            ->render();
122
    }
123
}
124