Passed
Push — dependabot/github_actions/shiv... ( a0bd3e )
by
unknown
10:33
created

TextArea::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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
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\Widget;
6
7
use Yiisoft\Arrays\ArrayHelper;
8
use Yiisoft\Form\FormModelInterface;
9
use Yiisoft\Form\Helper\HtmlForm;
10
use Yiisoft\Html\Html;
11
use Yiisoft\Widget\Widget;
12
13
final class TextArea extends Widget
14
{
15
    private ?string $id = null;
16
    private FormModelInterface $data;
17
    private string $attribute;
18
    private array $options = [];
19
    private string $charset = 'UTF-8';
20
    private bool $noPlaceholder = false;
21
22
    /**
23
     * Generates a textarea tag for the given form attribute.
24
     *
25
     * @return string the generated textarea tag.
26
     */
27 17
    public function run(): string
28
    {
29 17
        $new = clone $this;
30
31 17
        if ($new->noPlaceholder === false) {
32 16
            $new->setPlaceholder();
33
        }
34
35 17
        if (!empty($new->getId())) {
36 17
            $new->options['id'] = $new->getId();
37
        }
38
39 17
        $encode = ArrayHelper::remove($new->options, 'encode', true);
40
41 17
        return Html::textarea($new->getName(), $new->getValue())
0 ignored issues
show
Bug introduced by
It seems like $new->getValue() can also be of type array; however, parameter $value of Yiisoft\Html\Html::textarea() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
        return Html::textarea($new->getName(), /** @scrutinizer ignore-type */ $new->getValue())
Loading history...
42 17
            ->attributes($new->options)
43 17
            ->encode($encode)
44 17
            ->render();
45
    }
46
47
    /**
48
     * Set form model, name and options for the widget.
49
     *
50
     * @param FormModelInterface $data Form model.
51
     * @param string $attribute Form model property this widget is rendered for.
52
     * @param array $options The HTML attributes for the widget container tag.
53
     * See {@see \Yiisoft\Html\Html::renderTagAttributes()} for details on how attributes are being rendered.
54
     *
55
     * @return self
56
     */
57 17
    public function config(FormModelInterface $data, string $attribute, array $options = []): self
58
    {
59 17
        $new = clone $this;
60 17
        $new->data = $data;
61 17
        $new->attribute = $attribute;
62 17
        $new->options = $options;
63 17
        return $new;
64
    }
65
66
    /**
67
     * Focus on the control (put cursor into it) when the page loads.
68
     * Only one form element could be in focus at the same time.
69
     *
70
     * @param bool $value
71
     *
72
     * @return self
73
     */
74 1
    public function autofocus(bool $value = true): self
75
    {
76 1
        $new = clone $this;
77 1
        $new->options['autofocus'] = $value;
78 1
        return $new;
79
    }
80
81
    /**
82
     * Set the character set used to generate the widget id. See {@see HtmlForm::getInputId()}.
83
     *
84
     * @param string $value
85
     *
86
     * @return self
87
     */
88
    public function charset(string $value): self
89
    {
90
        $new = clone $this;
91
        $new->charset = $value;
92
        return $new;
93
    }
94
95
    /**
96
     * Set whether the element is disabled or not.
97
     *
98
     * If this attribute is set to `true`, the element is disabled. Disabled elements are usually drawn with grayed-out
99
     * text.
100
     * If the element is disabled, it does not respond to user actions, it cannot be focused, and the command event
101
     * will not fire. In the case of form elements, it will not be submitted. Do not set the attribute to true, as
102
     * this will suggest you can set it to false to enable the element again, which is not the case.
103
     *
104
     * @param bool $value
105
     *
106
     * @return self
107
     */
108 1
    public function disabled(bool $value = true): self
109
    {
110 1
        $new = clone $this;
111 1
        $new->options['disabled'] = $value;
112 1
        return $new;
113
    }
114
115
    /**
116
     * Specifies the form element the tag input element belongs to. The value of this attribute must be the id
117
     * attribute of a {@see Form} element in the same document.
118
     *
119
     * @param string $value
120
     *
121
     * @return self
122
     */
123 1
    public function form(string $value): self
124
    {
125 1
        $new = clone $this;
126 1
        $new->options['form'] = $value;
127 1
        return $new;
128
    }
129
130
    /**
131
     * The minimum number of characters (as UTF-16 code units) the user can enter into the text input.
132
     *
133
     * This must be an non-negative integer value smaller than or equal to the value specified by maxlength.
134
     * If no minlength is specified, or an invalid value is specified, the text input has no minimum length.
135
     *
136
     * @param int $value
137
     *
138
     * @return self
139
     */
140 1
    public function minlength(int $value): self
141
    {
142 1
        $new = clone $this;
143 1
        $new->options['minlength'] = $value;
144 1
        return $new;
145
    }
146
147
    /**
148
     * The maxlength attribute defines the maximum number of characters (as UTF-16 code units) the user can enter into
149
     * an tag input.
150
     *
151
     * If no maxlength is specified, or an invalid value is specified, the tag input has no maximum length.
152
     *
153
     * @param int $value
154
     *
155
     * @return self
156
     */
157 1
    public function maxlength(int $value): self
158
    {
159 1
        $new = clone $this;
160 1
        $new->options['maxlength'] = $value;
161 1
        return $new;
162
    }
163
164
    /**
165
     * Allows you to disable placeholder.
166
     *
167
     * @param bool $value
168
     *
169
     * @return self
170
     */
171 1
    public function noPlaceholder(bool $value = true): self
172
    {
173 1
        $new = clone $this;
174 1
        $new->noPlaceholder = $value;
175 1
        return $new;
176
    }
177
178
    /**
179
     * It allows defining placeholder.
180
     *
181
     * @param string $value
182
     *
183
     * @return self
184
     */
185 1
    public function placeholder(string $value): self
186
    {
187 1
        $new = clone $this;
188 1
        $new->options['placeholder'] = $value;
189 1
        return $new;
190
    }
191
192
    /**
193
     * A Boolean attribute which, if present, means this field cannot be edited by the user.
194
     * Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement.value
195
     * property.
196
     *
197
     * @param bool $value
198
     *
199
     * @return self
200
     */
201 1
    public function readOnly(bool $value = true): self
202
    {
203 1
        $new = clone $this;
204 1
        $new->options['readonly'] = $value;
205 1
        return $new;
206
    }
207
208
    /**
209
     * Spellcheck is a global attribute which is used to indicate whether or not to enable spell checking for an
210
     * element.
211
     *
212
     * @param bool $value
213
     *
214
     * @return self
215
     */
216 1
    public function spellcheck(bool $value = true): self
217
    {
218 1
        $new = clone $this;
219 1
        $new->options['spellcheck'] = $value;
220 1
        return $new;
221
    }
222
223
    /**
224
     * If it is required to fill in a value in order to submit the form.
225
     *
226
     * @param bool $value
227
     *
228
     * @return self
229
     */
230 1
    public function required(bool $value = true): self
231
    {
232 1
        $new = clone $this;
233 1
        $new->options['required'] = $value;
234 1
        return $new;
235
    }
236
237
    /**
238
     * The tabindex global attribute indicates that its element can be focused, and where it participates in sequential
239
     * keyboard navigation (usually with the Tab key, hence the name).
240
     *
241
     * It accepts an integer as a value, with different results depending on the integer's value:
242
     *
243
     * - A negative value (usually tabindex="-1") means that the element is not reachable via sequential keyboard
244
     * navigation, but could be focused with Javascript or visually. It's mostly useful to create accessible widgets
245
     * with JavaScript.
246
     * - tabindex="0" means that the element should be focusable in sequential keyboard navigation, but its order is
247
     * defined by the document's source order.
248
     * - A positive value means the element should be focusable in sequential keyboard navigation, with its order
249
     * defined by the value of the number. That is, tabindex="4" is focused before tabindex="5", but after tabindex="3".
250
     *
251
     * @param int $value
252
     *
253
     * @return self
254
     */
255 1
    public function tabIndex(int $value = 0): self
256
    {
257 1
        $new = clone $this;
258 1
        $new->options['tabindex'] = $value;
259 1
        return $new;
260
    }
261
262
    /**
263
     * The title global attribute contains text representing advisory information related to the element it belongs to.
264
     *
265
     * @param string $value
266
     *
267
     * @return self
268
     */
269 1
    public function title(string $value): self
270
    {
271 1
        $new = clone $this;
272 1
        $new->options['title'] = $value;
273 1
        return $new;
274
    }
275
276 17
    private function getId(): string
277
    {
278 17
        $id = $this->options['id'] ?? $this->id;
279
280 17
        if ($id === null) {
281 17
            $id = HtmlForm::getInputId($this->data, $this->attribute, $this->charset);
282
        }
283
284 17
        return $id !== false ? (string) $id : '';
285
    }
286
287 17
    private function getName(): string
288
    {
289 17
        return ArrayHelper::remove($this->options, 'name', HtmlForm::getInputName($this->data, $this->attribute));
290
    }
291
292 17
    private function getValue()
293
    {
294 17
        $value = HtmlForm::getAttributeValue($this->data, $this->attribute);
295 17
        if ($value !== null && is_scalar($value)) {
296 1
            $value = (string)$value;
297
        }
298
299 17
        return ArrayHelper::remove(
300 17
            $this->options,
301 17
            'value',
302 17
            $value
303
        );
304
    }
305
306 16
    private function setPlaceholder(): void
307
    {
308 16
        if (!isset($this->options['placeholder'])) {
309 15
            $attributeName = HtmlForm::getAttributeName($this->attribute);
310 15
            $this->options['placeholder'] = $this->data->getAttributeLabel($attributeName);
311
        }
312 16
    }
313
}
314