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