Passed
Push — master ( 34f20f...a12a6d )
by Enjoys
57s queued 12s
created

Textarea::getValidatedAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Elements;
6
7
use Closure;
8
use Enjoys\Forms\AttributeFactory;
9
use Enjoys\Forms\Element;
10
use Enjoys\Forms\Interfaces\Descriptionable;
11
use Enjoys\Forms\Interfaces\Ruleable;
12
use Enjoys\Forms\Traits\Description;
13
use Enjoys\Forms\Traits\Rules;
14
use Webmozart\Assert\Assert;
15
16
class Textarea extends Element implements Ruleable, Descriptionable
17
{
18
    use Description;
19
    use Rules;
20
21
    protected string $type = 'textarea';
22
23
24
    private string $value;
25
26 16
    public function __construct(string $name, string $label = null)
27
    {
28 16
        parent::__construct($name, $label);
29 16
        $this->value = '';
30
    }
31
32 3
    public function setValue(?string $value): Textarea
33
    {
34 3
        $this->value = $value ?? '';
35 3
        return $this;
36
    }
37
38 8
    public function getValue(): string
39
    {
40 8
        return $this->value;
41
    }
42
43
44
    /**
45
     * Высота поля в строках текста.
46
     * @param int|Closure():int $rows
47
     * @return Textarea
48
     * @psalm-suppress RedundantConditionGivenDocblockType
49
     */
50 1
    public function setRows(int|Closure $rows): Textarea
51
    {
52 1
        if ($rows instanceof Closure) {
53 1
            $rows = $rows();
54 1
            Assert::integer($rows);
55
        }
56 1
        $this->setAttribute(AttributeFactory::create('rows', $rows));
57 1
        return $this;
58
    }
59
60
    /**
61
     * Ширина поля в символах.
62
     * @param int|Closure():int $cols
63
     * @return Textarea
64
     * @psalm-suppress RedundantConditionGivenDocblockType
65
     */
66 2
    public function setCols(int|Closure $cols): Textarea
67
    {
68 2
        if ($cols instanceof Closure) {
69 2
            $cols = $cols();
70 2
            Assert::integer($cols);
71
        }
72 1
        $this->setAttribute(AttributeFactory::create('cols', $cols));
73 1
        return $this;
74
    }
75
76 7
    public function baseHtml(): string
77
    {
78 7
        $value = $this->getAttribute('value');
79
80 7
        if ($value !== null) {
81 1
            $this->setValue($value->getValueString());
82 1
            $this->getAttributeCollection()->remove('value');
83
        }
84 7
        return "<textarea{$this->getAttributesString()}>{$this->getValue()}</textarea>";
85
    }
86
}
87