Passed
Pull Request — master (#192)
by Alexander
02:53
created

Textarea   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 27.78%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 60
ccs 5
cts 18
cp 0.2778
rs 10
c 1
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A wrap() 0 5 1
A rows() 0 5 1
A generateInput() 0 12 3
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\AbstractField;
9
use Yiisoft\Form\Field\Base\DirnameTrait;
10
use Yiisoft\Form\Field\Base\MinMaxLengthTrait;
11
use Yiisoft\Form\Field\Base\PlaceholderTrait;
12
use Yiisoft\Form\Field\Base\ReadonlyTrait;
13
use Yiisoft\Form\Field\Base\RequiredTrait;
14
use Yiisoft\Html\Html;
15
16
use function is_string;
17
18
final class Textarea extends AbstractField
19
{
20
    use DirnameTrait;
21
    use MinMaxLengthTrait;
22
    use PlaceholderTrait;
23
    use ReadonlyTrait;
24
    use RequiredTrait;
25
26
    /**
27
     * The expected maximum number of characters per line of text to show.
28
     *
29
     * @link https://html.spec.whatwg.org/multipage/form-elements.html#attr-textarea-cols
30
     */
31
    public function cols(?int $value): self
32
    {
33
        $new = clone $this;
34
        $new->inputTagAttributes['cols'] = $value;
35
        return $new;
36
    }
37
38
    /**
39
     * The number of lines of text to show.
40
     *
41
     * @link https://html.spec.whatwg.org/multipage/form-elements.html#attr-textarea-rows
42
     */
43
    public function rows(?int $value): self
44
    {
45
        $new = clone $this;
46
        $new->inputTagAttributes['rows'] = $value;
47
        return $new;
48
    }
49
50
    /**
51
     * Define how the value of the form control is to be wrapped for form submission:
52
     *  - `hard` indicates that the text in the `textarea` is to have newlines added by the user agent so that the text
53
     *    is wrapped when it is submitted.
54
     *  - `soft` indicates that the text in the `textarea` is not to be wrapped when it is submitted (though it can
55
     *    still be wrapped in the rendering).
56
     *
57
     * @link https://html.spec.whatwg.org/multipage/form-elements.html#attr-textarea-wrap
58
     */
59
    public function wrap(?string $value): self
60
    {
61
        $new = clone $this;
62
        $new->inputTagAttributes['wrap'] = $value;
63
        return $new;
64
    }
65
66 1
    protected function generateInput(): string
67
    {
68 1
        $value = $this->getAttributeValue();
69
70 1
        if (!is_string($value) && $value !== null) {
71
            throw new InvalidArgumentException('Textarea widget must be a string or null value.');
72
        }
73
74 1
        $tagAttributes = $this->getInputTagAttributes();
75
76
        /** @psalm-suppress MixedArgumentTypeCoercion */
77 1
        return Html::textarea($this->getInputName(), $value, $tagAttributes)->render();
78
    }
79
}
80