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