Completed
Push — feature/version-2 ( a353c5...4aa9b8 )
by Romain
19:54 queued 12:50
created

FormDefinition   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 11

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 11
dl 0
loc 137
rs 10
c 0
b 0
f 0

11 Methods

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