Passed
Pull Request — master (#192)
by Alexander
03:40
created

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