1 | <?php |
||
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) |
||
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) |
||
72 | |||
73 | /** |
||
74 | * Returns the condition tree for a given validation instance, giving access |
||
75 | * to CSS, JavaScript and PHP transpiled results. |
||
76 | * |
||
77 | * @param Validation $validation |
||
78 | * @return ConditionTree |
||
79 | */ |
||
80 | public function getActivationConditionTreeForValidation(Validation $validation) |
||
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 | |||
101 | foreach ($fields as $field) { |
||
102 | $this->getActivationConditionTreeForField($field); |
||
103 | |||
104 | foreach ($field->getValidation() as $validation) { |
||
|
|||
105 | $this->getActivationConditionTreeForValidation($validation); |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param ActivationInterface $activation |
||
112 | * @return ConditionTree |
||
113 | */ |
||
114 | protected function getConditionTree(ActivationInterface $activation) |
||
115 | { |
||
116 | $tree = $this->getNewConditionTreeFromActivation($activation); |
||
117 | $tree->alongNodes(function (NodeInterface $node) { |
||
118 | $this->attachNodeJavaScriptFiles($node); |
||
119 | }); |
||
120 | |||
121 | return $tree; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param ActivationInterface $activation |
||
126 | * @return ConditionTree |
||
127 | */ |
||
128 | protected function getNewConditionTreeFromActivation(ActivationInterface $activation) |
||
129 | { |
||
130 | return ConditionParserFactory::get() |
||
131 | ->parse($activation) |
||
132 | ->attachConditionProcessor($this); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * @param NodeInterface $node |
||
137 | */ |
||
138 | protected function attachNodeJavaScriptFiles(NodeInterface $node) |
||
139 | { |
||
140 | if ($node instanceof ConditionNode) { |
||
141 | $files = $node->getCondition()->getJavaScriptFiles(); |
||
142 | |||
143 | foreach ($files as $file) { |
||
144 | if (false === in_array($file, $this->javaScriptFiles)) { |
||
145 | $this->javaScriptFiles[] = $file; |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param FormObject $formObject |
||
153 | */ |
||
154 | public function attachFormObject(FormObject $formObject) |
||
155 | { |
||
156 | $this->formObject = $formObject; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @return array |
||
161 | */ |
||
162 | public function getJavaScriptFiles() |
||
166 | |||
167 | /** |
||
168 | * @return array |
||
169 | */ |
||
170 | public function __sleep() |
||
174 | } |
||
175 |
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.