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

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