1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace TYPO3Fluid\Fluid\Core\Parser\SyntaxTree; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file belongs to the package "TYPO3 Fluid". |
7
|
|
|
* See LICENSE.txt that was shipped with this package. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
use TYPO3Fluid\Fluid\Component\AbstractComponent; |
11
|
|
|
use TYPO3Fluid\Fluid\Component\ComponentInterface; |
12
|
|
|
use TYPO3Fluid\Fluid\Component\EmbeddedComponentInterface; |
13
|
|
|
use TYPO3Fluid\Fluid\Component\TransparentComponentInterface; |
14
|
|
|
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Entry node. Used to represent either the root |
18
|
|
|
* entry point of a template tree, or a section |
19
|
|
|
* within the template tree. Common for EntryNode |
20
|
|
|
* is that: |
21
|
|
|
* |
22
|
|
|
* - It supports arguments that become variables |
23
|
|
|
* - It supports setting the name of the node which |
24
|
|
|
* allows it to be returned from getNamedChild. |
25
|
|
|
* - It is an EmbeddedComponent which means that it |
26
|
|
|
* is only rendered when explicitly calling the |
27
|
|
|
* evaluate() method on the instance itself, it |
28
|
|
|
* is not rendered when rendering the parent node. |
29
|
|
|
* |
30
|
|
|
* EntryNode instances can be resolved directly |
31
|
|
|
* from getTypedChildren to extract all sections |
32
|
|
|
* within a tree; or getNamedChild can be used |
33
|
|
|
* to extract an EntryNode (or an EntryNode child |
34
|
|
|
* of another EntryNode to any nesting depth by |
35
|
|
|
* using dotted path as argument for getNamedChild). |
36
|
|
|
*/ |
37
|
|
|
class EntryNode extends AbstractComponent implements EmbeddedComponentInterface, TransparentComponentInterface |
38
|
|
|
{ |
39
|
|
|
protected $escapeOutput = false; |
40
|
|
|
|
41
|
|
|
public function setName(?string $name): ComponentInterface |
42
|
|
|
{ |
43
|
|
|
$this->name = $name; |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function evaluate(RenderingContextInterface $renderingContext) |
48
|
|
|
{ |
49
|
|
|
$renderingContext = clone $renderingContext; |
50
|
|
|
$renderingContext->setVariableProvider( |
51
|
|
|
$renderingContext->getVariableProvider()->getScopeCopy( |
52
|
|
|
$this->getArguments()->setRenderingContext($renderingContext)->getArrayCopy() |
53
|
|
|
+ $renderingContext->getVariableProvider()->getAll() |
54
|
|
|
) |
55
|
|
|
); |
56
|
|
|
return parent::evaluate($renderingContext); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function onOpen(RenderingContextInterface $renderingContext): ComponentInterface |
60
|
|
|
{ |
61
|
|
|
$this->getArguments()->setRenderingContext($renderingContext)->validate(); |
62
|
|
|
return parent::onOpen($renderingContext); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.