|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* User: delboy1978uk |
|
4
|
|
|
* Date: 19/11/2016 |
|
5
|
|
|
* Time: 12:13 |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Del\Form; |
|
9
|
|
|
|
|
10
|
|
|
use Del\Form\Collection\FieldCollection; |
|
11
|
|
|
use Del\Form\Field\FieldInterface; |
|
12
|
|
|
use Del\Form\Field\FileUpload; |
|
13
|
|
|
use Del\Form\Renderer\FormRenderer; |
|
14
|
|
|
use Del\Form\Renderer\FormRendererInterface; |
|
15
|
|
|
use Del\Form\Traits\HasAttributesTrait; |
|
16
|
|
|
|
|
17
|
|
|
abstract class AbstractForm implements FormInterface |
|
18
|
|
|
{ |
|
19
|
|
|
const ENC_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data'; |
|
20
|
|
|
const ENC_TYPE_URL_ENCODED = 'application/x-www-form-urlencoded'; |
|
21
|
|
|
const ENC_TYPE_TEXT_PLAIN = 'text/plain'; |
|
22
|
|
|
|
|
23
|
|
|
const METHOD_POST = 'post'; |
|
24
|
|
|
const METHOD_GET = 'get'; |
|
25
|
|
|
|
|
26
|
|
|
/** @var FieldCollection $fieldCollection */ |
|
27
|
|
|
private $fieldCollection; |
|
28
|
|
|
|
|
29
|
|
|
/** @var FormRendererInterface */ |
|
30
|
|
|
private $formRenderer; |
|
31
|
|
|
|
|
32
|
|
|
/** @var array $errorMessages */ |
|
33
|
|
|
private $errorMessages; |
|
34
|
|
|
|
|
35
|
|
|
/** @var bool $displayErrors */ |
|
36
|
|
|
private $displayErrors; |
|
37
|
|
|
|
|
38
|
|
|
use HasAttributesTrait; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* AbstractForm constructor. |
|
42
|
|
|
* @param $name |
|
43
|
|
|
*/ |
|
44
|
47 |
|
public function __construct($name) |
|
45
|
|
|
{ |
|
46
|
47 |
|
$this->fieldCollection = new FieldCollection(); |
|
47
|
47 |
|
$this->formRenderer = new FormRenderer(); |
|
48
|
47 |
|
$this->attributes = [ |
|
49
|
47 |
|
'name' => $name, |
|
50
|
47 |
|
'method' => self::METHOD_POST, |
|
51
|
|
|
]; |
|
52
|
47 |
|
$this->displayErrors = false; |
|
53
|
47 |
|
$this->init(); |
|
54
|
47 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
abstract public function init(); |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return bool |
|
60
|
|
|
*/ |
|
61
|
8 |
|
public function isValid() |
|
62
|
|
|
{ |
|
63
|
8 |
|
$this->errorMessages = []; |
|
64
|
8 |
|
$this->validateFields(); |
|
65
|
8 |
|
$count = count($this->errorMessages); |
|
66
|
8 |
|
$valid = ($count == 0); |
|
67
|
8 |
|
if ($valid) { |
|
68
|
6 |
|
$this->moveUploadedFiles(); |
|
69
|
|
|
} |
|
70
|
7 |
|
return $valid; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
8 |
|
private function validateFields() |
|
74
|
|
|
{ |
|
75
|
8 |
|
$this->fieldCollection->rewind(); |
|
76
|
8 |
|
while ($this->fieldCollection->valid()) { |
|
77
|
8 |
|
$this->checkFieldForErrors($this->fieldCollection->current()); |
|
78
|
8 |
|
$this->fieldCollection->next(); |
|
79
|
|
|
} |
|
80
|
8 |
|
$this->fieldCollection->rewind(); |
|
81
|
8 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param FieldInterface $field |
|
85
|
|
|
*/ |
|
86
|
8 |
|
private function checkFieldForErrors(FieldInterface $field) |
|
87
|
|
|
{ |
|
88
|
8 |
|
if (!$field->isValid()) { |
|
89
|
6 |
|
$this->errorMessages[$field->getName()] = $field->getMessages(); |
|
90
|
|
|
} |
|
91
|
8 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
4 |
|
public function getValues() |
|
97
|
|
|
{ |
|
98
|
4 |
|
$values = []; |
|
99
|
4 |
|
$this->fieldCollection->rewind(); |
|
100
|
4 |
|
while ($this->fieldCollection->valid()) { |
|
101
|
4 |
|
$field = $this->fieldCollection->current(); |
|
102
|
4 |
|
$values[$field->getName()] = $field->getValue(); |
|
103
|
4 |
|
$this->fieldCollection->next(); |
|
104
|
|
|
} |
|
105
|
4 |
|
$this->fieldCollection->rewind(); |
|
106
|
4 |
|
return $values; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param array $data |
|
111
|
|
|
* @return $this |
|
112
|
|
|
*/ |
|
113
|
5 |
|
public function populate(array $data) |
|
114
|
|
|
{ |
|
115
|
5 |
|
$this->fieldCollection->rewind(); |
|
116
|
5 |
|
while ($this->fieldCollection->valid()) { |
|
117
|
5 |
|
$field = $this->fieldCollection->current(); |
|
118
|
5 |
|
$name = $field->getName(); |
|
119
|
5 |
|
if (isset($data[$name])) { |
|
120
|
2 |
|
$field->setValue($data[$name]); |
|
121
|
|
|
} |
|
122
|
5 |
|
$this->fieldCollection->next(); |
|
123
|
|
|
} |
|
124
|
5 |
|
$this->fieldCollection->rewind(); |
|
125
|
5 |
|
$this->displayErrors = true; |
|
126
|
5 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $name |
|
131
|
|
|
* @return FieldInterface|null |
|
132
|
|
|
*/ |
|
133
|
2 |
|
public function getField($name) |
|
134
|
|
|
{ |
|
135
|
2 |
|
return $this->fieldCollection->findByName($name); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return FieldCollection |
|
140
|
|
|
*/ |
|
141
|
30 |
|
public function getFields() |
|
142
|
|
|
{ |
|
143
|
30 |
|
return $this->fieldCollection; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* @param FieldInterface $field |
|
148
|
|
|
* @return $this |
|
149
|
|
|
*/ |
|
150
|
38 |
|
public function addField(FieldInterface $field) |
|
151
|
|
|
{ |
|
152
|
38 |
|
$this->fieldCollection->append($field); |
|
153
|
38 |
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @return string |
|
158
|
|
|
*/ |
|
159
|
28 |
|
public function render() |
|
160
|
|
|
{ |
|
161
|
28 |
|
return $this->formRenderer->render($this, $this->isDisplayErrors()); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param $url |
|
166
|
|
|
* @return $this |
|
167
|
|
|
*/ |
|
168
|
2 |
|
public function setAction($url) |
|
169
|
|
|
{ |
|
170
|
2 |
|
$this->setAttribute('action', $url); |
|
171
|
2 |
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
2 |
|
public function getAction() |
|
178
|
|
|
{ |
|
179
|
2 |
|
return $this->getAttribute('action'); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @return string |
|
184
|
|
|
*/ |
|
185
|
29 |
|
public function getId() |
|
186
|
|
|
{ |
|
187
|
29 |
|
return $this->getAttribute('id'); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param string $id |
|
192
|
|
|
* @return $this |
|
193
|
|
|
*/ |
|
194
|
2 |
|
public function setId($id) |
|
195
|
|
|
{ |
|
196
|
2 |
|
$this->setAttribute('id', $id); |
|
197
|
2 |
|
return $this; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param $encType |
|
202
|
|
|
* @return $this |
|
203
|
|
|
*/ |
|
204
|
2 |
|
public function setEncType($encType) |
|
205
|
|
|
{ |
|
206
|
2 |
|
$this->setAttribute('enctype', $encType); |
|
207
|
2 |
|
return $this; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return string |
|
212
|
|
|
*/ |
|
213
|
1 |
|
public function getEncType() |
|
214
|
|
|
{ |
|
215
|
1 |
|
return $this->getAttribute('enctype'); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* @param string $method |
|
220
|
|
|
* @return FormInterface |
|
221
|
|
|
*/ |
|
222
|
2 |
|
public function setMethod($method) |
|
223
|
|
|
{ |
|
224
|
2 |
|
$this->setAttribute('method', $method); |
|
225
|
2 |
|
return $this; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @return string |
|
230
|
|
|
*/ |
|
231
|
29 |
|
public function getMethod() |
|
232
|
|
|
{ |
|
233
|
29 |
|
return $this->getAttribute('method'); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param $class |
|
238
|
|
|
* @return FormInterface |
|
239
|
|
|
*/ |
|
240
|
2 |
|
public function setClass($class) |
|
241
|
|
|
{ |
|
242
|
2 |
|
$this->setAttribute('class', $class); |
|
243
|
2 |
|
return $this; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @return string |
|
248
|
|
|
*/ |
|
249
|
2 |
|
public function getClass() |
|
250
|
|
|
{ |
|
251
|
2 |
|
return $this->getAttribute('class'); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* @return boolean |
|
256
|
|
|
*/ |
|
257
|
29 |
|
public function isDisplayErrors() |
|
258
|
|
|
{ |
|
259
|
29 |
|
return $this->displayErrors; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @param boolean $displayErrors |
|
264
|
|
|
* @return AbstractForm |
|
265
|
|
|
*/ |
|
266
|
3 |
|
public function setDisplayErrors($displayErrors) |
|
267
|
|
|
{ |
|
268
|
3 |
|
$this->displayErrors = $displayErrors; |
|
269
|
3 |
|
return $this; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @param FormRendererInterface $renderer |
|
274
|
|
|
* @return AbstractForm |
|
275
|
|
|
*/ |
|
276
|
2 |
|
public function setFormRenderer(FormRendererInterface $renderer) |
|
277
|
|
|
{ |
|
278
|
2 |
|
$this->formRenderer = $renderer; |
|
279
|
2 |
|
return $this; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
6 |
|
public function moveUploadedFiles() |
|
283
|
|
|
{ |
|
284
|
6 |
|
$this->fieldCollection->rewind(); |
|
285
|
6 |
|
while ($this->fieldCollection->valid()) { |
|
286
|
6 |
|
$current = $this->fieldCollection->current(); |
|
287
|
6 |
|
$this->moveFileIfUploadField($current); |
|
288
|
5 |
|
$this->fieldCollection->next() ; |
|
289
|
|
|
} |
|
290
|
5 |
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* @param FieldInterface $field |
|
294
|
|
|
* @return bool |
|
295
|
|
|
*/ |
|
296
|
6 |
|
public function moveFileIfUploadField(FieldInterface $field) |
|
297
|
|
|
{ |
|
298
|
6 |
|
if ($field instanceof FileUpload) { |
|
299
|
2 |
|
$field->moveUploadToDestination(); |
|
300
|
1 |
|
return true; |
|
301
|
|
|
} |
|
302
|
4 |
|
return false; |
|
303
|
|
|
} |
|
304
|
|
|
} |