|
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; |
|
13
|
|
|
|
|
14
|
|
|
use Iterator; |
|
15
|
|
|
|
|
16
|
|
|
abstract class AbstractForm implements FormInterface, Iterator |
|
17
|
|
|
{ |
|
18
|
|
|
/** @var array<FormElement> */ |
|
19
|
|
|
protected $form; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* FormInterface constructor. Creates <FORM> element automatically. |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $name |
|
25
|
|
|
* @param string $action |
|
26
|
|
|
* @param string $method |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct($name, $action = '', $method = 'POST') |
|
29
|
|
|
{ |
|
30
|
|
|
$form = new FormElement('form', $name); |
|
31
|
|
|
$form->setAttribute('action', $action) |
|
32
|
|
|
->setAttribute('method', strtoupper($method)); |
|
33
|
|
|
|
|
34
|
|
|
// for simplicity in rendering (twig macro), we store it in an array. |
|
35
|
|
|
$this->form[0] = $form; |
|
36
|
|
|
|
|
37
|
|
|
$this->initForm(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Initialize form. |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
abstract protected function initForm(); |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Set unique identifier for the form. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $uniqueFormNamePostfix |
|
51
|
|
|
* @return AbstractForm |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function setUniqueFormNamePostfix($uniqueFormNamePostfix) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->form[0]->setUniqueFormNamePostfix($uniqueFormNamePostfix); |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Sets form action. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $action |
|
64
|
|
|
* @return AbstractForm |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function setAction($action) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->form[0]->setAttribute('action', $action); |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Sets form submit. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $method |
|
77
|
|
|
* @return AbstractForm |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function setMethod($method = 'POST') |
|
80
|
|
|
{ |
|
81
|
|
|
$this->form[0]->setAttribute('method', $method); |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Sets form autocomplete option. |
|
88
|
|
|
* |
|
89
|
|
|
* @param bool $autocomplete |
|
90
|
|
|
* @return AbstractForm |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function setAutocomplete($autocomplete = true) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->form[0]->setAttribute('autocomplete', $autocomplete); |
|
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
return $this; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Sets form encryption type. |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $enctype |
|
103
|
|
|
* @return AbstractForm |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function setEnctype($enctype = 'application/x-www-form-urlencoded') |
|
106
|
|
|
{ |
|
107
|
|
|
$this->form[0]->setAttribute('enctype', $enctype); |
|
108
|
|
|
|
|
109
|
|
|
return $this; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Adds a form element to the form. |
|
114
|
|
|
* |
|
115
|
|
|
* @param FormElement $formElement |
|
116
|
|
|
* @return AbstractForm |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function addChildNode(FormElement $formElement) |
|
119
|
|
|
{ |
|
120
|
|
|
$formElement->setParentNode($this->form[0]); |
|
121
|
|
|
|
|
122
|
|
|
$this->form[0]->addChildNode($formElement); |
|
123
|
|
|
|
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Gets the form elements. |
|
129
|
|
|
* |
|
130
|
|
|
* @return array<FormElement> |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getChildNodes() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->form[0]->getChildNodes(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Validates the form. |
|
139
|
|
|
* |
|
140
|
|
|
* @return boolean |
|
141
|
|
|
*/ |
|
142
|
|
|
public function isValid() |
|
143
|
|
|
{ |
|
144
|
|
|
$valid = true; |
|
145
|
|
|
|
|
146
|
|
|
// TODO: TBD |
|
147
|
|
|
|
|
148
|
|
|
return $valid; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Sets form data. |
|
153
|
|
|
* |
|
154
|
|
|
* @param array $data |
|
155
|
|
|
* @return FormInterface |
|
156
|
|
|
*/ |
|
157
|
|
|
public function setData(array $data) |
|
158
|
|
|
{ |
|
159
|
|
|
// TODO: TBD |
|
160
|
|
|
|
|
161
|
|
|
// fake cotent to avoid phpmd warning until the real function logic is created... |
|
162
|
|
|
if (!empty($data)) { |
|
163
|
|
|
$this->isValid(); |
|
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Returns the form data. |
|
171
|
|
|
* |
|
172
|
|
|
* @return array |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getData() |
|
175
|
|
|
{ |
|
176
|
|
|
// TODO: TBD |
|
177
|
|
|
return []; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Return the current element. |
|
182
|
|
|
* |
|
183
|
|
|
* @return FormElement |
|
184
|
|
|
*/ |
|
185
|
|
|
final public function current() |
|
186
|
|
|
{ |
|
187
|
|
|
return current($this->form); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Moves the pointer forward to next element. |
|
192
|
|
|
* |
|
193
|
|
|
* @return void |
|
194
|
|
|
*/ |
|
195
|
|
|
final public function next() |
|
196
|
|
|
{ |
|
197
|
|
|
next($this->form); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Returns the key of the current element. |
|
202
|
|
|
* |
|
203
|
|
|
* @return mixed |
|
204
|
|
|
*/ |
|
205
|
|
|
final public function key() |
|
206
|
|
|
{ |
|
207
|
|
|
return key($this->form); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Checks if current position is valid. |
|
212
|
|
|
* |
|
213
|
|
|
* @return boolean |
|
214
|
|
|
*/ |
|
215
|
|
|
final public function valid() |
|
216
|
|
|
{ |
|
217
|
|
|
$key = key($this->form); |
|
218
|
|
|
|
|
219
|
|
|
return ($key !== null && $key !== false); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Rewinds the Iterator to the first element. |
|
224
|
|
|
* |
|
225
|
|
|
* @return void |
|
226
|
|
|
*/ |
|
227
|
|
|
final public function rewind() |
|
228
|
|
|
{ |
|
229
|
|
|
reset($this->form); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: