Text::readonly()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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