Passed
Push — master ( c202e1...34af4b )
by Alexander
20:59 queued 18:00
created

TextArea::readonly()   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\Widget;
6
7
use InvalidArgumentException;
8
use Yiisoft\Form\Widget\Attribute\InputAttributes;
9
use Yiisoft\Form\Widget\Attribute\PlaceholderInterface;
10
use Yiisoft\Form\Widget\Validator\HasLengthInterface;
11
use Yiisoft\Html\Tag\Textarea as TextAreaTag;
12
13
use function in_array;
14
use function is_string;
15
16
/**
17
 * Generates a textarea tag for the given form attribute.
18
 *
19
 * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html
20
 */
21
final class TextArea extends InputAttributes implements HasLengthInterface, PlaceholderInterface
22
{
23
    /**
24
     * The expected maximum number of characters per line of text for the UA to show.
25
     *
26
     * @param int $value Positive integer.
27
     *
28
     * @return self
29
     *
30
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.cols
31
     */
32 3
    public function cols(int $value): self
33
    {
34 3
        $new = clone $this;
35 3
        $new->attributes['cols'] = $value;
36 3
        return $new;
37
    }
38
39
    /**
40
     * Enables submission of a value for the directionality of the element, and gives the name of the field that
41
     * contains that value.
42
     *
43
     * @param string $value Any string that is not empty.
44
     *
45
     * @return self
46
     *
47
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.dirname
48
     */
49 5
    public function dirname(string $value): self
50
    {
51 5
        if (empty($value)) {
52 2
            throw new InvalidArgumentException('The value cannot be empty.');
53
        }
54
55 3
        $new = clone $this;
56 3
        $new->attributes['dirname'] = $value;
57 3
        return $new;
58
    }
59
60 3
    public function maxlength(int $value): self
61
    {
62 3
        $new = clone $this;
63 3
        $new->attributes['maxlength'] = $value;
64 3
        return $new;
65
    }
66
67 3
    public function minlength(int $value): self
68
    {
69 3
        $new = clone $this;
70 3
        $new->attributes['minlength'] = $value;
71 3
        return $new;
72
    }
73
74
    /**
75
     * It allows defining placeholder.
76
     *
77
     * @param string $value
78
     *
79
     * @return self
80
     *
81
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.placeholder
82
     */
83 2
    public function placeholder(string $value): self
84
    {
85 2
        $new = clone $this;
86 2
        $new->attributes['placeholder'] = $value;
87 2
        return $new;
88
    }
89
90
    /**
91
     * A Boolean attribute which, if present, means this field cannot be edited by the user.
92
     * Its value can, however, still be changed by JavaScript code directly setting the HTMLInputElement.value
93
     * property.
94
     *
95
     * @param bool $value
96
     *
97
     * @return static
98
     *
99
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.readonly
100
     */
101 2
    public function readonly(bool $value = true): static
102
    {
103 2
        $new = clone $this;
104 2
        $new->attributes['readonly'] = $value;
105 2
        return $new;
106
    }
107
108
    /**
109
     * The number of lines of text for the UA to show.
110
     *
111
     * @param int $value
112
     *
113
     * @return self
114
     *
115
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.rows
116
     */
117 3
    public function rows(int $value): self
118
    {
119 3
        $new = clone $this;
120 3
        $new->attributes['rows'] = $value;
121 3
        return $new;
122
    }
123
124
    /**
125
     * @param string $value Contains the hard and soft values.
126
     * `hard` Instructs the UA to insert line breaks into the submitted value of the textarea such that each line has no
127
     *  more characters than the value specified by the cols attribute.
128
     * `soft` Instructs the UA to add no line breaks to the submitted value of the textarea.
129
     *
130
     * @return self
131
     *
132
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.wrap.hard
133
     * @link https://www.w3.org/TR/2012/WD-html-markup-20120329/textarea.html#textarea.attrs.wrap.soft
134
     */
135 5
    public function wrap(string $value = 'hard'): self
136
    {
137 5
        if (!in_array($value, ['hard', 'soft'])) {
138 2
            throw new InvalidArgumentException('Invalid wrap value. Valid values are: hard, soft.');
139
        }
140
141 3
        $new = clone $this;
142 3
        $new->attributes['wrap'] = $value;
143 3
        return $new;
144
    }
145
146
    /**
147
     * @return string the generated textarea tag.
148
     */
149 45
    protected function run(): string
150
    {
151 45
        $attributes = $this->build($this->attributes);
152
153
        /** @link https://html.spec.whatwg.org/multipage/input.html#attr-input-value */
154 45
        $value = $attributes['value'] ?? $this->getAttributeValue();
155 45
        unset($attributes['value']);
156
157 45
        if (!is_string($value) && null !== $value) {
158 2
            throw new InvalidArgumentException('TextArea widget must be a string or null value.');
159
        }
160
161 43
        return TextAreaTag::tag()->attributes($attributes)->content((string)$value)->render();
162
    }
163
}
164