1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2016 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\ConditionTree; |
17
|
|
|
use Romm\Formz\Condition\Parser\ConditionParserFactory; |
18
|
|
|
use Romm\Formz\Configuration\Form\Field\Field; |
19
|
|
|
use Romm\Formz\Configuration\Form\Field\Validation\Validation; |
20
|
|
|
use Romm\Formz\Configuration\Form\Form; |
21
|
|
|
use Romm\Formz\Form\FormObject; |
22
|
|
|
|
23
|
|
|
class ConditionProcessor |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var FormObject |
27
|
|
|
*/ |
28
|
|
|
private $formObject; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Form |
32
|
|
|
*/ |
33
|
|
|
private $formConfiguration; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ConditionTree[] |
37
|
|
|
*/ |
38
|
|
|
private $fieldsTrees = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var ConditionTree[] |
42
|
|
|
*/ |
43
|
|
|
private $validationsTrees = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param FormObject $formObject |
47
|
|
|
*/ |
48
|
|
|
public function __construct(FormObject $formObject) |
49
|
|
|
{ |
50
|
|
|
$this->attachFormObject($formObject); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the condition tree for a given field instance, giving access to |
55
|
|
|
* CSS, JavaScript and PHP transpiled results. |
56
|
|
|
* |
57
|
|
|
* @param Field $field |
58
|
|
|
* @return ConditionTree |
59
|
|
|
*/ |
60
|
|
|
public function getActivationConditionTreeForField(Field $field) |
61
|
|
|
{ |
62
|
|
|
if (false === array_key_exists($field->getFieldName(), $this->fieldsTrees)) { |
63
|
|
|
$this->fieldsTrees[$field->getFieldName()] = ConditionParserFactory::get($this->formObject) |
64
|
|
|
->parse($field->getActivation()) |
65
|
|
|
->attachConditionProcessor($this); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->fieldsTrees[$field->getFieldName()]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns the condition tree for a given validation instance, giving access |
73
|
|
|
* to CSS, JavaScript and PHP transpiled results. |
74
|
|
|
* |
75
|
|
|
* @param Validation $validation |
76
|
|
|
* @return ConditionTree |
77
|
|
|
*/ |
78
|
|
|
public function getActivationConditionTreeForValidation(Validation $validation) |
79
|
|
|
{ |
80
|
|
|
$key = $validation->getParentField()->getFieldName() . '->' . $validation->getValidationName(); |
81
|
|
|
|
82
|
|
|
if (false === array_key_exists($key, $this->validationsTrees)) { |
83
|
|
|
$this->validationsTrees[$key] = ConditionParserFactory::get($this->formObject) |
84
|
|
|
->parse($validation->getActivation()) |
85
|
|
|
->attachConditionProcessor($this); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $this->validationsTrees[$key]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Function that will calculate all trees from fields and their validation |
93
|
|
|
* rules. |
94
|
|
|
* |
95
|
|
|
* This is useful to be able to store this instance in cache. |
96
|
|
|
*/ |
97
|
|
|
public function calculateAllTrees() |
98
|
|
|
{ |
99
|
|
|
$fields = $this->formObject->getConfiguration()->getFields(); |
100
|
|
|
foreach ($fields as $field) { |
101
|
|
|
$this->getActivationConditionTreeForField($field); |
102
|
|
|
|
103
|
|
|
foreach ($field->getValidation() as $validation) { |
|
|
|
|
104
|
|
|
$this->getActivationConditionTreeForValidation($validation); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param FormObject $formObject |
111
|
|
|
*/ |
112
|
|
|
public function attachFormObject(FormObject $formObject) |
113
|
|
|
{ |
114
|
|
|
$this->formObject = $formObject; |
115
|
|
|
$this->formConfiguration = $this->formObject->getConfiguration();; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return FormObject |
120
|
|
|
*/ |
121
|
|
|
public function getFormObject() |
122
|
|
|
{ |
123
|
|
|
return $this->formObject; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function __sleep() |
130
|
|
|
{ |
131
|
|
|
return ['fieldsTrees', 'validationsTrees']; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.