Completed
Push — unit-tests-conditions ( 10987e...dba20c )
by Romain
03:04
created

ConditionProcessor::getConditionTree()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
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\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
        return $this->fieldsTrees[$key];
71
    }
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)
81
    {
82
        $key = $validation->getParentField()->getFieldName() . '->' . $validation->getValidationName();
83
84
        if (false === array_key_exists($key, $this->validationsTrees)) {
85
            $this->validationsTrees[$key] = $this->getConditionTree($validation->getActivation());
86
        }
87
88
        return $this->validationsTrees[$key];
89
    }
90
91
    /**
92
     * @param ActivationInterface $activation
93
     * @return ConditionTree
94
     */
95
    protected function getConditionTree(ActivationInterface $activation)
96
    {
97
        return ConditionParserFactory::get()
98
            ->parse($activation)
99
            ->attachConditionProcessor($this);
100
    }
101
102
    /**
103
     * Function that will calculate all trees from fields and their validation
104
     * rules.
105
     *
106
     * This is useful to be able to store this instance in cache.
107
     */
108
    public function calculateAllTrees()
109
    {
110
        $fields = $this->formObject->getConfiguration()->getFields();
111
112
        foreach ($fields as $field) {
113
            $this->getActivationConditionTreeForField($field)
114
                ->alongNodes(function (NodeInterface $node) {
115
                    $this->attachNodeJavaScriptFiles($node);
116
                });
117
118
            foreach ($field->getValidation() as $validation) {
0 ignored issues
show
Bug introduced by
The expression $field->getValidation() of type object<Romm\Formz\Config...Validation\Validation>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. 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:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
119
                $this->getActivationConditionTreeForValidation($validation)
120
                    ->alongNodes(function (NodeInterface $node) {
121
                        $this->attachNodeJavaScriptFiles($node);
122
                    });
123
            }
124
        }
125
    }
126
127
    /**
128
     * @param NodeInterface $node
129
     */
130
    protected function attachNodeJavaScriptFiles(NodeInterface $node)
131
    {
132
        if ($node instanceof ConditionNode) {
133
            $files = $node->getCondition()->getJavaScriptFiles();
134
135
            foreach ($files as $file) {
136
                if (false === in_array($file, $this->javaScriptFiles)) {
137
                    $this->javaScriptFiles[] = $file;
138
                }
139
            }
140
        }
141
    }
142
143
    /**
144
     * @param FormObject $formObject
145
     */
146
    public function attachFormObject(FormObject $formObject)
147
    {
148
        $this->formObject = $formObject;
149
    }
150
151
    /**
152
     * @return FormObject
153
     */
154
    public function getFormObject()
155
    {
156
        return $this->formObject;
157
    }
158
159
    /**
160
     * @return array
161
     */
162
    public function getJavaScriptFiles()
163
    {
164
        return $this->javaScriptFiles;
165
    }
166
167
    /**
168
     * @return array
169
     */
170
    public function __sleep()
171
    {
172
        return ['fieldsTrees', 'validationsTrees', 'javaScriptFiles'];
173
    }
174
}
175