Passed
Push — attributes ( 57ed6a...578d64 )
by Peter
08:20
created

Textarea::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Form\Element;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Html\Attribute;
9
use AbterPhp\Framework\Html\Attributes;
10
use AbterPhp\Framework\Html\Helper\TagHelper;
11
use AbterPhp\Framework\Html\Tag;
12
use InvalidArgumentException;
13
14
class Textarea extends Tag implements IElement
15
{
16
    public const CLASS_WYSIWYG = 'wysiwyg';
17
18
    protected const DEFAULT_TAG = Html5::TAG_TEXTAREA;
19
20
    protected const DEFAULT_ROW = '3';
21
22
    /**
23
     * Textarea constructor.
24
     *
25
     * @param string          $inputId
26
     * @param string          $name
27
     * @param string          $value
28
     * @param string[]        $intents
29
     * @param Attributes|null $attributes
30
     * @param string|null     $tag
31
     */
32
    public function __construct(
33
        string $inputId,
34
        string $name,
35
        string $value = '',
36
        array $intents = [],
37
        ?Attributes $attributes = null,
38
        ?string $tag = null
39
    ) {
40
        $attributes = $attributes ?? new Attributes();
41
        if ($inputId) {
42
            $attributes->replaceItem(new Attribute(Html5::ATTR_ID, $inputId));
43
        }
44
        if (!$attributes->hasItem(Html5::ATTR_ROWS)) {
45
            $attributes->replaceItem(new Attribute(Html5::ATTR_ROWS, static::DEFAULT_ROW));
46
        }
47
48
        $attributes->replaceItem(new Attribute(Html5::ATTR_NAME, $name));
49
50
        parent::__construct(null, $intents, $attributes, $tag);
51
52
        $this->setValue($value);
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getName(): string
59
    {
60
        if (!$this->getAttributes()->hasItem(Html5::ATTR_NAME)) {
61
            return '';
62
        }
63
64
        $values = $this->getAttributes()->getItem(Html5::ATTR_NAME)->getValues();
65
        if (null === $values) {
66
            return '';
67
        }
68
69
        return implode(' ', $values);
70
    }
71
72
    /**
73
     * @param string|string[] $value
74
     *
75
     * @return $this
76
     */
77
    public function setValue($value): IElement
78
    {
79
        if (!is_string($value)) {
80
            throw new InvalidArgumentException();
81
        }
82
83
        $this->forceGetAttribute(Html5::ATTR_VALUE)->set(htmlspecialchars($value));
84
85
        return $this;
86
    }
87
88
    /**
89
     * @return string|string[]
90
     */
91
    public function getValue()
92
    {
93
        return $this->getAttribute(Html5::ATTR_VALUE)->getValue();
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function __toString(): string
100
    {
101
        $value = $this->getAttributes()->getItem(Html5::ATTR_VALUE)->getValue();
102
103
        $attributes = clone $this->getAttributes();
104
        $attributes->remove(Html5::ATTR_VALUE);
105
106
        return TagHelper::toString($this->tag, $value, $attributes);
0 ignored issues
show
Bug introduced by
It seems like $this->tag can also be of type null; however, parameter $tag of AbterPhp\Framework\Html\...r\TagHelper::toString() does only seem to accept 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

106
        return TagHelper::toString(/** @scrutinizer ignore-type */ $this->tag, $value, $attributes);
Loading history...
107
    }
108
}
109