Completed
Push — task/configuration-unit-tests ( b40345...73923e )
by Romain
02:34
created

AbstractActivation::getExpression()   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 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\Configuration\Form\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\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 ConditionItemInterface[]
37
     * @mixedTypesResolver \Romm\Formz\Configuration\Form\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
     * Will merge the items with the ones from the `$activationCondition`
64
     * property of the root form configuration.
65
     *
66
     * @return ConditionItemInterface[]
67
     */
68
    public function getConditions()
69
    {
70
        $activationCondition = $this->withFirstParent(
71
            Form::class,
72
            function (Form $formConfiguration) {
73
                return $formConfiguration->getActivationCondition();
74
            }
75
        );
76
        $activationCondition = ($activationCondition) ?: [];
77
78
        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...
79
    }
80
81
    /**
82
     * @param string $name Name of the item.
83
     * @return bool
84
     */
85
    public function hasCondition($name)
86
    {
87
        $conditions = $this->getConditions();
88
89
        return true === isset($conditions[$name]);
90
    }
91
92
    /**
93
     * Return the item with the given name.
94
     *
95
     * @param string $name Name of the item.
96
     * @return ConditionItemInterface
97
     * @throws EntryNotFoundException
98
     */
99
    public function getCondition($name)
100
    {
101
        if (false === $this->hasCondition($name)) {
102
            throw EntryNotFoundException::conditionNotFound($name);
103
        }
104
105
        $items = $this->getConditions();
106
107
        return $items[$name];
108
    }
109
110
    /**
111
     * @param string                 $name
112
     * @param ConditionItemInterface $condition
113
     */
114
    public function addCondition($name, ConditionItemInterface $condition)
115
    {
116
        $this->conditions[$name] = $condition;
117
    }
118
119
    /**
120
     * @return ActivationUsageInterface
121
     */
122
    public function getRootObject()
123
    {
124
        return $this->rootObject;
125
    }
126
127
    /**
128
     * @param ActivationUsageInterface $rootObject
129
     */
130
    public function setRootObject(ActivationUsageInterface $rootObject)
131
    {
132
        $this->rootObject = $rootObject;
133
    }
134
135
    /**
136
     * @param DataPreProcessor $processor
137
     */
138
    public static function dataPreProcessor(DataPreProcessor $processor)
139
    {
140
        $data = $processor->getData();
141
142
        if (isset($data['condition'])) {
143
            $error = new Error(
144
                'The property "condition" has been deprecated and renamed to "expression", please change your TypoScript configuration.',
145
                1488483802
146
            );
147
            $processor->addError($error);
148
        }
149
        if (isset($data['items'])) {
150
            $error = new Error(
151
                'The property "items" has been deprecated and renamed to "conditions", please change your TypoScript configuration.',
152
                1488531112
153
            );
154
            $processor->addError($error);
155
        }
156
    }
157
}
158