Telephone::prepareInputAttributes()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Form\Field;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\Field\Base\EnrichFromValidationRules\EnrichFromValidationRulesInterface;
9
use Yiisoft\Form\Field\Base\EnrichFromValidationRules\EnrichFromValidationRulesTrait;
10
use Yiisoft\Form\Field\Base\InputField;
11
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderInterface;
12
use Yiisoft\Form\Field\Base\Placeholder\PlaceholderTrait;
13
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassInterface;
14
use Yiisoft\Form\Field\Base\ValidationClass\ValidationClassTrait;
15
use Yiisoft\Form\ThemeContainer;
16
use Yiisoft\Html\Html;
17
18
use function is_string;
19
20
/**
21
 * @link https://html.spec.whatwg.org/multipage/input.html#telephone-state-(type=tel)
22
 * @link https://developer.mozilla.org/docs/Web/HTML/Element/input/tel
23
 */
24
final class Telephone extends InputField implements EnrichFromValidationRulesInterface, PlaceholderInterface, ValidationClassInterface
25
{
26
    use EnrichFromValidationRulesTrait;
27
    use PlaceholderTrait;
28
    use ValidationClassTrait;
29
30
    /**
31
     * Maximum length of value.
32
     *
33
     * @param int|null $value A limit on the number of characters a user can input.
34
     *
35
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-maxlength
36
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-maxlength
37
     */
38 2
    public function maxlength(?int $value): self
39
    {
40 2
        $new = clone $this;
41 2
        $new->inputAttributes['maxlength'] = $value;
42 2
        return $new;
43
    }
44
45
    /**
46
     * Minimum length of value.
47
     *
48
     * @param int|null $value A lower bound on the number of characters a user can input.
49
     *
50
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-minlength
51
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-minlength
52
     */
53 2
    public function minlength(?int $value): self
54
    {
55 2
        $new = clone $this;
56 2
        $new->inputAttributes['minlength'] = $value;
57 2
        return $new;
58
    }
59
60
    /**
61
     * Pattern to be matched by the form control's value.
62
     *
63
     * @param string|null $value A regular expression against which the control's value.
64
     *
65
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-pattern
66
     */
67 2
    public function pattern(?string $value): self
68
    {
69 2
        $new = clone $this;
70 2
        $new->inputAttributes['pattern'] = $value;
71 2
        return $new;
72
    }
73
74
    /**
75
     * A boolean attribute that controls whether or not the user can edit the form control.
76
     *
77
     * @param bool $value Whether to allow the value to be edited by the user.
78
     *
79
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-readonly
80
     */
81 2
    public function readonly(bool $value = true): self
82
    {
83 2
        $new = clone $this;
84 2
        $new->inputAttributes['readonly'] = $value;
85 2
        return $new;
86
    }
87
88
    /**
89
     * A boolean attribute. When specified, the element is required.
90
     *
91
     * @param bool $value Whether the control is required for form submission.
92
     *
93
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-required
94
     */
95 2
    public function required(bool $value = true): self
96
    {
97 2
        $new = clone $this;
98 2
        $new->inputAttributes['required'] = $value;
99 2
        return $new;
100
    }
101
102
    /**
103
     * @link https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-disabled
104
     */
105 2
    public function disabled(bool $disabled = true): self
106
    {
107 2
        $new = clone $this;
108 2
        $new->inputAttributes['disabled'] = $disabled;
109 2
        return $new;
110
    }
111
112
    /**
113
     * Identifies the element (or elements) that describes the object.
114
     *
115
     * @link https://w3c.github.io/aria/#aria-describedby
116
     */
117 2
    public function ariaDescribedBy(?string $value): self
118
    {
119 2
        $new = clone $this;
120 2
        $new->inputAttributes['aria-describedby'] = $value;
121 2
        return $new;
122
    }
123
124
    /**
125
     * Defines a string value that labels the current element.
126
     *
127
     * @link https://w3c.github.io/aria/#aria-label
128
     */
129 2
    public function ariaLabel(?string $value): self
130
    {
131 2
        $new = clone $this;
132 2
        $new->inputAttributes['aria-label'] = $value;
133 2
        return $new;
134
    }
135
136
    /**
137
     * Focus on the control (put cursor into it) when the page loads. Only one form element could be in focus
138
     * at the same time.
139
     *
140
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-fe-autofocus
141
     */
142 2
    public function autofocus(bool $value = true): self
143
    {
144 2
        $new = clone $this;
145 2
        $new->inputAttributes['autofocus'] = $value;
146 2
        return $new;
147
    }
148
149
    /**
150
     * The `tabindex` attribute indicates that its element can be focused, and where it participates in sequential
151
     * keyboard navigation (usually with the Tab key, hence the name).
152
     *
153
     * It accepts an integer as a value, with different results depending on the integer's value:
154
     *
155
     * - A negative value (usually `tabindex="-1"`) means that the element is not reachable via sequential keyboard
156
     *   navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets
157
     *   with JavaScript.
158
     * - `tabindex="0"` means that the element should be focusable in sequential keyboard navigation, but its order is
159
     *   defined by the document's source order.
160
     * - A positive value means the element should be focusable in sequential keyboard navigation, with its order
161
     *   defined by the value of the number. That is, `tabindex="4"` is focused before `tabindex="5"`, but after
162
     *   `tabindex="3"`.
163
     *
164
     * @link https://html.spec.whatwg.org/multipage/interaction.html#attr-tabindex
165
     */
166 2
    public function tabIndex(?int $value): self
167
    {
168 2
        $new = clone $this;
169 2
        $new->inputAttributes['tabindex'] = $value;
170 2
        return $new;
171
    }
172
173
    /**
174
     * The size of the control.
175
     *
176
     * @param int|null $value The number of characters that allow the user to see while editing the element's value.
177
     *
178
     * @link https://html.spec.whatwg.org/multipage/input.html#attr-input-size
179
     */
180 2
    public function size(?int $value): self
181
    {
182 2
        $new = clone $this;
183 2
        $new->inputAttributes['size'] = $value;
184 2
        return $new;
185
    }
186
187 23
    protected function beforeRender(): void
188
    {
189 23
        if ($this->enrichFromValidationRules) {
190 1
            $this->enrichment = ThemeContainer::getEnrichment($this, $this->getInputData());
191
        }
192
    }
193
194 23
    protected function generateInput(): string
195
    {
196 23
        $value = $this->getValue();
197
198 23
        if (!is_string($value) && $value !== null) {
199 1
            throw new InvalidArgumentException('Telephone field requires a string or null value.');
200
        }
201
202
        /** @psalm-suppress MixedArgument We guess that enrichment contain correct values. */
203 22
        $inputAttributes = array_merge(
204 22
            $this->enrichment['inputAttributes'] ?? [],
205 22
            $this->getInputAttributes()
206 22
        );
207
208 22
        return Html::input('tel', $this->getName(), $value, $inputAttributes)->render();
209
    }
210
211 11
    protected function prepareContainerAttributes(array &$attributes): void
212
    {
213 11
        $this->addValidationClassToAttributes(
214 11
            $attributes,
215 11
            $this->getInputData(),
216 11
            $this->hasCustomError() ? true : null,
217 11
        );
218
    }
219
220 22
    protected function prepareInputAttributes(array &$attributes): void
221
    {
222 22
        $this->preparePlaceholderInInputAttributes($attributes);
223 22
        $this->addInputValidationClassToAttributes(
224 22
            $attributes,
225 22
            $this->getInputData(),
226 22
            $this->hasCustomError() ? true : null,
227 22
        );
228
    }
229
}
230