Completed
Push — middleware-wip ( 668c38...d5b291 )
by Romain
02:54
created

Form::hasMiddleware()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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
     */
200
    public function getMiddleware($name)
201
    {
202
        if (false === $this->hasMiddleware($name)) {
203
            throw new \Exception('todo'); // @todo
204
        }
205
206
        return $this->middlewares[$name];
207
    }
208
209
    /**
210
     * @param string $name
211
     * @return bool
212
     */
213
    public function hasMiddleware($name)
214
    {
215
        return isset($this->middlewares[$name]);
216
    }
217
218
    /**
219
     * Returns the merged list of preset middlewares and custom registered
220
     * middlewares.
221
     *
222
     * @return MiddlewareInterface[]
223
     */
224
    public function getAllMiddlewares()
225
    {
226
        $middlewaresList = $this->middlewares;
227
228
        foreach ($this->presetMiddlewares->getList() as $name => $middleware) {
229
            $middlewaresList['__preset-' . $name] = $middleware;
230
        }
231
232
        return $middlewaresList;
233
    }
234
235
    /**
236
     * @return bool
237
     */
238
    public function hasPersistence()
239
    {
240
        return false === empty($this->persistence);
241
    }
242
243
    /**
244
     * @return PersistenceInterface[]
245
     */
246
    public function getPersistence()
247
    {
248
        return $this->persistence;
249
    }
250
251
    /**
252
     * @return bool
253
     */
254
    public function hasStep()
255
    {
256
        return null !== $this->step;
257
    }
258
259
    /**
260
     * @return Step
261
     */
262
    public function getStep()
263
    {
264
        // @todo check has
265
266
        return $this->step;
267
    }
268
269
    /**
270
     * @param DataPreProcessor $processor
271
     */
272
    public static function dataPreProcessor(DataPreProcessor $processor)
273
    {
274
        $data = $processor->getData();
275
276
        if (false === isset($data['presetMiddlewares'])) {
277
            $data['presetMiddlewares'] = [];
278
        }
279
280
        if (isset($data['activationCondition'])) {
281
            $error = new Error(
282
                'The property "activationCondition" has been deprecated and renamed to "conditionList", please change your TypoScript configuration.',
283
                1489763042
284
            );
285
            $processor->addError($error);
286
        }
287
288
        $processor->setData($data);
289
    }
290
}
291