Passed
Pull Request — master (#192)
by Sergei
03:07
created

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