|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.6 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2016 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace WebHemi\Form\Element; |
|
13
|
|
|
|
|
14
|
|
|
use Iterator; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class AbstractFormElement |
|
18
|
|
|
*/ |
|
19
|
|
|
abstract class AbstractFormElement implements FormElementInterface, Iterator |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var int */ |
|
22
|
|
|
protected static $tabIndex = 1; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
protected $tagName; |
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
protected $name; |
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
protected $label; |
|
30
|
|
|
/** @var mixed */ |
|
31
|
|
|
protected $value; |
|
32
|
|
|
/** @var array */ |
|
33
|
|
|
protected $attributes = []; |
|
34
|
|
|
/** @var array<FormValidatorInterface> */ |
|
35
|
|
|
protected $validators = []; |
|
36
|
|
|
/** @var array */ |
|
37
|
|
|
protected $errors = []; |
|
38
|
|
|
/** @var FormElementInterface */ |
|
39
|
|
|
protected $parentNode = null; |
|
40
|
|
|
/** @var array<FormElementInterface> */ |
|
41
|
|
|
protected $childNodes = []; |
|
42
|
|
|
|
|
43
|
|
|
/** @var array */ |
|
44
|
|
|
protected $mandatoryTagParents = [ |
|
45
|
|
|
self::TAG_FORM => [], |
|
46
|
|
|
self::TAG_LEGEND => [ |
|
47
|
|
|
self::TAG_FIELDSET |
|
48
|
|
|
], |
|
49
|
|
|
self::TAG_OPTION => [ |
|
50
|
|
|
self::TAG_DATALIST, |
|
51
|
|
|
self::TAG_OPTION_GROUP, |
|
52
|
|
|
self::TAG_SELECT |
|
53
|
|
|
], |
|
54
|
|
|
self::TAG_OPTION_GROUP => [ |
|
55
|
|
|
self::TAG_SELECT |
|
56
|
|
|
], |
|
57
|
|
|
]; |
|
58
|
|
|
/** @var array */ |
|
59
|
|
|
protected $multiOptionTags = [ |
|
60
|
|
|
self::TAG_SELECT, |
|
61
|
|
|
self::TAG_INPUT_RADIO, |
|
62
|
|
|
self::TAG_INPUT_CHECKBOX, |
|
63
|
|
|
self::TAG_DATALIST |
|
64
|
|
|
]; |
|
65
|
|
|
/** @var array */ |
|
66
|
|
|
protected $tabIndexableTags = [ |
|
67
|
|
|
self::TAG_INPUT_CHECKBOX, |
|
68
|
|
|
self::TAG_INPUT_COLOR, |
|
69
|
|
|
self::TAG_INPUT_DATA, |
|
70
|
|
|
self::TAG_INPUT_DATETIME, |
|
71
|
|
|
self::TAG_INPUT_DATETIME_LOCAL, |
|
72
|
|
|
self::TAG_INPUT_EMAIL, |
|
73
|
|
|
self::TAG_INPUT_FILE, |
|
74
|
|
|
self::TAG_INPUT_IMAGE, |
|
75
|
|
|
self::TAG_INPUT_MONTH, |
|
76
|
|
|
self::TAG_INPUT_NUMBER, |
|
77
|
|
|
self::TAG_INPUT_PASSWORD, |
|
78
|
|
|
self::TAG_INPUT_RADIO, |
|
79
|
|
|
self::TAG_INPUT_RANGE, |
|
80
|
|
|
self::TAG_INPUT_SEARCH, |
|
81
|
|
|
self::TAG_INPUT_TEL, |
|
82
|
|
|
self::TAG_INPUT_TEXT, |
|
83
|
|
|
self::TAG_INPUT_TIME, |
|
84
|
|
|
self::TAG_INPUT_URL, |
|
85
|
|
|
self::TAG_INPUT_WEEK, |
|
86
|
|
|
self::TAG_TEXTAREA, |
|
87
|
|
|
self::TAG_BUTTON_SUBMIT, |
|
88
|
|
|
self::TAG_BUTTON_RESET, |
|
89
|
|
|
self::TAG_BUTTON, |
|
90
|
|
|
self::TAG_DATALIST, |
|
91
|
|
|
self::TAG_SELECT, |
|
92
|
|
|
self::TAG_KEYGEN, |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* AbstractFormElement constructor. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $tagName |
|
99
|
|
|
* @param string $name |
|
100
|
|
|
* @param string $label |
|
101
|
|
|
* @param mixed $value |
|
102
|
|
|
*/ |
|
103
|
|
|
abstract public function __construct($tagName, $name = '', $label = '', $value = null); |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns the element tag name. |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getTagName() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->tagName; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Sets element name. The implementation should decide if it is allowed after init. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $name |
|
119
|
|
|
* @return FormElementInterface |
|
120
|
|
|
*/ |
|
121
|
|
|
abstract public function setName($name); |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns the element name. |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getName() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->name; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Sets element label. |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $label |
|
137
|
|
|
* @return FormElementInterface |
|
138
|
|
|
*/ |
|
139
|
|
|
abstract public function setLabel($label); |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Returns the element label. |
|
143
|
|
|
* |
|
144
|
|
|
* @return string |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getLabel() |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->label; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Sets element value. |
|
153
|
|
|
* |
|
154
|
|
|
* @param mixed $value |
|
155
|
|
|
* @return FormElementInterface |
|
156
|
|
|
*/ |
|
157
|
|
|
abstract public function setValue($value); |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Returns element value. |
|
161
|
|
|
* |
|
162
|
|
|
* @return mixed |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getValue() |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->value; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Sets multiple attributes. |
|
171
|
|
|
* |
|
172
|
|
|
* @param array $attributes |
|
173
|
|
|
* @return FormElementInterface |
|
174
|
|
|
*/ |
|
175
|
|
|
abstract public function setAttributes(array $attributes); |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Gets all the attributes. |
|
179
|
|
|
* |
|
180
|
|
|
* @return array |
|
181
|
|
|
*/ |
|
182
|
|
|
public function getAttributes() |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->attributes; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Sets the element errors. Usually the validator should set it, but it is allowed to set from outside too. |
|
189
|
|
|
* |
|
190
|
|
|
* @param array $errors |
|
191
|
|
|
* @return FormElementInterface |
|
192
|
|
|
*/ |
|
193
|
|
|
abstract public function setErrors(array $errors); |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* Gets validation errors. |
|
197
|
|
|
* |
|
198
|
|
|
* @return array |
|
199
|
|
|
*/ |
|
200
|
|
|
public function getErrors() |
|
201
|
|
|
{ |
|
202
|
|
|
return $this->errors; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Sets the element validators. |
|
207
|
|
|
* |
|
208
|
|
|
* @param array<FormValidatorInterface> $validators |
|
209
|
|
|
* @return FormElementInterface |
|
210
|
|
|
*/ |
|
211
|
|
|
abstract public function setValidators(array $validators); |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Gets the element validators. |
|
215
|
|
|
* |
|
216
|
|
|
* @return array<FormValidatorInterface> |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getValidators() |
|
219
|
|
|
{ |
|
220
|
|
|
return $this->validators; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Validates element value. |
|
225
|
|
|
* |
|
226
|
|
|
* @param bool $reValidate |
|
227
|
|
|
* @return bool |
|
228
|
|
|
*/ |
|
229
|
|
|
abstract public function isValid($reValidate = false); |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Sets the parent element. |
|
233
|
|
|
* |
|
234
|
|
|
* @param FormElementInterface $formElement |
|
235
|
|
|
* @return FormElementInterface |
|
236
|
|
|
*/ |
|
237
|
|
|
abstract public function setParentNode(FormElementInterface $formElement); |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Gets the parent element. |
|
241
|
|
|
* |
|
242
|
|
|
* @return FormElementInterface |
|
243
|
|
|
*/ |
|
244
|
|
|
public function getParentNode() |
|
245
|
|
|
{ |
|
246
|
|
|
return $this->parentNode; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Set the child nodes for the element. |
|
251
|
|
|
* |
|
252
|
|
|
* @param array<FormElementInterface> $childNodes |
|
253
|
|
|
* @return FormElementInterface |
|
254
|
|
|
*/ |
|
255
|
|
|
abstract public function setChildNodes(array $childNodes); |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Checks if there are child elements. |
|
259
|
|
|
* |
|
260
|
|
|
* @return boolean |
|
261
|
|
|
*/ |
|
262
|
|
|
public function hasChildNodes() |
|
263
|
|
|
{ |
|
264
|
|
|
return !empty($this->childNodes); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Gets the child nodes of the element. |
|
269
|
|
|
* |
|
270
|
|
|
* @return array<FormElementInterface> |
|
271
|
|
|
*/ |
|
272
|
|
|
public function getChildNodes() |
|
273
|
|
|
{ |
|
274
|
|
|
return $this->childNodes; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Return the current element. |
|
279
|
|
|
* |
|
280
|
|
|
* @return FormElementInterface |
|
281
|
|
|
*/ |
|
282
|
|
|
final public function current() |
|
283
|
|
|
{ |
|
284
|
|
|
return current($this->childNodes); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Moves the pointer forward to next element. |
|
289
|
|
|
* |
|
290
|
|
|
* @return void |
|
291
|
|
|
*/ |
|
292
|
|
|
final public function next() |
|
293
|
|
|
{ |
|
294
|
|
|
next($this->childNodes); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* Returns the key of the current element. |
|
299
|
|
|
* |
|
300
|
|
|
* @return mixed |
|
301
|
|
|
*/ |
|
302
|
|
|
final public function key() |
|
303
|
|
|
{ |
|
304
|
|
|
return key($this->childNodes); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* Checks if current position is valid. |
|
309
|
|
|
* |
|
310
|
|
|
* @return boolean |
|
311
|
|
|
*/ |
|
312
|
|
|
final public function valid() |
|
313
|
|
|
{ |
|
314
|
|
|
$key = key($this->childNodes); |
|
315
|
|
|
|
|
316
|
|
|
return ($key !== null && $key !== false); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Rewinds the Iterator to the first element. |
|
321
|
|
|
* |
|
322
|
|
|
* @return void |
|
323
|
|
|
*/ |
|
324
|
|
|
final public function rewind() |
|
325
|
|
|
{ |
|
326
|
|
|
reset($this->childNodes); |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|