|
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 DOMDocument; |
|
13
|
|
|
use DOMElement; |
|
14
|
|
|
|
|
15
|
|
|
abstract class AbstractForm implements FormInterface |
|
16
|
|
|
{ |
|
17
|
|
|
const ENC_TYPE_MULTIPART_FORM_DATA = 'multipart/form-data'; |
|
18
|
|
|
const METHOD_POST = 'post'; |
|
19
|
|
|
const METHOD_GET = 'get'; |
|
20
|
|
|
|
|
21
|
|
|
/** @var FieldCollection $fieldCollection */ |
|
22
|
|
|
private $fieldCollection; |
|
23
|
|
|
|
|
24
|
|
|
/** @var DOMDocument $dom */ |
|
25
|
|
|
private $dom; |
|
26
|
|
|
|
|
27
|
|
|
/** @var DomElement $form */ |
|
28
|
|
|
private $form; |
|
29
|
|
|
|
|
30
|
7 |
|
public function __construct($name) |
|
31
|
|
|
{ |
|
32
|
7 |
|
$this->fieldCollection = new FieldCollection(); |
|
33
|
7 |
|
$this->dom = new DOMDocument(); |
|
34
|
7 |
|
$form = $this->dom->createElement('form'); |
|
35
|
7 |
|
$form->setAttribute('name', $name); |
|
36
|
7 |
|
$this->form = $form; |
|
|
|
|
|
|
37
|
7 |
|
$this->init(); |
|
38
|
7 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
abstract public function init(); |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return bool |
|
44
|
|
|
*/ |
|
45
|
|
|
public function isValid() |
|
46
|
|
|
{ |
|
47
|
|
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function getValues() |
|
54
|
|
|
{ |
|
55
|
1 |
|
$values = []; |
|
56
|
|
|
/** @var FieldInterface $field */ |
|
57
|
1 |
|
foreach ($this->fieldCollection as $field) { |
|
58
|
1 |
|
$values[$field->getName()] = $field->getValue(); |
|
59
|
|
|
} |
|
60
|
1 |
|
return $values; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param array $data |
|
65
|
|
|
* @return $this |
|
66
|
|
|
*/ |
|
67
|
|
|
public function populate(array $data) |
|
68
|
|
|
{ |
|
69
|
|
|
/** @var FieldInterface $field */ |
|
70
|
|
|
foreach ($this->fieldCollection as $field) { |
|
71
|
|
|
$name = $field->getName(); |
|
72
|
|
|
if (isset($data[$name])) { |
|
73
|
|
|
$field->setValue($data[$name]); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $name |
|
81
|
|
|
* @return FieldInterface|null |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getField($name) |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->fieldCollection->findByName($name); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return FieldCollection |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getFields() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->fieldCollection; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param FieldInterface $field |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function addField(FieldInterface $field) |
|
101
|
|
|
{ |
|
102
|
1 |
|
$this->fieldCollection->append($field); |
|
103
|
1 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
2 |
|
public function render() |
|
110
|
|
|
{ |
|
111
|
2 |
|
$method = $this->form->getAttribute('method') ?: self::METHOD_POST; |
|
112
|
2 |
|
$id = $this->form->getAttribute('id') ?: $this->form->getAttribute('name'); |
|
113
|
2 |
|
$action = $this->form->getAttribute('action') ?: $this->form->getAttribute('action'); |
|
114
|
|
|
|
|
115
|
2 |
|
$this->form->setAttribute('id', $id); |
|
116
|
2 |
|
$this->form->setAttribute('method', $method); |
|
117
|
2 |
|
$this->form->setAttribute('action', $action); |
|
118
|
|
|
|
|
119
|
2 |
|
$this->fieldCollection->rewind(); |
|
120
|
2 |
|
while ($this->fieldCollection->valid()) { |
|
121
|
|
|
/** @var FieldInterface $current */ |
|
122
|
1 |
|
$current = $this->fieldCollection->current(); |
|
123
|
1 |
|
$child = $this->dom->createElement($current->getTag()); |
|
124
|
|
|
|
|
125
|
1 |
|
$child->setAttribute('type', $current->getTagType()); |
|
126
|
1 |
|
$child->setAttribute('name', $current->getName()); |
|
127
|
1 |
|
$child->setAttribute('id', $current->getId()); |
|
128
|
1 |
|
$child->setAttribute('value', $current->getValue()); |
|
129
|
1 |
|
$child->setAttribute('class', $current->getClass()); |
|
130
|
|
|
|
|
131
|
1 |
|
$this->form->appendChild($child); |
|
132
|
1 |
|
$this->fieldCollection->next(); |
|
133
|
|
|
} |
|
134
|
2 |
|
$this->fieldCollection->rewind(); |
|
135
|
|
|
|
|
136
|
2 |
|
$this->dom->appendChild($this->form); |
|
137
|
|
|
|
|
138
|
2 |
|
return $this->dom->saveHTML(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param $url |
|
143
|
|
|
* @return $this |
|
144
|
|
|
*/ |
|
145
|
2 |
|
public function setAction($url) |
|
146
|
|
|
{ |
|
147
|
2 |
|
$this->form->setAttribute('action', $url); |
|
148
|
2 |
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
2 |
|
public function getAction() |
|
155
|
|
|
{ |
|
156
|
2 |
|
return $this->form->getAttribute('action'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
2 |
|
public function getId() |
|
163
|
|
|
{ |
|
164
|
2 |
|
return $this->form->getAttribute('id'); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $id |
|
169
|
|
|
* @return $this |
|
170
|
|
|
*/ |
|
171
|
2 |
|
public function setId($id) |
|
172
|
|
|
{ |
|
173
|
2 |
|
$this->form->setAttribute('id', $id); |
|
174
|
2 |
|
return $this; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param $encType |
|
179
|
|
|
* @return $this |
|
180
|
|
|
*/ |
|
181
|
2 |
|
public function setEncType($encType) |
|
182
|
|
|
{ |
|
183
|
2 |
|
$this->form->setAttribute('enctype', $encType); |
|
184
|
2 |
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
1 |
|
public function getEncType() |
|
191
|
|
|
{ |
|
192
|
1 |
|
return $this->form->getAttribute('enctype'); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* @param string $method |
|
197
|
|
|
* @return FormInterface |
|
198
|
|
|
*/ |
|
199
|
2 |
|
public function setMethod($method) |
|
200
|
|
|
{ |
|
201
|
2 |
|
$this->form->setAttribute('method', $method); |
|
202
|
2 |
|
return $this; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return string |
|
207
|
|
|
*/ |
|
208
|
1 |
|
public function getMethod() |
|
209
|
|
|
{ |
|
210
|
1 |
|
return $this->form->getAttribute('method'); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param $class |
|
215
|
|
|
* @return FormInterface |
|
216
|
|
|
*/ |
|
217
|
2 |
|
public function setClass($class) |
|
218
|
|
|
{ |
|
219
|
2 |
|
$this->form->setAttribute('class', $class); |
|
220
|
2 |
|
return $this; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @return string |
|
225
|
|
|
*/ |
|
226
|
2 |
|
public function getClass() |
|
227
|
|
|
{ |
|
228
|
2 |
|
return $this->form->getAttribute('class'); |
|
229
|
|
|
} |
|
230
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..