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

Telephone::ariaLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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