Completed
Push — middleware-wip ( fcef97...7414b2 )
by Romain
02:36
created

Form::dataPreProcessor()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 18
rs 9.4285
cc 3
eloc 10
nc 4
nop 1
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\Configuration\Form;
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\Items\Parents\ParentsTrait;
20
use Romm\ConfigurationObject\Service\ServiceFactory;
21
use Romm\ConfigurationObject\Traits\ConfigurationObject\ArrayConversionTrait;
22
use Romm\ConfigurationObject\Traits\ConfigurationObject\DefaultConfigurationObjectTrait;
23
use Romm\ConfigurationObject\Traits\ConfigurationObject\StoreArrayIndexTrait;
24
use Romm\Formz\Condition\Items\ConditionItemInterface;
25
use Romm\Formz\Configuration\AbstractFormzConfiguration;
26
use Romm\Formz\Configuration\Configuration;
27
use Romm\Formz\Configuration\Form\Field\Field;
28
use Romm\Formz\Configuration\Form\Middleware\PresetMiddlewares;
29
use Romm\Formz\Configuration\Form\Settings\FormSettings;
30
use Romm\Formz\Configuration\Form\Step\Step;
31
use Romm\Formz\Middleware\MiddlewareInterface;
32
use Romm\Formz\Exceptions\EntryNotFoundException;
33
use Romm\Formz\Persistence\PersistenceInterface;
34
use TYPO3\CMS\Core\Utility\GeneralUtility;
35
use TYPO3\CMS\Extbase\Validation\Error;
36
37
class Form extends AbstractFormzConfiguration implements ConfigurationObjectInterface, DataPreProcessorInterface
38
{
39
    use DefaultConfigurationObjectTrait;
40
    use StoreArrayIndexTrait;
41
    use ParentsTrait;
42
    use ArrayConversionTrait;
43
44
    /**
45
     * @var \Romm\Formz\Configuration\Form\Field\Field[]
46
     * @validate NotEmpty
47
     */
48
    protected $fields = [];
49
50
    /**
51
     * @var \Romm\Formz\Condition\Items\ConditionItemInterface[]
52
     * @mixedTypesResolver \Romm\Formz\Configuration\Form\Condition\ConditionItemResolver
53
     */
54
    protected $conditionList = [];
55
56
    /**
57
     * @var \Romm\Formz\Configuration\Form\Settings\FormSettings
58
     */
59
    protected $settings;
60
61
    /**
62
     * @var \Romm\Formz\Configuration\Form\Middleware\PresetMiddlewares
63
     */
64
    protected $presetMiddlewares;
65
66
    /**
67
     * @var \Romm\Formz\Middleware\MiddlewareInterface[]
68
     * @mixedTypesResolver \Romm\Formz\Configuration\Form\Middleware\MiddlewareResolver
69
     */
70
    protected $middlewares = [];
71
72
    /**
73
     * @var \Romm\Formz\Persistence\PersistenceInterface[]
74
     * @mixedTypesResolver \Romm\Formz\Configuration\Form\Persistence\PersistenceResolver
75
     */
76
    protected $persistence;
77
78
    /**
79
     * @var \Romm\Formz\Configuration\Form\Step\Step
80
     */
81
    protected $step;
82
83
    /**
84
     * Constructor.
85
     */
86
    public function __construct()
87
    {
88
        $this->settings = GeneralUtility::makeInstance(FormSettings::class);
89
    }
90
91
    /**
92
     * Will initialize correctly the configuration object settings.
93
     *
94
     * @return ServiceFactory
95
     */
96
    public static function getConfigurationObjectServices()
97
    {
98
        return Configuration::getConfigurationObjectServices();
99
    }
100
101
    /**
102
     * Returns FormZ root configuration object.
103
     *
104
     * @return Configuration
105
     */
106
    public function getRootConfiguration()
107
    {
108
        /** @var Configuration $configuration */
109
        $configuration = $this->getFirstParent(Configuration::class);
110
111
        return $configuration;
112
    }
113
114
    /**
115
     * @return Field[]
116
     */
117
    public function getFields()
118
    {
119
        return $this->fields;
120
    }
121
122
    /**
123
     * @param string $name
124
     * @return bool
125
     */
126
    public function hasField($name)
127
    {
128
        return true === isset($this->fields[$name]);
129
    }
130
131
    /**
132
     * @param string $name
133
     * @return Field
134
     * @throws EntryNotFoundException
135
     */
136
    public function getField($name)
137
    {
138
        if (false === $this->hasField($name)) {
139
            throw EntryNotFoundException::configurationFieldNotFound($name);
140
        }
141
142
        return $this->fields[$name];
143
    }
144
145
    /**
146
     * @param Field $field
147
     */
148
    public function addField(Field $field)
149
    {
150
        $field->setParents([$this]);
151
152
        $this->fields[$field->getName()] = $field;
153
    }
154
155
    /**
156
     * @return ConditionItemInterface[]
157
     */
158
    public function getConditionList()
159
    {
160
        return $this->conditionList;
161
    }
162
163
    /**
164
     * @param string                 $name
165
     * @param ConditionItemInterface $condition
166
     */
167
    public function addCondition($name, ConditionItemInterface $condition)
168
    {
169
        $this->conditionList[$name] = $condition;
170
    }
171
172
    /**
173
     * @return FormSettings
174
     */
175
    public function getSettings()
176
    {
177
        return $this->settings;
178
    }
179
180
    /**
181
     * @return PresetMiddlewares
182
     */
183
    public function getPresetMiddlewares()
184
    {
185
        return $this->presetMiddlewares;
186
    }
187
188
    /**
189
     * @return MiddlewareInterface[]
190
     */
191
    public function getMiddlewares()
192
    {
193
        return $this->middlewares;
194
    }
195
196
    /**
197
     * @param string $name
198
     * @return MiddlewareInterface
199
     * @throws EntryNotFoundException
200
     */
201
    public function getMiddleware($name)
202
    {
203
        if (false === $this->hasMiddleware($name)) {
204
            throw EntryNotFoundException::middlewareNotFound($name);
205
        }
206
207
        return $this->middlewares[$name];
208
    }
209
210
    /**
211
     * @param string $name
212
     * @return bool
213
     */
214
    public function hasMiddleware($name)
215
    {
216
        return isset($this->middlewares[$name]);
217
    }
218
219
    /**
220
     * Returns the merged list of preset middlewares and custom registered
221
     * middlewares.
222
     *
223
     * @return MiddlewareInterface[]
224
     */
225
    public function getAllMiddlewares()
226
    {
227
        $middlewaresList = $this->middlewares;
228
229
        foreach ($this->presetMiddlewares->getList() as $name => $middleware) {
230
            $middlewaresList['__preset-' . $name] = $middleware;
231
        }
232
233
        return $middlewaresList;
234
    }
235
236
    /**
237
     * @return bool
238
     */
239
    public function hasPersistence()
240
    {
241
        return false === empty($this->persistence);
242
    }
243
244
    /**
245
     * @return PersistenceInterface[]
246
     */
247
    public function getPersistence()
248
    {
249
        return $this->persistence;
250
    }
251
252
    /**
253
     * @return Step
254
     */
255
    public function getStep()
256
    {
257
        return $this->step;
258
    }
259
260
    /**
261
     * @return bool
262
     */
263
    public function hasSteps()
264
    {
265
        return $this->step instanceof Step;
266
    }
267
268
    /**
269
     * @param DataPreProcessor $processor
270
     */
271
    public static function dataPreProcessor(DataPreProcessor $processor)
272
    {
273
        $data = $processor->getData();
274
275
        if (false === isset($data['presetMiddlewares'])) {
276
            $data['presetMiddlewares'] = [];
277
        }
278
279
        if (isset($data['activationCondition'])) {
280
            $error = new Error(
281
                'The property "activationCondition" has been deprecated and renamed to "conditionList", please change your TypoScript configuration.',
282
                1489763042
283
            );
284
            $processor->addError($error);
285
        }
286
287
        $processor->setData($data);
288
    }
289
}
290