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

Input::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
13
class Input extends Tag implements IElement
14
{
15
    public const TYPE_BUTTON         = 'button';
16
    public const TYPE_CHECKBOX       = 'checkbox';
17
    public const TYPE_COLOR          = 'color';
18
    public const TYPE_DATE           = 'date';
19
    public const TYPE_DATETIME       = 'datetime';
20
    public const TYPE_DATETIME_LOCAL = 'datetime-local';
21
    public const TYPE_EMAIL          = 'email';
22
    public const TYPE_FILE           = 'file';
23
    public const TYPE_HIDDEN         = 'hidden';
24
    public const TYPE_IMAGE          = 'image';
25
    public const TYPE_MONTH          = 'month';
26
    public const TYPE_NUMBER         = 'number';
27
    public const TYPE_PASSWORD       = 'password';
28
    public const TYPE_RADIO          = 'radio';
29
    public const TYPE_RANGE          = 'range';
30
    public const TYPE_RESET          = 'reset';
31
    public const TYPE_SEARCH         = 'search';
32
    public const TYPE_SUBMIT         = 'submit';
33
    public const TYPE_TEL            = 'tel';
34
    public const TYPE_TEXT           = 'text';
35
    public const TYPE_URL            = 'url';
36
    public const TYPE_WEEK           = 'week';
37
38
    public const NAME_HTTP_METHOD = '_method';
39
40
    public const AUTOCOMPLETE_OFF = 'off';
41
42
    protected const DEFAULT_TAG = Html5::TAG_INPUT;
43
44
    protected const DEFAULT_TYPE = self::TYPE_TEXT;
45
46
    /**
47
     * Input constructor.
48
     *
49
     * @param string          $inputId
50
     * @param string          $name
51
     * @param string          $value
52
     * @param string[]        $intents
53
     * @param Attributes|null $attributes
54
     * @param string|null     $tag
55
     */
56
    public function __construct(
57
        string $inputId,
58
        string $name,
59
        string $value = '',
60
        array $intents = [],
61
        ?Attributes $attributes = null,
62
        ?string $tag = null
63
    ) {
64
        $attributes = $attributes ?? new Attributes();
65
        if ($inputId) {
66
            $attributes->replaceItem(new Attribute(Html5::ATTR_ID, $inputId));
67
        }
68
        if (!$attributes->hasItem(Html5::ATTR_TYPE)) {
69
            $attributes->replaceItem(new Attribute(Html5::ATTR_TYPE, static::DEFAULT_TYPE));
70
        }
71
72
        $attributes->replaceItem(new Attribute(Html5::ATTR_NAME, $name));
73
74
        parent::__construct(null, $intents, $attributes, $tag);
75
76
        $this->setValue($value);
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getName(): string
83
    {
84
        return $this->forceGetAttribute(Html5::ATTR_NAME)->getValue();
85
    }
86
87
    /**
88
     * @param string|string[] $value
89
     *
90
     * @return $this
91
     */
92
    public function setValue($value): IElement
93
    {
94
        if (!is_string($value)) {
95
            throw new \InvalidArgumentException();
96
        }
97
98
        $this->forceGetAttribute(Html5::ATTR_VALUE)->set(htmlspecialchars($value));
99
100
        return $this;
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getValue()
107
    {
108
        return $this->getAttribute(Html5::ATTR_VALUE)->getValue();
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function __toString(): string
115
    {
116
        return TagHelper::toString($this->tag, '', $this->getAttributes());
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

116
        return TagHelper::toString(/** @scrutinizer ignore-type */ $this->tag, '', $this->getAttributes());
Loading history...
117
    }
118
}
119