Completed
Push — feature/improve-form-definitio... ( bf8e8a...4622d4 )
by Romain
10:50
created

Activation::setExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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