|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* 2017 Romain CANON <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
|
7
|
|
|
* under the terms of the GNU General Public License, either |
|
8
|
|
|
* version 3 of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, see: |
|
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Romm\Formz\Form\Definition; |
|
15
|
|
|
|
|
16
|
|
|
use Romm\ConfigurationObject\ConfigurationObjectInterface; |
|
17
|
|
|
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor; |
|
18
|
|
|
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface; |
|
19
|
|
|
use Romm\ConfigurationObject\Service\ServiceFactory; |
|
20
|
|
|
use Romm\ConfigurationObject\Traits\ConfigurationObject\ArrayConversionTrait; |
|
21
|
|
|
use Romm\ConfigurationObject\Traits\ConfigurationObject\DefaultConfigurationObjectTrait; |
|
22
|
|
|
use Romm\Formz\Condition\ConditionFactory; |
|
23
|
|
|
use Romm\Formz\Condition\Items\ConditionItemInterface; |
|
24
|
|
|
use Romm\Formz\Configuration\Configuration; |
|
25
|
|
|
use Romm\Formz\Exceptions\DuplicateEntryException; |
|
26
|
|
|
use Romm\Formz\Exceptions\EntryNotFoundException; |
|
27
|
|
|
use Romm\Formz\Form\Definition\Field\Field; |
|
28
|
|
|
use Romm\Formz\Form\Definition\Middleware\PresetMiddlewares; |
|
29
|
|
|
use Romm\Formz\Form\Definition\Settings\FormSettings; |
|
30
|
|
|
use Romm\Formz\Form\Definition\Step\Steps; |
|
31
|
|
|
use Romm\Formz\Middleware\MiddlewareInterface; |
|
32
|
|
|
use Romm\Formz\Persistence\PersistenceInterface; |
|
33
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
34
|
|
|
|
|
35
|
|
|
class FormDefinition extends AbstractFormDefinitionComponent implements ConfigurationObjectInterface, DataPreProcessorInterface |
|
36
|
|
|
{ |
|
37
|
|
|
use DefaultConfigurationObjectTrait; |
|
38
|
|
|
use ArrayConversionTrait; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var \Romm\Formz\Form\Definition\Field\Field[] |
|
42
|
|
|
* @validate NotEmpty |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $fields = []; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var \Romm\Formz\Condition\Items\ConditionItemInterface[] |
|
48
|
|
|
* @mixedTypesResolver \Romm\Formz\Form\Definition\Condition\ConditionItemResolver |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $conditionList = []; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var \Romm\Formz\Form\Definition\Settings\FormSettings |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $settings; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var \Romm\Formz\Form\Definition\Middleware\PresetMiddlewares |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $presetMiddlewares; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var \Romm\Formz\Middleware\MiddlewareInterface[] |
|
64
|
|
|
* @mixedTypesResolver \Romm\Formz\Form\Definition\Middleware\MiddlewareResolver |
|
65
|
|
|
*/ |
|
66
|
|
|
protected $middlewares = []; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @var \Romm\Formz\Persistence\PersistenceInterface[] |
|
70
|
|
|
* @mixedTypesResolver \Romm\Formz\Form\Definition\Persistence\PersistenceResolver |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $persistence; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var \Romm\Formz\Form\Definition\Step\Steps |
|
76
|
|
|
*/ |
|
77
|
|
|
protected $steps; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @var FormDefinitionState |
|
81
|
|
|
*/ |
|
82
|
|
|
private $state; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Constructor. |
|
86
|
|
|
*/ |
|
87
|
|
|
public function __construct() |
|
88
|
|
|
{ |
|
89
|
|
|
$this->settings = GeneralUtility::makeInstance(FormSettings::class); |
|
90
|
|
|
$this->state = GeneralUtility::makeInstance(FormDefinitionState::class); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Will initialize correctly the configuration object settings. |
|
95
|
|
|
* |
|
96
|
|
|
* @return ServiceFactory |
|
97
|
|
|
*/ |
|
98
|
|
|
public static function getConfigurationObjectServices() |
|
99
|
|
|
{ |
|
100
|
|
|
return Configuration::getConfigurationObjectServices(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Returns FormZ root configuration object. |
|
105
|
|
|
* |
|
106
|
|
|
* @return Configuration |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getRootConfiguration() |
|
109
|
|
|
{ |
|
110
|
|
|
/** @var Configuration $configuration */ |
|
111
|
|
|
$configuration = $this->getFirstParent(Configuration::class); |
|
112
|
|
|
|
|
113
|
|
|
return $configuration; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return Field[] |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getFields() |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->fields; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $name |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
|
public function hasField($name) |
|
129
|
|
|
{ |
|
130
|
|
|
return true === isset($this->fields[$name]); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $name |
|
135
|
|
|
* @return Field |
|
136
|
|
|
* @throws EntryNotFoundException |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getField($name) |
|
139
|
|
|
{ |
|
140
|
|
|
if (false === $this->hasField($name)) { |
|
141
|
|
|
throw EntryNotFoundException::configurationFieldNotFound($name); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $this->fields[$name]; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param string $name |
|
149
|
|
|
* @return Field |
|
150
|
|
|
* @throws DuplicateEntryException |
|
151
|
|
|
*/ |
|
152
|
|
|
public function addField($name) |
|
153
|
|
|
{ |
|
154
|
|
|
$this->checkDefinitionFreezeState(); |
|
155
|
|
|
|
|
156
|
|
|
if ($this->hasField($name)) { |
|
157
|
|
|
throw DuplicateEntryException::fieldAlreadyAdded($name); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** @var Field $field */ |
|
161
|
|
|
$field = GeneralUtility::makeInstance(Field::class, $name); |
|
162
|
|
|
$field->attachParent($this); |
|
163
|
|
|
|
|
164
|
|
|
$this->fields[$name] = $field; |
|
165
|
|
|
|
|
166
|
|
|
return $field; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @return ConditionItemInterface[] |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getConditionList() |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->conditionList; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param string $name |
|
179
|
|
|
* @return bool |
|
180
|
|
|
*/ |
|
181
|
|
|
public function hasCondition($name) |
|
182
|
|
|
{ |
|
183
|
|
|
return true === isset($this->conditionList[$name]); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param string $name |
|
188
|
|
|
* @return ConditionItemInterface |
|
189
|
|
|
* @throws EntryNotFoundException |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getCondition($name) |
|
192
|
|
|
{ |
|
193
|
|
|
if (false === $this->hasCondition($name)) { |
|
194
|
|
|
throw EntryNotFoundException::conditionNotFoundInDefinition($name); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $this->conditionList[$name]; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param string $name |
|
202
|
|
|
* @param string $identifier |
|
203
|
|
|
* @param array $arguments |
|
204
|
|
|
* @return ConditionItemInterface |
|
205
|
|
|
* @throws DuplicateEntryException |
|
206
|
|
|
*/ |
|
207
|
|
|
public function addCondition($name, $identifier, array $arguments = []) |
|
208
|
|
|
{ |
|
209
|
|
|
$this->checkDefinitionFreezeState(); |
|
210
|
|
|
|
|
211
|
|
|
if ($this->hasCondition($name)) { |
|
212
|
|
|
throw DuplicateEntryException::formConditionAlreadyAdded($name); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$this->conditionList[$name] = $this->createCondition($identifier, $arguments); |
|
216
|
|
|
|
|
217
|
|
|
return $this->conditionList[$name]; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @param string $identifier |
|
222
|
|
|
* @param array $arguments |
|
223
|
|
|
* @return ConditionItemInterface |
|
224
|
|
|
* @throws EntryNotFoundException |
|
225
|
|
|
*/ |
|
226
|
|
|
protected function createCondition($identifier, array $arguments = []) |
|
227
|
|
|
{ |
|
228
|
|
|
$conditionFactory = ConditionFactory::get(); |
|
229
|
|
|
|
|
230
|
|
|
if (false === $conditionFactory->hasCondition($identifier)) { |
|
231
|
|
|
throw EntryNotFoundException::formAddConditionNotFound($identifier, $conditionFactory->getConditions()); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
$condition = $conditionFactory->instantiateCondition($identifier, $arguments); |
|
235
|
|
|
$condition->attachParent($this); |
|
236
|
|
|
|
|
237
|
|
|
return $condition; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @return FormSettings |
|
242
|
|
|
*/ |
|
243
|
|
|
public function getSettings() |
|
244
|
|
|
{ |
|
245
|
|
|
return $this->settings; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @return PresetMiddlewares |
|
250
|
|
|
*/ |
|
251
|
|
|
public function getPresetMiddlewares() |
|
252
|
|
|
{ |
|
253
|
|
|
return $this->presetMiddlewares; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @return MiddlewareInterface[] |
|
258
|
|
|
*/ |
|
259
|
|
|
public function getMiddlewares() |
|
260
|
|
|
{ |
|
261
|
|
|
return $this->middlewares; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @param string $name |
|
266
|
|
|
* @return MiddlewareInterface |
|
267
|
|
|
* @throws EntryNotFoundException |
|
268
|
|
|
*/ |
|
269
|
|
|
public function getMiddleware($name) |
|
270
|
|
|
{ |
|
271
|
|
|
if (false === $this->hasMiddleware($name)) { |
|
272
|
|
|
throw EntryNotFoundException::middlewareNotFound($name); |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
return $this->middlewares[$name]; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @param string $name |
|
280
|
|
|
* @return bool |
|
281
|
|
|
*/ |
|
282
|
|
|
public function hasMiddleware($name) |
|
283
|
|
|
{ |
|
284
|
|
|
return isset($this->middlewares[$name]); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Returns the merged list of preset middlewares and custom registered |
|
289
|
|
|
* middlewares. |
|
290
|
|
|
* |
|
291
|
|
|
* @return MiddlewareInterface[] |
|
292
|
|
|
*/ |
|
293
|
|
|
public function getAllMiddlewares() |
|
294
|
|
|
{ |
|
295
|
|
|
$middlewaresList = $this->middlewares; |
|
296
|
|
|
|
|
297
|
|
|
foreach ($this->presetMiddlewares->getList() as $name => $middleware) { |
|
298
|
|
|
$middlewaresList['__preset-' . $name] = $middleware; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
return $middlewaresList; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* @return bool |
|
306
|
|
|
*/ |
|
307
|
|
|
public function hasPersistence() |
|
308
|
|
|
{ |
|
309
|
|
|
return false === empty($this->persistence); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
/** |
|
313
|
|
|
* @return PersistenceInterface[] |
|
314
|
|
|
*/ |
|
315
|
|
|
public function getPersistence() |
|
316
|
|
|
{ |
|
317
|
|
|
return $this->persistence; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* @return Steps |
|
322
|
|
|
*/ |
|
323
|
|
|
public function getSteps() |
|
324
|
|
|
{ |
|
325
|
|
|
return $this->steps; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* @return bool |
|
330
|
|
|
*/ |
|
331
|
|
|
public function hasSteps() |
|
332
|
|
|
{ |
|
333
|
|
|
return $this->steps instanceof Steps; |
|
334
|
|
|
} |
|
335
|
|
|
|
|
336
|
|
|
/** |
|
337
|
|
|
* @return FormDefinitionState |
|
338
|
|
|
*/ |
|
339
|
|
|
public function getState() |
|
340
|
|
|
{ |
|
341
|
|
|
return $this->state; |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
/** |
|
345
|
|
|
* @param DataPreProcessor $processor |
|
346
|
|
|
*/ |
|
347
|
|
|
public static function dataPreProcessor(DataPreProcessor $processor) |
|
348
|
|
|
{ |
|
349
|
|
|
$data = $processor->getData(); |
|
350
|
|
|
|
|
351
|
|
|
if (false === isset($data['presetMiddlewares'])) { |
|
352
|
|
|
$data['presetMiddlewares'] = []; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/* |
|
356
|
|
|
* Forcing the names of the fields: they are the keys of the array |
|
357
|
|
|
* entries. |
|
358
|
|
|
*/ |
|
359
|
|
|
self::forceNameForProperty($data, 'fields'); |
|
360
|
|
|
|
|
361
|
|
|
$processor->setData($data); |
|
362
|
|
|
} |
|
363
|
|
|
} |
|
364
|
|
|
|