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\Helper\Attributes; |
10
|
|
|
use AbterPhp\Framework\Html\Helper\Tag as 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
|
|
|
protected const PROTECTED_KEYS = [Html5::ATTR_ID, Html5::ATTR_TYPE, Html5::ATTR_NAME, Html5::ATTR_VALUE]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Input constructor. |
50
|
|
|
* |
51
|
|
|
* @param string $inputId |
52
|
|
|
* @param string $name |
53
|
|
|
* @param string $value |
54
|
|
|
* @param string[] $intents |
55
|
|
|
* @param array<string, Attribute>|null $attributes |
56
|
|
|
* @param string|null $tag |
57
|
|
|
*/ |
58
|
|
|
public function __construct( |
59
|
|
|
string $inputId, |
60
|
|
|
string $name, |
61
|
|
|
string $value = '', |
62
|
|
|
array $intents = [], |
63
|
|
|
?array $attributes = null, |
64
|
|
|
?string $tag = null |
65
|
|
|
) { |
66
|
|
|
$attributes ??= []; |
67
|
|
|
$attributes = Attributes::addItem($attributes, Html5::ATTR_ID, $inputId); |
68
|
|
|
if (!array_key_exists(Html5::ATTR_TYPE, $attributes)) { |
69
|
|
|
$attributes = Attributes::addItem($attributes, Html5::ATTR_TYPE, static::DEFAULT_TYPE); |
70
|
|
|
} |
71
|
|
|
$attributes = Attributes::addItem($attributes, Html5::ATTR_NAME, $name); |
72
|
|
|
|
73
|
|
|
parent::__construct(null, $intents, $attributes, $tag); |
74
|
|
|
|
75
|
|
|
$this->setValue($value); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
|
|
public function getName(): string |
82
|
|
|
{ |
83
|
|
|
$values = $this->attributes[Html5::ATTR_NAME]->getValues(); |
84
|
|
|
if (null === $values) { |
85
|
|
|
return ''; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return implode(' ', $values); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @suppress PhanParamSignatureMismatch |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getValue() |
97
|
|
|
{ |
98
|
|
|
return $this->getAttribute(Html5::ATTR_VALUE)->getValue(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string|string[] $value |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function setValue($value): self |
107
|
|
|
{ |
108
|
|
|
if (!is_string($value)) { |
109
|
|
|
throw new \InvalidArgumentException(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->attributes = Attributes::addItem($this->attributes, Html5::ATTR_VALUE, htmlspecialchars($value)); |
113
|
|
|
|
114
|
|
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function __toString(): string |
121
|
|
|
{ |
122
|
|
|
return TagHelper::toString($this->tag, '', $this->getAttributes()); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|