Completed
Push — feature/improve-form-definitio... ( 432218...49be60 )
by Romain
02:28
created

FormDefinition::createCondition()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
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\Settings\FormSettings;
29
use TYPO3\CMS\Core\Utility\GeneralUtility;
30
use TYPO3\CMS\Extbase\Validation\Error;
31
32
class FormDefinition extends AbstractFormDefinition implements ConfigurationObjectInterface, DataPreProcessorInterface
33
{
34
    use DefaultConfigurationObjectTrait;
35
    use ArrayConversionTrait;
36
37
    /**
38
     * @var \Romm\Formz\Form\Definition\Field\Field[]
39
     * @validate NotEmpty
40
     */
41
    protected $fields = [];
42
43
    /**
44
     * @var \Romm\Formz\Condition\Items\ConditionItemInterface[]
45
     * @mixedTypesResolver \Romm\Formz\Form\Definition\Condition\ConditionItemResolver
46
     */
47
    protected $conditionList = [];
48
49
    /**
50
     * @var \Romm\Formz\Form\Definition\Settings\FormSettings
51
     */
52
    protected $settings;
53
54
    /**
55
     * @var FormDefinitionState
56
     */
57
    private $state;
58
59
    /**
60
     * Constructor.
61
     */
62
    public function __construct()
63
    {
64
        $this->settings = GeneralUtility::makeInstance(FormSettings::class);
65
        $this->state = GeneralUtility::makeInstance(FormDefinitionState::class);
66
    }
67
68
    /**
69
     * Will initialize correctly the configuration object settings.
70
     *
71
     * @return ServiceFactory
72
     */
73
    public static function getConfigurationObjectServices()
74
    {
75
        return Configuration::getConfigurationObjectServices();
76
    }
77
78
    /**
79
     * Returns FormZ root configuration object.
80
     *
81
     * @return Configuration
82
     */
83
    public function getRootConfiguration()
84
    {
85
        /** @var Configuration $configuration */
86
        $configuration = $this->getFirstParent(Configuration::class);
87
88
        return $configuration;
89
    }
90
91
    /**
92
     * @return Field[]
93
     */
94
    public function getFields()
95
    {
96
        return $this->fields;
97
    }
98
99
    /**
100
     * @param string $name
101
     * @return bool
102
     */
103
    public function hasField($name)
104
    {
105
        return true === isset($this->fields[$name]);
106
    }
107
108
    /**
109
     * @param string $name
110
     * @return Field
111
     * @throws EntryNotFoundException
112
     */
113
    public function getField($name)
114
    {
115
        if (false === $this->hasField($name)) {
116
            throw EntryNotFoundException::configurationFieldNotFound($name);
117
        }
118
119
        return $this->fields[$name];
120
    }
121
122
    /**
123
     * @param string $name
124
     * @return Field
125
     * @throws DuplicateEntryException
126
     */
127
    public function addField($name)
128
    {
129
        $this->checkDefinitionFreezeState();
130
131
        if ($this->hasField($name)) {
132
            throw DuplicateEntryException::fieldAlreadyAdded($name);
133
        }
134
135
        /** @var Field $field */
136
        $field = GeneralUtility::makeInstance(Field::class, $name);
137
        $field->attachParent($this);
138
139
        $this->fields[$name] = $field;
140
141
        return $field;
142
    }
143
144
    /**
145
     * @return ConditionItemInterface[]
146
     */
147
    public function getConditionList()
148
    {
149
        return $this->conditionList;
150
    }
151
152
    /**
153
     * @param string $name
154
     * @return bool
155
     */
156
    public function hasCondition($name)
157
    {
158
        return true === isset($this->conditionList[$name]);
159
    }
160
161
    /**
162
     * @param string $name
163
     * @return ConditionItemInterface
164
     * @throws EntryNotFoundException
165
     */
166
    public function getCondition($name)
167
    {
168
        if (false === $this->hasCondition($name)) {
169
            throw EntryNotFoundException::conditionNotFoundInDefinition($name);
170
        }
171
172
        return $this->conditionList[$name];
173
    }
174
175
    /**
176
     * @param string $name
177
     * @param string $identifier
178
     * @param array  $arguments
179
     * @return ConditionItemInterface
180
     */
181
    public function addCondition($name, $identifier, array $arguments = [])
182
    {
183
        $this->checkDefinitionFreezeState();
184
185
        if ($this->hasCondition($name)) {
186
            throw DuplicateEntryException::formConditionAlreadyAdded($name);
187
        }
188
189
        $this->conditionList[$name] = $this->createCondition($identifier, $arguments);
190
191
        return $this->conditionList[$name];
192
    }
193
194
    /**
195
     * @param string $identifier
196
     * @param array  $arguments
197
     * @return ConditionItemInterface
198
     * @throws EntryNotFoundException
199
     */
200
    protected function createCondition($identifier, array $arguments = [])
201
    {
202
        $conditionFactory = ConditionFactory::get();
203
204
        if (false === $conditionFactory->hasCondition($identifier)) {
205
            throw EntryNotFoundException::formAddConditionNotFound($identifier, $conditionFactory->getConditions());
206
        }
207
208
        $condition = $conditionFactory->instantiateCondition($identifier, $arguments);
209
        $condition->attachParent($this);
210
211
        return $condition;
212
    }
213
214
    /**
215
     * @return FormSettings
216
     */
217
    public function getSettings()
218
    {
219
        return $this->settings;
220
    }
221
222
    /**
223
     * @return FormDefinitionState
224
     */
225
    public function getState()
226
    {
227
        return $this->state;
228
    }
229
230
    /**
231
     * @param DataPreProcessor $processor
232
     */
233
    public static function dataPreProcessor(DataPreProcessor $processor)
234
    {
235
        $data = $processor->getData();
236
237
        /*
238
         * Forcing the names of the fields: they are the keys of the array
239
         * entries.
240
         */
241
        foreach ($data['fields'] as $key => $field) {
242
            $data['fields'][$key]['name'] = $key;
243
        }
244
245
        $processor->setData($data);
246
247
        if (isset($data['activationCondition'])) {
248
            $error = new Error(
249
                'The property "activationCondition" has been deprecated and renamed to "conditionList", please change your TypoScript configuration.',
250
                1489763042
251
            );
252
            $processor->addError($error);
253
        }
254
    }
255
}
256