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

InputField   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 45
c 1
b 0
f 0
dl 0
loc 110
ccs 47
cts 47
cp 1
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 renderError() 0 5 1
A getInputName() 0 3 1
A prepareInputTagAttributes() 0 2 1
A form() 0 5 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 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