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

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