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 validate() |
100
|
|
|
{ |
101
|
|
|
if(!$this->validator){ |
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if(null === $this->object){ |
106
|
|
|
throw new \InvalidArgumentException('Object not bind'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->validator->isValid($this->object, $_REQUEST); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param null|\ValidatorInterface $validator |
114
|
|
|
* |
115
|
|
|
* @return Form |
116
|
|
|
*/ |
117
|
|
|
protected function setValidator(ValidatorInterface $validator) |
118
|
|
|
{ |
119
|
|
|
$this->validator = $validator; |
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @inheritDoc |
126
|
|
|
*/ |
127
|
|
|
public function bind($object) |
128
|
|
|
{ |
129
|
|
|
foreach($this->elements as $element){ |
130
|
|
|
$name = $this->camelCase($element->getName()); |
131
|
|
|
$methodName = 'get'.$name; |
132
|
|
|
if(!method_exists($object, $methodName)){ |
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$element->setValue($object->{$methodName}()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
$this->object = $object; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @inheritDoc |
146
|
|
|
*/ |
147
|
|
|
public function setData(array $data) |
148
|
|
|
{ |
149
|
|
|
foreach($data as $fieldName => $currentData){ |
150
|
|
|
if(!key_exists($fieldName, $this->elements)){ |
151
|
|
|
continue; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->elements[$fieldName]->setValue($currentData); |
155
|
|
|
|
156
|
|
|
$methodName = 'set'.$this->camelCase($fieldName); |
157
|
|
|
if(null === $this->object || !method_exists($this->object, $methodName)){ |
158
|
|
|
continue; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$this->object->{$fieldName} = $currentData; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @inheritDoc |
169
|
|
|
*/ |
170
|
|
|
public function getObject() |
171
|
|
|
{ |
172
|
|
|
return $this->object; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @inheritDoc |
177
|
|
|
*/ |
178
|
|
|
public function getElement($elementName) |
179
|
|
|
{ |
180
|
|
|
if(!key_exists($elementName, $this->elements)){ |
181
|
|
|
throw new \InvalidArgumentException(sprintf('Element %s not found ', $elementName)); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $this->elements[$elementName]; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param string $name |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
private function camelCase($name) |
193
|
|
|
{ |
194
|
|
|
$str = str_replace('_', '', ucwords($name, '-')); |
195
|
|
|
return lcfirst($str); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
} |