Completed
Push — feature/version-2 ( 6dfc1b...4671c0 )
by Romain
02:10
created

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