Completed
Push — middleware-wip ( 5cfd03...f2f782 )
by Romain
05:53
created

ConditionNode::__sleep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
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\Condition\Parser\Node;
15
16
use Romm\Formz\Condition\Exceptions\InvalidConditionException;
17
use Romm\Formz\Condition\Items\ConditionItemInterface;
18
use Romm\Formz\Condition\Processor\ConditionProcessor;
19
use Romm\Formz\Condition\Processor\DataObject\PhpConditionDataObject;
20
use Romm\Formz\Form\Definition\Field\Activation\ActivationInterface;
21
use Romm\Formz\Form\Definition\Field\Activation\ActivationUsageInterface;
22
use Romm\Formz\Form\Definition\Field\Field;
23
use Romm\Formz\Form\Definition\Field\Validation\Validator;
24
use Romm\Formz\Form\FormObject\FormObject;
25
26
/**
27
 * A condition node, which contains an instance of `ConditionItemInterface`.
28
 */
29
class ConditionNode extends AbstractNode implements ActivationDependencyAwareInterface
30
{
31
    /**
32
     * @var string
33
     */
34
    protected $conditionName;
35
36
    /**
37
     * @var ConditionItemInterface
38
     */
39
    protected $condition;
40
41
    /**
42
     * @var FormObject
43
     */
44
    protected $formObject;
45
46
    /**
47
     * @var ActivationInterface
48
     */
49
    protected $activation;
50
51
    /**
52
     * Constructor, which needs a name for the condition and an instance of a
53
     * condition item.
54
     *
55
     * @param string                 $conditionName Name of the condition.
56
     * @param ConditionItemInterface $condition     Instance of the condition item.
57
     */
58
    public function __construct($conditionName, ConditionItemInterface $condition)
59
    {
60
        $this->conditionName = $conditionName;
61
        $this->condition = $condition;
62
    }
63
64
    /**
65
     * @param ConditionProcessor  $processor
66
     * @param ActivationInterface $activation
67
     */
68
    public function injectDependencies(ConditionProcessor $processor, ActivationInterface $activation)
69
    {
70
        $this->formObject = $processor->getFormObject();
71
        $this->activation = $activation;
72
73
        $this->condition->attachFormObject($this->formObject);
74
        $this->condition->attachActivation($activation);
75
        $this->condition->attachConditionNode($this);
76
    }
77
78
    /**
79
     * @inheritdoc
80
     */
81
    public function getCssResult()
82
    {
83
        return $this->toArray($this->condition->getCssResult());
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    public function getJavaScriptResult()
90
    {
91
        return $this->toArray($this->condition->getJavaScriptResult());
92
    }
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function getPhpResult(PhpConditionDataObject $dataObject)
98
    {
99
        $this->checkConditionConfiguration();
100
101
        return $this->condition->getPhpResult($dataObject);
102
    }
103
104
    /**
105
     * Validates the configuration of the condition instance.
106
     *
107
     * @see \Romm\Formz\Condition\Items\ConditionItemInterface::validateConditionConfiguration
108
     */
109
    protected function checkConditionConfiguration()
110
    {
111
        try {
112
            $definition = $this->getFormObject()->getDefinition();
113
114
            $this->condition->validateConditionConfiguration($definition);
115
        } catch (InvalidConditionException $exception) {
116
            $this->throwInvalidConditionException($exception);
117
        }
118
    }
119
120
    /**
121
     * @param InvalidConditionException $exception
122
     * @throws InvalidConditionException
123
     */
124
    protected function throwInvalidConditionException(InvalidConditionException $exception)
125
    {
126
        $rootObject = $this->getRootObject();
127
        $conditionName = $this->getConditionName();
128
        $formClassName = $this->getFormObject()->getClassName();
129
130
        if ($rootObject instanceof Field) {
131
            throw InvalidConditionException::invalidFieldConditionConfiguration($conditionName, $rootObject, $formClassName, $exception);
132
        } elseif ($rootObject instanceof Validator) {
133
            throw InvalidConditionException::invalidValidationConditionConfiguration($conditionName, $rootObject, $formClassName, $exception);
134
        } else {
135
            throw $exception;
136
        }
137
    }
138
139
    /**
140
     * @return string
141
     */
142
    public function getConditionName()
143
    {
144
        return $this->conditionName;
145
    }
146
147
    /**
148
     * @return ConditionItemInterface
149
     */
150
    public function getCondition()
151
    {
152
        return $this->condition;
153
    }
154
155
    public function __sleep()
156
    {
157
        $properties = get_object_vars($this);
158
159
        unset($properties['formObject']);
160
        unset($properties['activation']);
161
162
        return array_keys($properties);
163
    }
164
165
    /**
166
     * @return FormObject
167
     */
168
    protected function getFormObject()
169
    {
170
        return $this->formObject;
171
    }
172
173
    /**
174
     * @return ActivationUsageInterface
175
     */
176
    protected function getRootObject()
177
    {
178
        return $this->activation->getRootObject();
179
    }
180
}
181