Completed
Push — unit-tests-conditions ( dc9eee...e8c03e )
by Romain
02:37
created

AbstractActivation::setExpression()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\Configuration\Form\Condition\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\Configuration\Form\Form;
22
use Romm\Formz\Exceptions\EntryNotFoundException;
23
use TYPO3\CMS\Extbase\Error\Error;
24
use TYPO3\CMS\Extbase\Utility\ArrayUtility;
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 \ArrayObject<Romm\Formz\Configuration\Form\Condition\ConditionItemResolver>
37
     */
38
    protected $conditions = [];
39
40
    /**
41
     * @var ActivationUsageInterface
42
     */
43
    private $rootObject;
44
45
    /**
46
     * @return string
47
     */
48
    public function getExpression()
49
    {
50
        return $this->expression;
51
    }
52
53
    /**
54
     * @param string $expression
55
     */
56
    public function setExpression($expression)
57
    {
58
        $this->expression = $expression;
59
    }
60
61
    /**
62
     * Will merge the items with the ones from the `$activationCondition`
63
     * property of the root form configuration.
64
     *
65
     * @return ConditionItemInterface[]
66
     */
67
    public function getConditions()
68
    {
69
        $activationCondition = $this->withFirstParent(
70
            Form::class,
71
            function (Form $formConfiguration) {
72
                return $formConfiguration->getActivationCondition();
73
            }
74
        );
75
        $activationCondition = ($activationCondition) ?: [];
76
77
        return ArrayUtility::arrayMergeRecursiveOverrule($activationCondition, $this->conditions);
0 ignored issues
show
Deprecated Code introduced by
The method TYPO3\CMS\Extbase\Utilit...ergeRecursiveOverrule() has been deprecated with message: since TYPO3 v8, will be removed in TYPO3 v9, use array_replace_recursive() instead if possible, other see the ArrayUtility in EXT:core

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
78
    }
79
80
    /**
81
     * @param string $name Name of the item.
82
     * @return bool
83
     */
84
    public function hasCondition($name)
85
    {
86
        $conditions = $this->getConditions();
87
88
        return true === isset($conditions[$name]);
89
    }
90
91
    /**
92
     * Return the item with the given name.
93
     *
94
     * @param string $name Name of the item.
95
     * @return ConditionItemInterface
96
     * @throws EntryNotFoundException
97
     */
98
    public function getCondition($name)
99
    {
100
        if (false === $this->hasCondition($name)) {
101
            throw new EntryNotFoundException(
102
                'No condition "' . $name . '" was found.',
103
                1488482191
104
            );
105
        }
106
107
        $items = $this->getConditions();
108
109
        return $items[$name];
110
    }
111
112
    /**
113
     * @param string                 $name
114
     * @param ConditionItemInterface $condition
115
     */
116
    public function addCondition($name, ConditionItemInterface $condition)
117
    {
118
        $this->conditions[$name] = $condition;
119
    }
120
121
    /**
122
     * @return ActivationUsageInterface
123
     */
124
    public function getRootObject()
125
    {
126
        return $this->rootObject;
127
    }
128
129
    /**
130
     * @param ActivationUsageInterface $rootObject
131
     */
132
    public function setRootObject(ActivationUsageInterface $rootObject)
133
    {
134
        $this->rootObject = $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