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

Email   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 203
Duplicated Lines 0 %

Test Coverage

Coverage 19.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 49
c 2
b 0
f 0
dl 0
loc 203
ccs 12
cts 61
cp 0.1967
rs 10
wmc 18

15 Methods

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