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\Processor; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Condition\Parser\ConditionParserFactory; |
17
|
|
|
use Romm\Formz\Condition\Parser\Tree\ConditionTree; |
18
|
|
|
use Romm\Formz\Condition\Parser\Tree\EmptyConditionTree; |
19
|
|
|
use Romm\Formz\Condition\Parser\Node\ConditionNode; |
20
|
|
|
use Romm\Formz\Condition\Parser\Node\NodeInterface; |
21
|
|
|
use Romm\Formz\Form\Definition\Field\Activation\ActivationInterface; |
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
|
|
|
class ConditionProcessor |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var FormObject |
30
|
|
|
*/ |
31
|
|
|
private $formObject; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ConditionTree[] |
35
|
|
|
*/ |
36
|
|
|
private $fieldsTrees = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ConditionTree[] |
40
|
|
|
*/ |
41
|
|
|
private $conditionTrees = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $javaScriptFiles = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param FormObject $formObject |
50
|
|
|
*/ |
51
|
|
|
public function __construct(FormObject $formObject) |
52
|
|
|
{ |
53
|
|
|
$this->attachFormObject($formObject); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the condition tree for a given field instance, giving access to |
58
|
|
|
* CSS, JavaScript and PHP transpiled results. |
59
|
|
|
* |
60
|
|
|
* @param Field $field |
61
|
|
|
* @return ConditionTree |
62
|
|
|
*/ |
63
|
|
|
public function getActivationConditionTreeForField(Field $field) |
64
|
|
|
{ |
65
|
|
|
$key = $field->getName(); |
66
|
|
|
|
67
|
|
|
if (false === array_key_exists($key, $this->fieldsTrees)) { |
68
|
|
|
$this->fieldsTrees[$key] = $field->hasActivation() |
69
|
|
|
? $this->getConditionTree($field->getActivation()) |
70
|
|
|
: EmptyConditionTree::get(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($field->hasActivation()) { |
74
|
|
|
$this->fieldsTrees[$key]->injectDependencies($this, $field->getActivation()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->fieldsTrees[$key]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns the condition tree for a given validator instance, giving access |
82
|
|
|
* to CSS, JavaScript and PHP transpiled results. |
83
|
|
|
* |
84
|
|
|
* @param Validator $validator |
85
|
|
|
* @return ConditionTree |
86
|
|
|
*/ |
87
|
|
|
public function getActivationConditionTreeForValidator(Validator $validator) |
88
|
|
|
{ |
89
|
|
|
$key = $validator->getParentField()->getName() . '->' . $validator->getName(); |
90
|
|
|
|
91
|
|
|
if (false === array_key_exists($key, $this->conditionTrees)) { |
92
|
|
|
$this->conditionTrees[$key] = $validator->hasActivation() |
93
|
|
|
? $this->getConditionTree($validator->getActivation()) |
94
|
|
|
: EmptyConditionTree::get(); |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($validator->hasActivation()) { |
99
|
|
|
$this->conditionTrees[$key]->injectDependencies($this, $validator->getActivation()); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->conditionTrees[$key]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Function that will calculate all trees from fields and their validators. |
107
|
|
|
* |
108
|
|
|
* This is useful to be able to store this instance in cache. |
109
|
|
|
*/ |
110
|
|
|
public |
111
|
|
|
function calculateAllTrees() |
112
|
|
|
{ |
113
|
|
|
$fields = $this->formObject->getDefinition()->getFields(); |
114
|
|
|
|
115
|
|
|
foreach ($fields as $field) { |
116
|
|
|
$this->getActivationConditionTreeForField($field); |
117
|
|
|
|
118
|
|
|
foreach ($field->getValidators() as $validator) { |
119
|
|
|
$this->getActivationConditionTreeForValidator($validator); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param ActivationInterface $activation |
126
|
|
|
* @return ConditionTree |
127
|
|
|
*/ |
128
|
|
|
protected |
129
|
|
|
function getConditionTree( |
130
|
|
|
ActivationInterface $activation |
131
|
|
|
) { |
132
|
|
|
$tree = $this->getNewConditionTreeFromActivation($activation); |
133
|
|
|
$tree->alongNodes(function (NodeInterface $node) { |
134
|
|
|
$this->attachNodeJavaScriptFiles($node); |
135
|
|
|
}); |
136
|
|
|
|
137
|
|
|
return $tree; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param ActivationInterface $activation |
142
|
|
|
* @return ConditionTree |
143
|
|
|
*/ |
144
|
|
|
protected |
145
|
|
|
function getNewConditionTreeFromActivation( |
146
|
|
|
ActivationInterface $activation |
147
|
|
|
) { |
148
|
|
|
return ConditionParserFactory::get() |
149
|
|
|
->parse($activation); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param NodeInterface $node |
154
|
|
|
*/ |
155
|
|
|
protected |
156
|
|
|
function attachNodeJavaScriptFiles( |
157
|
|
|
NodeInterface $node |
158
|
|
|
) { |
159
|
|
|
if ($node instanceof ConditionNode) { |
160
|
|
|
$files = $node->getCondition()->getJavaScriptFiles(); |
161
|
|
|
|
162
|
|
|
foreach ($files as $file) { |
163
|
|
|
if (false === in_array($file, $this->javaScriptFiles)) { |
164
|
|
|
$this->javaScriptFiles[] = $file; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param FormObject $formObject |
172
|
|
|
*/ |
173
|
|
|
public |
174
|
|
|
function attachFormObject( |
175
|
|
|
FormObject $formObject |
176
|
|
|
) { |
177
|
|
|
$this->formObject = $formObject; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @return FormObject |
182
|
|
|
*/ |
183
|
|
|
public |
184
|
|
|
function getFormObject() |
185
|
|
|
{ |
186
|
|
|
return $this->formObject; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
public |
193
|
|
|
function getJavaScriptFiles() |
194
|
|
|
{ |
195
|
|
|
return $this->javaScriptFiles; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @return array |
200
|
|
|
*/ |
201
|
|
|
public |
202
|
|
|
function __sleep() |
203
|
|
|
{ |
204
|
|
|
return ['fieldsTrees', 'conditionTrees', 'javaScriptFiles']; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|