Passed
Pull Request — master (#275)
by Sergei
02:49
created

Password::size()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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