1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RoyallTheFourth\HtmlDocument\Element; |
4
|
|
|
|
5
|
|
|
use RoyallTheFourth\HtmlDocument\Attribute\BooleanAttribute; |
6
|
|
|
use RoyallTheFourth\HtmlDocument\Attribute\StandardAttribute; |
7
|
|
|
use RoyallTheFourth\HtmlDocument\Set\AttributeSet; |
8
|
|
|
use RoyallTheFourth\HtmlDocument\Tag\EmptyTag; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Input |
12
|
|
|
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input |
13
|
|
|
*/ |
14
|
|
|
final class Input extends AbstractElement |
15
|
|
|
{ |
16
|
|
|
public function __construct(AttributeSet $attributes = null) |
17
|
|
|
{ |
18
|
|
|
$this->attributes = $attributes ?? new AttributeSet(); |
19
|
|
|
$this->tag = new EmptyTag('input', $attributes); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function withAttribute(string $name, string $value = null): Input |
23
|
|
|
{ |
24
|
|
|
if ($value) { |
|
|
|
|
25
|
|
|
$attribute = new StandardAttribute($name, $value); |
26
|
|
|
} else { |
27
|
|
|
$attribute = new BooleanAttribute($name); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return new Input($this->attributes->add($attribute)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function withType(string $type): Input |
34
|
|
|
{ |
35
|
|
|
return $this->withAttribute('type', $type); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function withAccept(string $fileTypes): Input |
39
|
|
|
{ |
40
|
|
|
return $this->withAttribute('accept', $fileTypes); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function withAutoComplete(string $mode = 'on'): Input |
44
|
|
|
{ |
45
|
|
|
return $this->withAttribute('autocomplete', $mode); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function withAutoFocus(): Input |
49
|
|
|
{ |
50
|
|
|
return $this->withAttribute('autofocus'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function withCapture(string $capture): Input |
54
|
|
|
{ |
55
|
|
|
return $this->withAttribute('capture', $capture); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function withChecked(): Input |
59
|
|
|
{ |
60
|
|
|
return $this->withAttribute('checked'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function withDisabled(): Input |
64
|
|
|
{ |
65
|
|
|
return $this->withAttribute('disabled'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function withForm(string $id): Input |
69
|
|
|
{ |
70
|
|
|
return $this->withAttribute('form', $id); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function withFormAction(string $url): Input |
74
|
|
|
{ |
75
|
|
|
return $this->withAttribute('formaction', $url); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function withFormEncType(string $encoding = 'application/x-www-form-urlencoded'): Input |
79
|
|
|
{ |
80
|
|
|
return $this->withAttribute('formenctype', $encoding); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function withFormMethod(string $method): Input |
84
|
|
|
{ |
85
|
|
|
return $this->withAttribute('formmethod', $method); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function withFormNoValidate(): Input |
89
|
|
|
{ |
90
|
|
|
return $this->withAttribute('formnovalidate'); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function withFormTarget(string $target = '_self'): Input |
94
|
|
|
{ |
95
|
|
|
return $this->withAttribute('formtarget', $target); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function withHeight(int $pixels): Input |
99
|
|
|
{ |
100
|
|
|
return $this->withAttribute('height', $pixels); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function withInputMode(string $mode): Input |
104
|
|
|
{ |
105
|
|
|
return $this->withAttribute('inputmode', $mode); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function withList(string $dataListId): Input |
109
|
|
|
{ |
110
|
|
|
return $this->withAttribute('list', $dataListId); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function withMax(string $max): Input |
114
|
|
|
{ |
115
|
|
|
return $this->withAttribute('max', $max); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function withMaxLength(string $max): Input |
119
|
|
|
{ |
120
|
|
|
return $this->withAttribute('maxlength', $max); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function withMin(string $min): Input |
124
|
|
|
{ |
125
|
|
|
return $this->withAttribute('min', $min); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function withMinLength(string $min): Input |
129
|
|
|
{ |
130
|
|
|
return $this->withAttribute('minlength', $min); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function withMultiple(): Input |
134
|
|
|
{ |
135
|
|
|
return $this->withAttribute('multiple'); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function withName(string $name): Input |
139
|
|
|
{ |
140
|
|
|
return $this->withAttribute('name', $name); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function withPattern(string $regex): Input |
144
|
|
|
{ |
145
|
|
|
return $this->withAttribute('pattern', $regex); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function withPlaceholder(string $placeholder): Input |
149
|
|
|
{ |
150
|
|
|
return $this->withAttribute('placeholder', $placeholder); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function withReadOnly(): Input |
154
|
|
|
{ |
155
|
|
|
return $this->withAttribute('readonly'); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function withRequired(): Input |
159
|
|
|
{ |
160
|
|
|
return $this->withAttribute('required'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function withSelectionDirection(string $direction = 'forward'): Input |
164
|
|
|
{ |
165
|
|
|
return $this->withAttribute('selectionDirection', $direction); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function withSelectionEnd(string $end): Input |
169
|
|
|
{ |
170
|
|
|
return $this->withAttribute('selectionEnd', $end); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function withSelectionStart(string $start): Input |
174
|
|
|
{ |
175
|
|
|
return $this->withAttribute('selectionStart', $start); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function withSize(int $size): Input |
179
|
|
|
{ |
180
|
|
|
return $this->withAttribute('size', $size); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function withSpellCheck(string $check = 'default'): Input |
184
|
|
|
{ |
185
|
|
|
return $this->withAttribute('spellcheck', $check); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function withSrc(string $url): Input |
189
|
|
|
{ |
190
|
|
|
return $this->withAttribute('src', $url); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function withStep(string $increment): Input |
194
|
|
|
{ |
195
|
|
|
return $this->withAttribute('step', $increment); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function withValue(string $value): Input |
199
|
|
|
{ |
200
|
|
|
return $this->withAttribute('value', $value); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function withWidth(int $width): Input |
204
|
|
|
{ |
205
|
|
|
return $this->withAttribute('width', $width); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: