1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Linio\DynamicFormBundle\Form; |
4
|
|
|
|
5
|
|
|
use Linio\DynamicFormBundle\DataProvider; |
6
|
|
|
use Linio\DynamicFormBundle\HelpMessageProvider; |
7
|
|
|
use Linio\DynamicFormBundle\Exception\NonExistentFormException; |
8
|
|
|
use Linio\DynamicFormBundle\Exception\NotExistentDataProviderException; |
9
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
10
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
11
|
|
|
use Symfony\Component\Form\FormFactory as SymfonyFormFactory; |
12
|
|
|
use Symfony\Component\Form\FormInterface; |
13
|
|
|
use Symfony\Component\Validator\Validation; |
14
|
|
|
|
15
|
|
|
class FormFactory |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var SymfonyFormFactory |
19
|
|
|
*/ |
20
|
|
|
protected $formFactory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $configuration; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var DataProvider[] |
29
|
|
|
*/ |
30
|
|
|
protected $dataProviders = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $eventSubscribers = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var HelpMessageProvider[] |
39
|
|
|
*/ |
40
|
|
|
protected $helpMessageProviders = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param SymfonyFormFactory $formFactory |
44
|
|
|
*/ |
45
|
|
|
public function setFormFactory(SymfonyFormFactory $formFactory) |
46
|
|
|
{ |
47
|
|
|
$this->formFactory = $formFactory; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $configuration |
52
|
|
|
*/ |
53
|
|
|
public function setConfiguration(array $configuration) |
54
|
|
|
{ |
55
|
|
|
$this->configuration = $configuration; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param string $alias |
60
|
|
|
* @param DataProvider $dataProvider |
61
|
|
|
*/ |
62
|
|
|
public function addDataProvider($alias, DataProvider $dataProvider) |
63
|
|
|
{ |
64
|
|
|
$this->dataProviders[$alias] = $dataProvider; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $alias |
69
|
|
|
* @param HelpMessageProvider $helpMessageProvider |
70
|
|
|
*/ |
71
|
|
|
public function addHelpMessageProvider($alias, HelpMessageProvider $helpMessageProvider) |
72
|
|
|
{ |
73
|
|
|
$this->helpMessageProviders[$alias] = $helpMessageProvider; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $formName |
78
|
|
|
* @param EventSubscriberInterface $eventSubscriber |
79
|
|
|
*/ |
80
|
|
|
public function addEventSubscriber($formName, EventSubscriberInterface $eventSubscriber) |
81
|
|
|
{ |
82
|
|
|
if (!isset($this->eventSubscribers[$formName])) { |
83
|
|
|
$this->eventSubscribers[$formName] = []; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->eventSubscribers[$formName][] = $eventSubscriber; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $key The key of the Form in the form configuration |
91
|
|
|
* @param array $data |
92
|
|
|
* @param array $options |
93
|
|
|
* @param string $name An name for the form. If empty, the key will be used |
94
|
|
|
* |
95
|
|
|
* @return FormInterface |
96
|
|
|
*/ |
97
|
|
|
public function createForm($key, $data = [], $options = [], $name = null) |
98
|
|
|
{ |
99
|
|
|
return $this->createBuilder($key, $data, $options, $name)->getForm(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* This method generates a form based on the configuration file. |
104
|
|
|
* |
105
|
|
|
* @param string $key The key of the Form in the form configuration |
106
|
|
|
* @param array $data |
107
|
|
|
* @param array $options |
108
|
|
|
* @param string $name An name for the form. If empty, the key will be used |
109
|
|
|
* |
110
|
|
|
* @return FormBuilderInterface |
111
|
|
|
* |
112
|
|
|
* @throws NonExistentFormException |
113
|
|
|
*/ |
114
|
|
|
public function createBuilder($key, $data = [], $options = [], $name = null) |
115
|
|
|
{ |
116
|
|
|
if (!isset($this->configuration[$key])) { |
117
|
|
|
throw new NonExistentFormException(sprintf('The form "%s" was not found.', $key)); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$formBuilder = $this->formFactory->createNamedBuilder($name ?: $key, 'form', $data, $options); |
121
|
|
|
|
122
|
|
|
if (isset($this->eventSubscribers[$key])) { |
123
|
|
|
foreach ($this->eventSubscribers[$key] as $eventSubscriber) { |
124
|
|
|
$formBuilder->addEventSubscriber($eventSubscriber); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
foreach ($this->configuration[$key] as $key => $fieldConfiguration) { |
129
|
|
|
if (!$fieldConfiguration['enabled']) { |
130
|
|
|
continue; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$fieldOptions = isset($fieldConfiguration['options']) ? $fieldConfiguration['options'] : []; |
134
|
|
|
|
135
|
|
|
if (isset($fieldConfiguration['data_provider'])) { |
136
|
|
|
$fieldOptions['choices'] = $this->loadDataProvider($fieldConfiguration['data_provider'])->getData(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (isset($fieldConfiguration['help_message_provider'])) { |
140
|
|
|
$fieldOptions['help'] = $this->loadHelpMessageProvider($fieldConfiguration['help_message_provider'])->getHelpMessage($fieldOptions['help']); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (isset($fieldConfiguration['validation'])) { |
144
|
|
|
$constraints = []; |
145
|
|
|
|
146
|
|
|
foreach ($fieldConfiguration['validation'] as $validatorName => $options) { |
147
|
|
|
$constraints[] = new $validatorName($options); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$fieldOptions['constraints'] = $constraints; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$field = $formBuilder->create($key, $fieldConfiguration['type'], $fieldOptions); |
154
|
|
|
|
155
|
|
|
if (isset($fieldConfiguration['transformer'])) { |
156
|
|
|
$transformerConfiguration = $fieldConfiguration['transformer']; |
157
|
|
|
$transformer = new $transformerConfiguration['class'](); |
158
|
|
|
|
159
|
|
|
if (isset($transformerConfiguration['calls'])) { |
160
|
|
|
foreach ($transformerConfiguration['calls'] as $call) { |
161
|
|
|
call_user_func([$transformer, $call[0]], $call[1]); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
$field->addModelTransformer($transformer); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$formBuilder->add($field); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $formBuilder; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* This method generates a validator based on the configuration file. |
176
|
|
|
* |
177
|
|
|
* @param string $key The key of the Form in the form configuration |
178
|
|
|
* @param mixed $object Object which is the target of the validator |
179
|
|
|
* |
180
|
|
|
* @return ValidatorInterface |
181
|
|
|
* |
182
|
|
|
* @throws NonExistentFormException |
183
|
|
|
*/ |
184
|
|
|
public function createValidator($key, $object) |
185
|
|
|
{ |
186
|
|
|
if (!isset($this->configuration[$key])) { |
187
|
|
|
throw new NonExistentFormException(sprintf('The form "%s" was not found.', $key)); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
$validator = Validation::createValidatorBuilder()->getValidator(); |
191
|
|
|
$metadata = $validator->getMetadataFor(get_class($object)); |
192
|
|
|
|
193
|
|
|
foreach ($this->configuration[$key] as $key => $fieldConfiguration) { |
194
|
|
|
if (!$fieldConfiguration['enabled']) { |
195
|
|
|
continue; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if (!isset($fieldConfiguration['validation'])) { |
199
|
|
|
continue; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
foreach ($fieldConfiguration['validation'] as $validatorName => $options) { |
203
|
|
|
$metadata->addPropertyConstraint($key, new $validatorName($options)); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $validator; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param string $alias |
212
|
|
|
* |
213
|
|
|
* @return DataProvider |
214
|
|
|
* |
215
|
|
|
* @throws NotExistentDataProviderException |
216
|
|
|
*/ |
217
|
|
|
public function loadDataProvider($alias) |
218
|
|
|
{ |
219
|
|
|
if (!isset($this->dataProviders[$alias])) { |
220
|
|
|
throw new NotExistentDataProviderException(); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $this->dataProviders[$alias]; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param string $alias |
228
|
|
|
* |
229
|
|
|
* @return HelpMessageProvider |
230
|
|
|
* |
231
|
|
|
* @throws NotExistentDataProviderException |
232
|
|
|
*/ |
233
|
|
|
public function loadHelpMessageProvider($alias) |
234
|
|
|
{ |
235
|
|
|
if (!isset($this->helpMessageProviders[$alias])) { |
236
|
|
|
throw new NotExistentDataProviderException(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $this->helpMessageProviders[$alias]; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string $name |
244
|
|
|
* |
245
|
|
|
* @return array |
246
|
|
|
* |
247
|
|
|
* @throws NonExistentFormException |
248
|
|
|
*/ |
249
|
|
|
public function getConfiguration($name = null) |
250
|
|
|
{ |
251
|
|
|
if ($name === null) { |
252
|
|
|
return $this->configuration; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
if (!$this->has($name)) { |
256
|
|
|
throw new NonExistentFormException(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
return $this->configuration[$name]; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Checks if a given form exists. |
264
|
|
|
* |
265
|
|
|
* @param string $name |
266
|
|
|
* |
267
|
|
|
* @return bool |
268
|
|
|
*/ |
269
|
|
|
public function has($name) |
270
|
|
|
{ |
271
|
|
|
return isset($this->configuration[$name]); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|