1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace flightlog\form; |
7
|
|
|
|
8
|
|
|
use ValidatorInterface; |
9
|
|
|
use Webmozart\Assert\Assert; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @author Laurent De Coninck <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
abstract class Form implements FormInterface |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $name; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $method; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
private $options; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \stdClass|null |
34
|
|
|
*/ |
35
|
|
|
private $object; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var null|\ValidatorInterface |
39
|
|
|
*/ |
40
|
|
|
private $validator; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array|FormElementInterface[] |
44
|
|
|
*/ |
45
|
|
|
private $elements; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Form constructor. |
49
|
|
|
* |
50
|
|
|
* @param string $name |
51
|
|
|
* @param string $method |
52
|
|
|
* @param array $options |
53
|
|
|
*/ |
54
|
|
|
public function __construct($name, $method = FormInterface::METHOD_GET, array $options = []) |
55
|
|
|
{ |
56
|
|
|
$this->name = $name; |
57
|
|
|
$this->method = $method; |
58
|
|
|
$this->options = $options; |
59
|
|
|
$this->elements = []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @inheritDoc |
64
|
|
|
*/ |
65
|
|
|
public function getName() |
66
|
|
|
{ |
67
|
|
|
return $this->name; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritDoc |
72
|
|
|
*/ |
73
|
|
|
public function getMethod() |
74
|
|
|
{ |
75
|
|
|
return $this->method; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
|
|
public function getOptions(){ |
82
|
|
|
return $this->options; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritDoc |
87
|
|
|
*/ |
88
|
|
|
public function add(FormElementInterface $element) |
89
|
|
|
{ |
90
|
|
|
Assert::keyNotExists($this->elements, $element->getName(), 'Element already exists'); |
91
|
|
|
$this->elements[$element->getName()] = $element; |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @inheritDoc |
98
|
|
|
*/ |
99
|
|
|
public function remove($fieldName) |
100
|
|
|
{ |
101
|
|
|
unset($this->elements[$fieldName]); |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritDoc |
107
|
|
|
*/ |
108
|
|
|
public function has($fieldName) |
109
|
|
|
{ |
110
|
|
|
return isset($this->elements[$fieldName]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritDoc |
115
|
|
|
*/ |
116
|
|
|
public function validate() |
117
|
|
|
{ |
118
|
|
|
if(!$this->validator){ |
119
|
|
|
return true; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if(null === $this->object){ |
123
|
|
|
throw new \InvalidArgumentException('Object not bound'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$validation = $this->validator->isValid($this->object, $_REQUEST); |
127
|
|
|
|
128
|
|
|
if(!$validation){ |
129
|
|
|
foreach($this->elements as $fieldName => $field){ |
130
|
|
|
$field->setErrors($this->validator->getError($fieldName)); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $validation; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return array|string[] |
139
|
|
|
*/ |
140
|
|
|
public function getErrorMessages(){ |
141
|
|
|
return $this->validator->getErrors(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param null|\ValidatorInterface $validator |
146
|
|
|
* |
147
|
|
|
* @return Form |
148
|
|
|
*/ |
149
|
|
|
protected function setValidator(ValidatorInterface $validator) |
150
|
|
|
{ |
151
|
|
|
$this->validator = $validator; |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @inheritDoc |
158
|
|
|
*/ |
159
|
|
|
public function bind($object) |
160
|
|
|
{ |
161
|
|
|
foreach($this->elements as $element){ |
162
|
|
|
$name = $this->camelCase($element->getName()); |
163
|
|
|
$methodName = 'get'.$name; |
164
|
|
|
if(!method_exists($object, $methodName)){ |
165
|
|
|
continue; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$element->setValue($object->{$methodName}()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$this->object = $object; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @inheritDoc |
178
|
|
|
*/ |
179
|
|
|
public function setData(array $data) |
180
|
|
|
{ |
181
|
|
|
foreach($data as $fieldName => $currentData){ |
182
|
|
|
if(!key_exists($fieldName, $this->elements) || $this->elements[$fieldName]->isDisabled()){ |
183
|
|
|
continue; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$this->elements[$fieldName]->setValue($currentData); |
187
|
|
|
|
188
|
|
|
$methodName = 'set'.$this->camelCase($fieldName); |
189
|
|
|
if(null === $this->object){ |
190
|
|
|
continue; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if(method_exists($this->object, $methodName)){ |
194
|
|
|
$this->object->{$methodName}($currentData); |
195
|
|
|
continue; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if(property_exists($this->object, $fieldName)){ |
199
|
|
|
$this->object->{$fieldName} = $currentData; |
200
|
|
|
continue; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $this; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @inheritDoc |
209
|
|
|
*/ |
210
|
|
|
public function getObject() |
211
|
|
|
{ |
212
|
|
|
return $this->object; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @inheritDoc |
217
|
|
|
*/ |
218
|
|
|
public function getElement($elementName) |
219
|
|
|
{ |
220
|
|
|
if(!key_exists($elementName, $this->elements)){ |
221
|
|
|
throw new \InvalidArgumentException(sprintf('Element %s not found ', $elementName)); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $this->elements[$elementName]; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param string $name |
229
|
|
|
* |
230
|
|
|
* @return string |
231
|
|
|
*/ |
232
|
|
|
private function camelCase($name) |
233
|
|
|
{ |
234
|
|
|
$str = str_replace('_', '', ucwords($name, '-')); |
235
|
|
|
return lcfirst($str); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
} |