1
|
|
|
<?php |
2
|
|
|
namespace TYPO3Fluid\Fluid\Core\Parser\SyntaxTree; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file belongs to the package "TYPO3 Fluid". |
6
|
|
|
* See LICENSE.txt that was shipped with this package. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use TYPO3Fluid\Fluid\Core\Parser; |
10
|
|
|
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Abstract node in the syntax tree which has been built. |
14
|
|
|
*/ |
15
|
|
|
abstract class AbstractNode implements NodeInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* List of Child Nodes. |
20
|
|
|
* |
21
|
|
|
* @var NodeInterface[] |
22
|
|
|
*/ |
23
|
|
|
protected $childNodes = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Evaluate all child nodes and return the evaluated results. |
27
|
|
|
* |
28
|
|
|
* @param RenderingContextInterface $renderingContext |
29
|
|
|
* @return mixed Normally, an object is returned - in case it is concatenated with a string, a string is returned. |
30
|
|
|
* @throws Parser\Exception |
31
|
|
|
*/ |
32
|
|
|
public function evaluateChildNodes(RenderingContextInterface $renderingContext) |
33
|
|
|
{ |
34
|
|
|
$evaluatedNodes = []; |
35
|
|
|
foreach ($this->getChildNodes() as $childNode) { |
36
|
|
|
$evaluatedNodes[] = $this->evaluateChildNode($childNode, $renderingContext, false); |
37
|
|
|
} |
38
|
|
|
// Make decisions about what to actually return |
39
|
|
|
if (empty($evaluatedNodes)) { |
40
|
|
|
return null; |
41
|
|
|
} |
42
|
|
|
if (count($evaluatedNodes) === 1) { |
43
|
|
|
return $evaluatedNodes[0]; |
44
|
|
|
} |
45
|
|
|
return implode('', array_map([$this, 'castToString'], $evaluatedNodes)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param NodeInterface $node |
50
|
|
|
* @param RenderingContextInterface $renderingContext |
51
|
|
|
* @param boolean $cast |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
protected function evaluateChildNode(NodeInterface $node, RenderingContextInterface $renderingContext, $cast) |
55
|
|
|
{ |
56
|
|
|
$output = $node->evaluate($renderingContext); |
57
|
|
|
if ($cast) { |
58
|
|
|
$output = $this->castToString($output); |
59
|
|
|
} |
60
|
|
|
return $output; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param mixed $value |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
protected function castToString($value) |
68
|
|
|
{ |
69
|
|
|
if (is_object($value) && !method_exists($value, '__toString')) { |
70
|
|
|
throw new Parser\Exception('Cannot cast object of type "' . get_class($value) . '" to string.', 1273753083); |
71
|
|
|
} |
72
|
|
|
$output = (string) $value; |
73
|
|
|
return $output; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns all child nodes for a given node. |
78
|
|
|
* This is especially needed to implement the boolean expression language. |
79
|
|
|
* |
80
|
|
|
* @return NodeInterface[] A list of nodes |
81
|
|
|
*/ |
82
|
|
|
public function getChildNodes() |
83
|
|
|
{ |
84
|
|
|
return $this->childNodes; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Appends a sub node to this node. Is used inside the parser to append children |
89
|
|
|
* |
90
|
|
|
* @param NodeInterface $childNode The sub node to add |
91
|
|
|
* @return void |
92
|
|
|
*/ |
93
|
|
|
public function addChildNode(NodeInterface $childNode) |
94
|
|
|
{ |
95
|
|
|
$this->childNodes[] = $childNode; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|