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