Passed
Pull Request — master (#192)
by Alexander
03:40
created

Textarea   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 215
Duplicated Lines 0 %

Test Coverage

Coverage 18.45%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 215
ccs 12
cts 65
cp 0.1845
rs 10
wmc 19

16 Methods

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