Completed
Push — feature/improve-form-definitio... ( 1e2db7...454aaa )
by Romain
02:37
created

Activation::getRootObject()   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\Form\Definition\Condition;
15
16
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessor;
17
use Romm\ConfigurationObject\Service\Items\DataPreProcessor\DataPreProcessorInterface;
18
use Romm\Formz\Condition\ConditionFactory;
19
use Romm\Formz\Condition\Items\ConditionItemInterface;
20
use Romm\Formz\Exceptions\DuplicateEntryException;
21
use Romm\Formz\Exceptions\EntryNotFoundException;
22
use Romm\Formz\Form\Definition\AbstractFormDefinitionComponent;
23
use Romm\Formz\Form\Definition\FormDefinition;
24
use TYPO3\CMS\Core\Utility\ArrayUtility;
25
use TYPO3\CMS\Extbase\Error\Error;
26
27
class Activation extends AbstractFormDefinitionComponent implements ActivationInterface, DataPreProcessorInterface
28
{
29
    /**
30
     * @var string
31
     * @validate NotEmpty
32
     */
33
    protected $expression;
34
35
    /**
36
     * @var \Romm\Formz\Condition\Items\ConditionItemInterface[]
37
     * @mixedTypesResolver \Romm\Formz\Form\Definition\Condition\ConditionItemResolver
38
     */
39
    protected $conditions = [];
40
41
    /**
42
     * @return string
43
     */
44
    public function getExpression()
45
    {
46
        return $this->expression;
47
    }
48
49
    /**
50
     * @param string $expression
51
     */
52
    public function setExpression($expression)
53
    {
54
        $this->checkDefinitionFreezeState();
55
56
        $this->expression = $expression;
57
    }
58
59
    /**
60
     * @return ConditionItemInterface[]
61
     */
62
    public function getConditions()
63
    {
64
        return $this->conditions;
65
    }
66
67
    /**
68
     * Returns the merged list of the conditions of this object and the
69
     * conditions of the parent form.
70
     *
71
     * @return ConditionItemInterface[]
72
     */
73
    public function getAllConditions()
74
    {
75
        $conditionList = $this->withFirstParent(
76
            FormDefinition::class,
77
            function (FormDefinition $formConfiguration) {
78
                return $formConfiguration->getConditionList();
79
            }
80
        );
81
82
        $conditionList = ($conditionList) ?: [];
83
        ArrayUtility::mergeRecursiveWithOverrule($conditionList, $this->conditions);
84
85
        return $conditionList;
86
    }
87
88
    /**
89
     * @param string $name Name of the condition.
90
     * @return bool
91
     */
92
    public function hasCondition($name)
93
    {
94
        $conditions = $this->getAllConditions();
95
96
        return true === isset($conditions[$name]);
97
    }
98
99
    /**
100
     * Return the condition with the given name.
101
     *
102
     * @param string $name Name of the item.
103
     * @return ConditionItemInterface
104
     * @throws EntryNotFoundException
105
     */
106
    public function getCondition($name)
107
    {
108
        if (false === $this->hasCondition($name)) {
109
            throw EntryNotFoundException::activationConditionNotFound($name);
110
        }
111
112
        $items = $this->getAllConditions();
113
114
        return $items[$name];
115
    }
116
117
    /**
118
     * @param string $name
119
     * @param string $identifier
120
     * @param array  $arguments
121
     * @return ConditionItemInterface
122
     * @throws DuplicateEntryException
123
     * @throws EntryNotFoundException
124
     */
125
    public function addCondition($name, $identifier, $arguments = [])
126
    {
127
        $this->checkDefinitionFreezeState();
128
129
        if (true === isset($this->conditions[$name])) {
130
            throw DuplicateEntryException::activationConditionAlreadyAdded($name);
131
        }
132
133
        $conditionFactory = ConditionFactory::get();
134
135
        if (false === $conditionFactory->hasCondition($identifier)) {
136
            throw EntryNotFoundException::activationAddConditionNotFound($identifier, $conditionFactory->getConditions());
137
        }
138
139
        $condition = $conditionFactory->instantiateCondition($identifier, $arguments);
140
        $condition->attachParent($this);
141
        $this->conditions[$name] = $condition;
142
143
        return $condition;
144
    }
145
146
    /**
147
     * @return ActivationUsageInterface
148
     */
149
    public function getRootObject()
150
    {
151
        /** @var ActivationUsageInterface $rootObject */
152
        $rootObject = $this->getFirstParent(ActivationUsageInterface::class);
153
154
        return $rootObject;
155
    }
156
157
    /**
158
     * @param DataPreProcessor $processor
159
     */
160
    public static function dataPreProcessor(DataPreProcessor $processor)
161
    {
162
        $data = $processor->getData();
163
164
        if (isset($data['condition'])) {
165
            $error = new Error(
166
                'The property "condition" has been deprecated and renamed to "expression", please change your TypoScript configuration.',
167
                1488483802
168
            );
169
            $processor->addError($error);
170
        }
171
        if (isset($data['items'])) {
172
            $error = new Error(
173
                'The property "items" has been deprecated and renamed to "conditions", please change your TypoScript configuration.',
174
                1488531112
175
            );
176
            $processor->addError($error);
177
        }
178
    }
179
}
180