Completed
Push — unit-tests-conditions ( 195e3f...91d90b )
by Romain
02:19
created

AbstractConditionItem::throwInvalidConditionException()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
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\Condition\Items;
15
16
use Romm\ConfigurationObject\Traits\ConfigurationObject\MagicMethodsTrait;
17
use Romm\Formz\Condition\Exceptions\InvalidConditionException;
18
use Romm\Formz\Condition\Parser\Node\ConditionNode;
19
use Romm\Formz\Configuration\Form\Condition\Activation\ActivationInterface;
20
use Romm\Formz\Configuration\Form\Field\Field;
21
use Romm\Formz\Configuration\Form\Field\Validation\Validation;
22
use Romm\Formz\Form\FormObject;
23
use Romm\Formz\Service\ArrayService;
24
25
/**
26
 * This class must be extended by every registered condition item. When it is
27
 * registered, a condition can then be used in the TypoScript configuration for
28
 * fields/validation activation rules.
29
 *
30
 * When you want to create a new condition item, first register it by using the
31
 * function `ConditionFactory::registerConditionType()` inside your
32
 * `ext_localconf.php`. Then, you have to implement the three abstract functions
33
 * of this class:
34
 * - `getCssResult()`
35
 * - `getJavaScriptResult()`
36
 * - `getPhpResult()`
37
 *
38
 * These functions must translate the "meaning" of this condition to the three
39
 * context: CSS, JavaScript and PHP.
40
 *
41
 * If you need more explanation about how this class works, please refer to the
42
 * documentation.
43
 *
44
 * @see \Romm\Formz\Condition\ConditionFactory
45
 * @see \Romm\Formz\Condition\Items\FieldHasValueCondition
46
 * @see \Romm\Formz\Condition\Items\FieldHasErrorCondition
47
 * @see \Romm\Formz\Condition\Items\FieldIsValidCondition
48
 */
49
abstract class AbstractConditionItem implements ConditionItemInterface
50
{
51
    use MagicMethodsTrait;
52
53
    /**
54
     * Contains a list of JavaScript files which will be included whenever this
55
     * condition is used.
56
     *
57
     * Example:
58
     * protected static $javaScriptFiles = [
59
     *     'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldHasValue.js'
60
     * ];
61
     *
62
     * @var array
63
     */
64
    protected static $javaScriptFiles = [];
65
66
    /**
67
     * @var FormObject
68
     * @disableMagicMethods
69
     */
70
    protected $formObject;
71
72
    /**
73
     * @var ActivationInterface
74
     * @disableMagicMethods
75
     */
76
    protected $activation;
77
78
    /**
79
     * @var ConditionNode
80
     * @disableMagicMethods
81
     */
82
    protected $conditionNode;
83
84
    /**
85
     * Will launch the condition validation: the child class should implement
86
     * the function `checkConditionConfiguration()` and check if the condition
87
     * can be considered as valid.
88
     *
89
     * If any syntax/configuration error is found, an exception of type
90
     * `InvalidConditionException` must be thrown.
91
     *
92
     * @throws InvalidConditionException
93
     * @return bool
94
     */
95
    final public function validateConditionConfiguration()
96
    {
97
        try {
98
            $this->checkConditionConfiguration();
99
        } catch (InvalidConditionException $exception) {
100
            $this->throwInvalidConditionException($exception);
101
        }
102
103
        return true;
104
    }
105
106
    /**
107
     * @see validateConditionConfiguration()
108
     *
109
     * @throws InvalidConditionException
110
     * @return bool
111
     */
112
    abstract protected function checkConditionConfiguration();
113
114
    /**
115
     * Returns a generic JavaScript code which uses FormZ API to validate a
116
     * condition which was registered in a single JavaScript file (which is
117
     * filled in the `$javaScriptFiles` attribute of the PHP condition class).
118
     *
119
     * @param array $data
120
     * @return string
121
     */
122
    protected function getDefaultJavaScriptCall(array $data)
123
    {
124
        $conditionName = addslashes(get_class($this));
125
        $data = ArrayService::get()->arrayToJavaScriptJson($data);
126
127
        return <<<JS
128
Formz.Condition.validateCondition('$conditionName', form, $data)
129
JS;
130
    }
131
132
    /**
133
     * @return array
134
     */
135
    public function getJavaScriptFiles()
136
    {
137
        return static::$javaScriptFiles;
138
    }
139
140
    /**
141
     * @param InvalidConditionException $exception
142
     * @throws InvalidConditionException
143
     */
144
    protected function throwInvalidConditionException(InvalidConditionException $exception)
145
    {
146
        $rootObject = $this->activation->getRootObject();
147
        $conditionName = $this->conditionNode->getConditionName();
148
        $formClassName = $this->formObject->getClassName();
149
150
        if ($rootObject instanceof Field) {
151
            throw InvalidConditionException::invalidFieldConditionConfiguration($conditionName, $rootObject, $formClassName, $exception);
152
        } elseif ($rootObject instanceof Validation) {
153
            throw InvalidConditionException::invalidValidationConditionConfiguration($conditionName, $rootObject, $formClassName, $exception);
154
        }
155
    }
156
157
    /**
158
     * @param FormObject $formObject
159
     */
160
    public function attachFormObject(FormObject $formObject)
161
    {
162
        $this->formObject = $formObject;
163
    }
164
165
    /**
166
     * @param ActivationInterface $activation
167
     */
168
    public function attachActivation(ActivationInterface $activation)
169
    {
170
        $this->activation = $activation;
171
    }
172
173
    /**
174
     * @param ConditionNode $conditionNode
175
     */
176
    public function attachConditionNode(ConditionNode $conditionNode)
177
    {
178
        $this->conditionNode = $conditionNode;
179
    }
180
181
    /**
182
     * @return array
183
     */
184
    public function __sleep()
185
    {
186
        $properties = get_object_vars($this);
187
        unset($properties['formObject']);
188
        unset($properties['activation']);
189
        unset($properties['conditionNode']);
190
191
        return array_keys($properties);
192
    }
193
}
194