Passed
Pull Request — master (#192)
by Alexander
05:43 queued 02:47
created

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