Completed
Push — middleware-wip ( 1bbe3a...71b03c )
by Romain
02:47
created

Form::getPresetMiddlewares()   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 0
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\Middleware\MiddlewareInterface;
31
use Romm\Formz\Exceptions\EntryNotFoundException;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Extbase\Validation\Error;
34
35
class Form extends AbstractFormzConfiguration implements ConfigurationObjectInterface, DataPreProcessorInterface
36
{
37
    use DefaultConfigurationObjectTrait;
38
    use StoreArrayIndexTrait;
39
    use ParentsTrait;
40
    use ArrayConversionTrait;
41
42
    /**
43
     * @var \Romm\Formz\Configuration\Form\Field\Field[]
44
     * @validate NotEmpty
45
     */
46
    protected $fields = [];
47
48
    /**
49
     * @var \Romm\Formz\Condition\Items\ConditionItemInterface[]
50
     * @mixedTypesResolver \Romm\Formz\Configuration\Form\Condition\ConditionItemResolver
51
     */
52
    protected $conditionList = [];
53
54
    /**
55
     * @var \Romm\Formz\Configuration\Form\Settings\FormSettings
56
     */
57
    protected $settings;
58
59
    /**
60
     * @var \Romm\Formz\Configuration\Form\Middleware\PresetMiddlewares
61
     */
62
    protected $presetMiddlewares;
63
64
    /**
65
     * @var \Romm\Formz\Middleware\MiddlewareInterface[]
66
     * @mixedTypesResolver \Romm\Formz\Configuration\Form\Middleware\MiddlewareResolver
67
     */
68
    protected $middlewares = [];
69
70
    /**
71
     * Constructor.
72
     */
73
    public function __construct()
74
    {
75
        $this->settings = GeneralUtility::makeInstance(FormSettings::class);
76
    }
77
78
    /**
79
     * Will initialize correctly the configuration object settings.
80
     *
81
     * @return ServiceFactory
82
     */
83
    public static function getConfigurationObjectServices()
84
    {
85
        return Configuration::getConfigurationObjectServices();
86
    }
87
88
    /**
89
     * Returns FormZ root configuration object.
90
     *
91
     * @return Configuration
92
     */
93
    public function getRootConfiguration()
94
    {
95
        /** @var Configuration $configuration */
96
        $configuration = $this->getFirstParent(Configuration::class);
97
98
        return $configuration;
99
    }
100
101
    /**
102
     * @return Field[]
103
     */
104
    public function getFields()
105
    {
106
        return $this->fields;
107
    }
108
109
    /**
110
     * @param string $name
111
     * @return bool
112
     */
113
    public function hasField($name)
114
    {
115
        return true === isset($this->fields[$name]);
116
    }
117
118
    /**
119
     * @param string $name
120
     * @return Field
121
     * @throws EntryNotFoundException
122
     */
123
    public function getField($name)
124
    {
125
        if (false === $this->hasField($name)) {
126
            throw EntryNotFoundException::configurationFieldNotFound($name);
127
        }
128
129
        return $this->fields[$name];
130
    }
131
132
    /**
133
     * @param Field $field
134
     */
135
    public function addField(Field $field)
136
    {
137
        $field->setParents([$this]);
138
139
        $this->fields[$field->getName()] = $field;
140
    }
141
142
    /**
143
     * @return ConditionItemInterface[]
144
     */
145
    public function getConditionList()
146
    {
147
        return $this->conditionList;
148
    }
149
150
    /**
151
     * @param string                 $name
152
     * @param ConditionItemInterface $condition
153
     */
154
    public function addCondition($name, ConditionItemInterface $condition)
155
    {
156
        $this->conditionList[$name] = $condition;
157
    }
158
159
    /**
160
     * @return FormSettings
161
     */
162
    public function getSettings()
163
    {
164
        return $this->settings;
165
    }
166
167
    /**
168
     * @return PresetMiddlewares
169
     */
170
    public function getPresetMiddlewares()
171
    {
172
        return $this->presetMiddlewares;
173
    }
174
175
    /**
176
     * Returns the merges list of default middlewares and custom registered
177
     * middlewares.
178
     *
179
     * @return MiddlewareInterface[]
180
     */
181
    public function getMiddlewares()
182
    {
183
        $middlewaresList = $this->middlewares;
184
185
        foreach ($this->presetMiddlewares->getList() as $name => $middleware) {
186
            $middlewaresList['__preset-' . $name] = $middleware;
187
        }
188
189
        return $middlewaresList;
190
    }
191
192
    /**
193
     * @param DataPreProcessor $processor
194
     */
195
    public static function dataPreProcessor(DataPreProcessor $processor)
196
    {
197
        $data = $processor->getData();
198
199
        if (false === isset($data['presetMiddlewares'])) {
200
            $data['presetMiddlewares'] = [];
201
        }
202
203
        if (isset($data['activationCondition'])) {
204
            $error = new Error(
205
                'The property "activationCondition" has been deprecated and renamed to "conditionList", please change your TypoScript configuration.',
206
                1489763042
207
            );
208
            $processor->addError($error);
209
        }
210
211
        $processor->setData($data);
212
    }
213
}
214