Completed
Pull Request — master (#270)
by Marc
03:17 queued 55s
created

AbstractNode::setEscapeOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
     * @var boolean
27
     */
28
    protected $escapeOutput = false;
29
30
    /**
31
     * Evaluate all child nodes and return the evaluated results.
32
     *
33
     * @param RenderingContextInterface $renderingContext
34
     * @return mixed Normally, an object is returned - in case it is concatenated with a string, a string is returned.
35
     * @throws Parser\Exception
36
     */
37
    public function evaluateChildNodes(RenderingContextInterface $renderingContext)
38
    {
39
        $numberOfChildNodes = count($this->childNodes);
40
        if ($numberOfChildNodes === 0) {
41
            return null;
42
        }
43
        if ($numberOfChildNodes === 1) {
44
            return $this->evaluateChildNode($this->childNodes[0], $renderingContext, false);
45
        }
46
        $output = '';
47
        /** @var $subNode NodeInterface */
48
        foreach ($this->childNodes as $subNode) {
49
            $output .= $this->evaluateChildNode($subNode, $renderingContext, true);
50
        }
51
        return $output;
52
    }
53
54
    /**
55
     * @param NodeInterface $node
56
     * @param RenderingContextInterface $renderingContext
57
     * @param boolean $cast
58
     * @return mixed
59
     */
60
    protected function evaluateChildNode(NodeInterface $node, RenderingContextInterface $renderingContext, $cast)
61
    {
62
        $output = $node->evaluate($renderingContext);
63
        if ($cast && is_object($output)) {
64
            if (!method_exists($output, '__toString')) {
65
                throw new Parser\Exception('Cannot cast object of type "' . get_class($output) . '" to string.', 1273753083);
66
            }
67
            $output = (string) $output;
68
        }
69
        if ($cast && is_array($output)) {
70
            $output = 'Array';
71
        }
72
        return $output;
73
    }
74
75
    /**
76
     * Returns all child nodes for a given node.
77
     * This is especially needed to implement the boolean expression language.
78
     *
79
     * @return NodeInterface[] A list of nodes
80
     */
81
    public function getChildNodes()
82
    {
83
        return $this->childNodes;
84
    }
85
86
    /**
87
     * Appends a sub node to this node. Is used inside the parser to append children
88
     *
89
     * @param NodeInterface $childNode The sub node to add
90
     * @return void
91
     */
92
    public function addChildNode(NodeInterface $childNode)
93
    {
94
        $this->childNodes[] = $childNode;
95
    }
96
97
    /**
98
     * Specifies, if this node's output should be escaped
99
     *
100
     * @return bool
101
     */
102
    public function isEscapeOutputEnabled()
103
    {
104
        return $this->escapeOutput;
105
    }
106
107
    /**
108
     * @param bool $escapeOutput
109
     */
110
    public function setEscapeOutput($escapeOutput)
111
    {
112
        $this->escapeOutput = $escapeOutput;
113
    }
114
115
}
116