Completed
Push — feature/version-2 ( 4671c0...cfd51e )
by Romain
02:15
created

AbstractActivation::getAllConditions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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