Completed
Push — feature/improve-form-definitio... ( 8ffff6...a3c595 )
by Romain
03:04
created

FormDefinition::addCondition()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 11
nc 3
nop 3
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
     * @throws DuplicateEntryException
181
     * @throws EntryNotFoundException
182
     */
183
    public function addCondition($name, $identifier, $arguments = [])
184
    {
185
        $this->checkDefinitionFreezeState();
186
187
        if ($this->hasCondition($name)) {
188
            throw DuplicateEntryException::conditionAlreadyAdded($name);
189
        }
190
191
        $conditionFactory = ConditionFactory::get();
192
193
        if (false === $conditionFactory->hasCondition($identifier)) {
194
            throw EntryNotFoundException::addConditionNotFound($identifier, $conditionFactory->getConditions());
195
        }
196
197
        $condition = $conditionFactory->instantiateCondition($identifier, $arguments);
198
        $condition->attachParent($this);
199
        $this->conditionList[$name] = $condition;
200
201
        return $condition;
202
    }
203
204
    /**
205
     * @return FormSettings
206
     */
207
    public function getSettings()
208
    {
209
        return $this->settings;
210
    }
211
212
    /**
213
     * @return FormDefinitionState
214
     */
215
    public function getState()
216
    {
217
        return $this->state;
218
    }
219
220
    /**
221
     * @param DataPreProcessor $processor
222
     */
223
    public static function dataPreProcessor(DataPreProcessor $processor)
224
    {
225
        $data = $processor->getData();
226
227
        /*
228
         * Forcing the names of the fields: they are the keys of the array
229
         * entries.
230
         */
231
        foreach ($data['fields'] as $key => $field) {
232
            $data['fields'][$key]['name'] = $key;
233
        }
234
235
        $processor->setData($data);
236
237
        if (isset($data['activationCondition'])) {
238
            $error = new Error(
239
                'The property "activationCondition" has been deprecated and renamed to "conditionList", please change your TypoScript configuration.',
240
                1489763042
241
            );
242
            $processor->addError($error);
243
        }
244
    }
245
}
246