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

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