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\Parser; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Condition\Parser\Node\NodeInterface; |
17
|
|
|
use Romm\Formz\Condition\Processor\ConditionProcessor; |
18
|
|
|
use Romm\Formz\Condition\Processor\DataObject\PhpConditionDataObject; |
19
|
|
|
use TYPO3\CMS\Extbase\Error\Result; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Tree built with instances of `NodeInterface` that represents a condition |
23
|
|
|
* instance. |
24
|
|
|
* |
25
|
|
|
* It can be used to get CSS, JavaScript or PHP results of the condition. |
26
|
|
|
*/ |
27
|
|
|
class ConditionTree |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var NodeInterface |
31
|
|
|
*/ |
32
|
|
|
private $rootNode; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Result |
36
|
|
|
*/ |
37
|
|
|
private $validationResult; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ConditionProcessor |
41
|
|
|
*/ |
42
|
|
|
private $conditionProcessor; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param NodeInterface $rootNode |
46
|
|
|
* @param Result $validationResult |
47
|
|
|
*/ |
48
|
|
|
public function __construct(NodeInterface $rootNode, Result $validationResult) |
49
|
|
|
{ |
50
|
|
|
$this->rootNode = $rootNode; |
51
|
|
|
$this->validationResult = $validationResult; |
52
|
|
|
|
53
|
|
|
$this->rootNode->setTree($this); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param ConditionProcessor $conditionProcessor |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function attachConditionProcessor(ConditionProcessor $conditionProcessor) |
61
|
|
|
{ |
62
|
|
|
$this->conditionProcessor = $conditionProcessor; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Allows to go through all the nodes and sub-nodes of the tree. The |
69
|
|
|
* callback is called for every node, with a unique argument: the node |
70
|
|
|
* instance. |
71
|
|
|
* |
72
|
|
|
* @param callable $callback |
73
|
|
|
*/ |
74
|
|
|
public function alongNodes(callable $callback) |
75
|
|
|
{ |
76
|
|
|
$this->rootNode->along($callback); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function getCssConditions() |
83
|
|
|
{ |
84
|
|
|
return $this->rootNode->getCssResult(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function getJavaScriptConditions() |
91
|
|
|
{ |
92
|
|
|
return $this->rootNode->getJavaScriptResult(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param PhpConditionDataObject $dataObject |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function getPhpResult(PhpConditionDataObject $dataObject) |
100
|
|
|
{ |
101
|
|
|
return $this->rootNode->getPhpResult($dataObject); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Result |
106
|
|
|
*/ |
107
|
|
|
public function getValidationResult() |
108
|
|
|
{ |
109
|
|
|
return $this->validationResult; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return ConditionProcessor |
114
|
|
|
*/ |
115
|
|
|
public function getConditionProcessor() |
116
|
|
|
{ |
117
|
|
|
return $this->conditionProcessor; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|