Completed
Push — middleware-wip ( 73dcf0...f9e0fc )
by Romain
02:47
created

Form::getRootConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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