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

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