1 | <?php |
||
22 | final class FormElement implements Iterator |
||
23 | { |
||
24 | /** HTML5 form elements */ |
||
25 | const TAG_FORM = 'form'; |
||
26 | const TAG_INPUT_CHECKBOX = 'checkbox'; |
||
27 | const TAG_INPUT_COLOR = 'color'; |
||
28 | const TAG_INPUT_DATA = 'date'; |
||
29 | const TAG_INPUT_DATETIME = 'datetime'; |
||
30 | const TAG_INPUT_DATETIME_LOCAL = 'datetime-local'; |
||
31 | const TAG_INPUT_EMAIL = 'email'; |
||
32 | const TAG_INPUT_FILE = 'file'; |
||
33 | const TAG_INPUT_HIDDEN = 'hidden'; |
||
34 | const TAG_INPUT_IMAGE = 'image'; |
||
35 | const TAG_INPUT_MONTH = 'month'; |
||
36 | const TAG_INPUT_NUMBER = 'number'; |
||
37 | const TAG_INPUT_PASSWORD = 'password'; |
||
38 | const TAG_INPUT_RADIO = 'radio'; |
||
39 | const TAG_INPUT_RANGE = 'range'; |
||
40 | const TAG_INPUT_SEARCH = 'search'; |
||
41 | const TAG_INPUT_TEL = 'tel'; |
||
42 | const TAG_INPUT_TEXT = 'text'; |
||
43 | const TAG_INPUT_TIME = 'time'; |
||
44 | const TAG_INPUT_URL = 'url'; |
||
45 | const TAG_INPUT_WEEK = 'week'; |
||
46 | const TAG_TEXTAREA = 'textarea'; |
||
47 | const TAG_FIELDSET = 'fieldset'; |
||
48 | const TAG_LEGEND = 'legend'; |
||
49 | const TAG_LABEL = 'label'; |
||
50 | const TAG_BUTTON_SUBMIT = 'submit'; |
||
51 | const TAG_BUTTON_RESET = 'reset'; |
||
52 | const TAG_BUTTON = 'button'; |
||
53 | const TAG_DATALIST = 'datalist'; |
||
54 | const TAG_SELECT = 'select'; |
||
55 | const TAG_OPTION_GROUP = 'optgroup'; |
||
56 | const TAG_OPTION = 'option'; |
||
57 | const TAG_KEYGEN = 'keygen'; |
||
58 | const TAG_OUTPUT = 'output'; |
||
59 | |||
60 | /** @var int */ |
||
61 | protected static $tabIndex = 1; |
||
62 | /** @var string */ |
||
63 | private $tagName; |
||
64 | /** @var string */ |
||
65 | private $name; |
||
66 | /** @var string */ |
||
67 | private $label; |
||
68 | /** @var mixed */ |
||
69 | private $value; |
||
70 | /** @var array */ |
||
71 | private $options = []; |
||
72 | /** @var array */ |
||
73 | private $attributes; |
||
74 | /** @var FormElement */ |
||
75 | private $parentNode; |
||
76 | /** @var array<FormElement> */ |
||
77 | private $childNodes; |
||
78 | /** @var array<FormValidatorInterface> */ |
||
79 | private $validators; |
||
80 | /** @var array */ |
||
81 | private $mandatoryTagParents = [ |
||
82 | self::TAG_FORM => [], |
||
83 | self::TAG_LEGEND => [ |
||
84 | self::TAG_FIELDSET |
||
85 | ], |
||
86 | self::TAG_OPTION => [ |
||
87 | self::TAG_DATALIST, |
||
88 | self::TAG_OPTION_GROUP, |
||
89 | self::TAG_SELECT |
||
90 | ], |
||
91 | self::TAG_OPTION_GROUP => [ |
||
92 | self::TAG_SELECT |
||
93 | ], |
||
94 | ]; |
||
95 | /** @var array */ |
||
96 | private $multiOptionTags = [ |
||
97 | self::TAG_SELECT, |
||
98 | self::TAG_INPUT_RADIO, |
||
99 | self::TAG_INPUT_CHECKBOX, |
||
100 | self::TAG_DATALIST |
||
101 | ]; |
||
102 | |||
103 | /** |
||
104 | * FormElement constructor. |
||
105 | * |
||
106 | * @param string $tagName |
||
107 | * @param string $name |
||
108 | * @param string $label |
||
109 | */ |
||
110 | public function __construct($tagName, $name, $label = '') |
||
117 | |||
118 | /** |
||
119 | * Returns the element tag name. |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getTagName() |
||
127 | |||
128 | /** |
||
129 | * Sets parent element name |
||
130 | * |
||
131 | * @param FormElement $formElement |
||
132 | * @throws RuntimeException |
||
133 | * @return FormElement |
||
134 | */ |
||
135 | public function setParentNode(FormElement $formElement) |
||
155 | |||
156 | /** |
||
157 | * Returns the element name. |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public function getName() |
||
178 | |||
179 | /** |
||
180 | * Returns the element label. |
||
181 | * |
||
182 | * @return string |
||
183 | */ |
||
184 | public function getLabel() |
||
188 | |||
189 | /** |
||
190 | * Sets element value. |
||
191 | * |
||
192 | * @param mixed $value |
||
193 | * @return FormElement |
||
194 | */ |
||
195 | public function setValue($value) |
||
201 | |||
202 | /** |
||
203 | * Returns element value. |
||
204 | * |
||
205 | * @return mixed |
||
206 | */ |
||
207 | public function getValue() |
||
211 | |||
212 | /** |
||
213 | * Set label-value options for the element. |
||
214 | * |
||
215 | * @param array $options |
||
216 | * @throws RuntimeException |
||
217 | * @return FormElement |
||
218 | */ |
||
219 | public function setOptions(array $options) |
||
228 | |||
229 | /** |
||
230 | * Sets label-value option for the element. |
||
231 | * |
||
232 | * @param string $label |
||
233 | * @param string $value |
||
234 | * @param boolean $checked |
||
235 | * @return FormElement |
||
236 | */ |
||
237 | public function setOption($label, $value, $checked = false) |
||
252 | |||
253 | /** |
||
254 | * Checks if the element has value options. |
||
255 | * |
||
256 | * @return bool |
||
257 | */ |
||
258 | public function hasOptions() |
||
262 | |||
263 | /** |
||
264 | * Gets element value options. |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | public function getOptions() |
||
272 | |||
273 | /** |
||
274 | * Set child node for the element. |
||
275 | * |
||
276 | * @param FormElement $childNode |
||
277 | * @return FormElement |
||
278 | */ |
||
279 | public function addChildNode(FormElement $childNode) |
||
287 | |||
288 | /** |
||
289 | * Gets the child nodes of the element. |
||
290 | * |
||
291 | * @return array<FormElement> |
||
292 | */ |
||
293 | public function getChildNodes() |
||
297 | |||
298 | /** |
||
299 | * Sets element attribute. |
||
300 | * |
||
301 | * @param string $key |
||
302 | * @param string $value |
||
303 | * @throws InvalidArgumentException |
||
304 | * @return FormElement |
||
305 | */ |
||
306 | public function setAttribute($key, $value) |
||
320 | |||
321 | /** |
||
322 | * Sets multiple attributes. |
||
323 | * |
||
324 | * @param array $attributes |
||
325 | * @return FormElement |
||
326 | */ |
||
327 | public function setAttributes(array $attributes) |
||
335 | |||
336 | /** |
||
337 | * Gets element attribute. |
||
338 | * |
||
339 | * @param string $name |
||
340 | * @return mixed |
||
341 | */ |
||
342 | public function getAttribute($name) |
||
350 | |||
351 | /** |
||
352 | * Gets all the attributes. |
||
353 | * |
||
354 | * @return array |
||
355 | */ |
||
356 | public function getAttributes() |
||
360 | |||
361 | /** |
||
362 | * Adds validator to the form. |
||
363 | * |
||
364 | * @param FormValidatorInterface $validator |
||
365 | * @return FormElement |
||
366 | */ |
||
367 | public function addValidator(FormValidatorInterface $validator) |
||
373 | |||
374 | /** |
||
375 | * Validates element value. |
||
376 | * |
||
377 | * @return bool |
||
378 | */ |
||
379 | public function isValid() |
||
389 | |||
390 | /** |
||
391 | * Return the current element. |
||
392 | * |
||
393 | * @return FormElement |
||
394 | */ |
||
395 | final public function current() |
||
399 | |||
400 | /** |
||
401 | * Moves the pointer forward to next element. |
||
402 | * |
||
403 | * @return void |
||
404 | */ |
||
405 | final public function next() |
||
409 | |||
410 | /** |
||
411 | * Returns the key of the current element. |
||
412 | * |
||
413 | * @return mixed |
||
414 | */ |
||
415 | final public function key() |
||
419 | |||
420 | /** |
||
421 | * Checks if current position is valid. |
||
422 | * |
||
423 | * @return boolean |
||
424 | */ |
||
425 | final public function valid() |
||
431 | |||
432 | /** |
||
433 | * Rewinds the Iterator to the first element. |
||
434 | * |
||
435 | * @return void |
||
436 | */ |
||
437 | final public function rewind() |
||
441 | } |
||
442 |