Completed
Push — feature/version-2 ( 61b6a8...2f0885 )
by Romain
03:48 queued 01:55
created

AbstractActivation::dataPreProcessor()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 4
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\Parents\ParentsTrait;
17
use Romm\Formz\Condition\Items\ConditionItemInterface;
18
use Romm\Formz\Configuration\AbstractFormzConfiguration;
19
use Romm\Formz\Exceptions\EntryNotFoundException;
20
use Romm\Formz\Form\Definition\FormDefinition;
21
use TYPO3\CMS\Core\Utility\ArrayUtility;
22
23
abstract class AbstractActivation extends AbstractFormzConfiguration implements ActivationInterface
24
{
25
    use ParentsTrait;
26
27
    /**
28
     * @var string
29
     */
30
    protected $expression;
31
32
    /**
33
     * @var \Romm\Formz\Condition\Items\ConditionItemInterface[]
34
     * @mixedTypesResolver \Romm\Formz\Form\Definition\Condition\ConditionItemResolver
35
     */
36
    protected $conditions = [];
37
38
    /**
39
     * @var ActivationUsageInterface
40
     */
41
    private $rootObject;
42
43
    /**
44
     * @return string
45
     */
46
    public function getExpression()
47
    {
48
        return $this->expression;
49
    }
50
51
    /**
52
     * @param string $expression
53
     */
54
    public function setExpression($expression)
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
     * Will merge the conditions with the condition list of the parent form.
69
     *
70
     * @return ConditionItemInterface[]
71
     */
72
    public function getAllConditions()
73
    {
74
        $conditionList = $this->withFirstParent(
75
            FormDefinition::class,
76
            function (FormDefinition $formConfiguration) {
77
                return $formConfiguration->getConditionList();
78
            }
79
        );
80
81
        $conditionList = ($conditionList) ?: [];
82
        ArrayUtility::mergeRecursiveWithOverrule($conditionList, $this->conditions);
83
84
        return $conditionList;
85
    }
86
87
    /**
88
     * @param string $name Name of the condition.
89
     * @return bool
90
     */
91
    public function hasCondition($name)
92
    {
93
        $conditions = $this->getAllConditions();
94
95
        return true === isset($conditions[$name]);
96
    }
97
98
    /**
99
     * Return the condition with the given name.
100
     *
101
     * @param string $name Name of the item.
102
     * @return ConditionItemInterface
103
     * @throws EntryNotFoundException
104
     */
105
    public function getCondition($name)
106
    {
107
        if (false === $this->hasCondition($name)) {
108
            throw EntryNotFoundException::activationConditionNotFound($name);
109
        }
110
111
        $items = $this->getAllConditions();
112
113
        return $items[$name];
114
    }
115
116
    /**
117
     * @param string                 $name
118
     * @param ConditionItemInterface $condition
119
     */
120
    public function addCondition($name, ConditionItemInterface $condition)
121
    {
122
        $this->conditions[$name] = $condition;
123
    }
124
125
    /**
126
     * @return ActivationUsageInterface
127
     */
128
    public function getRootObject()
129
    {
130
        return $this->rootObject;
131
    }
132
133
    /**
134
     * @param ActivationUsageInterface $rootObject
135
     */
136
    public function setRootObject(ActivationUsageInterface $rootObject)
137
    {
138
        $this->rootObject = $rootObject;
139
    }
140
}
141