Passed
Pull Request — master (#192)
by Alexander
06:02 queued 02:54
created

Textarea   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 13.16%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 127
ccs 5
cts 38
cp 0.1316
rs 10
c 1
b 0
f 0
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A minlength() 0 5 1
A readonly() 0 5 1
A dirname() 0 5 1
A wrap() 0 5 1
A required() 0 5 1
A rows() 0 5 1
A generateInput() 0 11 3
A maxlength() 0 5 1
A cols() 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\AbstractInputField;
9
use Yiisoft\Form\Field\Base\PlaceholderTrait;
10
use Yiisoft\Html\Html;
11
12
use function is_string;
13
14
final class Textarea extends AbstractInputField
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
     * The expected maximum number of characters per line of text to show.
92
     *
93
     * @link https://html.spec.whatwg.org/multipage/form-elements.html#attr-textarea-cols
94
     */
95
    public function cols(?int $value): self
96
    {
97
        $new = clone $this;
98
        $new->inputTagAttributes['cols'] = $value;
99
        return $new;
100
    }
101
102
    /**
103
     * The number of lines of text to show.
104
     *
105
     * @link https://html.spec.whatwg.org/multipage/form-elements.html#attr-textarea-rows
106
     */
107
    public function rows(?int $value): self
108
    {
109
        $new = clone $this;
110
        $new->inputTagAttributes['rows'] = $value;
111
        return $new;
112
    }
113
114
    /**
115
     * Define how the value of the form control is to be wrapped for form submission:
116
     *  - `hard` indicates that the text in the `textarea` is to have newlines added by the user agent so that the text
117
     *    is wrapped when it is submitted.
118
     *  - `soft` indicates that the text in the `textarea` is not to be wrapped when it is submitted (though it can
119
     *    still be wrapped in the rendering).
120
     *
121
     * @link https://html.spec.whatwg.org/multipage/form-elements.html#attr-textarea-wrap
122
     */
123
    public function wrap(?string $value): self
124
    {
125
        $new = clone $this;
126
        $new->inputTagAttributes['wrap'] = $value;
127
        return $new;
128
    }
129
130 1
    protected function generateInput(): string
131
    {
132 1
        $value = $this->getAttributeValue();
133
134 1
        if (!is_string($value) && $value !== null) {
135
            throw new InvalidArgumentException('Textarea widget must be a string or null value.');
136
        }
137
138 1
        $tagAttributes = $this->getInputTagAttributes();
139
140 1
        return Html::textarea($this->getInputName(), $value, $tagAttributes)->render();
141
    }
142
}
143