-
Notifications
You must be signed in to change notification settings - Fork 93
/
ObjectAccessorNode.php
103 lines (94 loc) · 2.94 KB
/
ObjectAccessorNode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types=1);
/*
* This file belongs to the package "TYPO3 Fluid".
* See LICENSE.txt that was shipped with this package.
*/
namespace TYPO3Fluid\Fluid\Core\Parser\SyntaxTree;
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
/**
* A node which handles object access. This means it handles structures like {object.accessor.bla}
*
* @internal
* @todo Make class final.
*/
class ObjectAccessorNode extends AbstractNode
{
/**
* Object path which will be called. Is a list like "post.name.email"
*/
protected string $objectPath;
/**
* Constructor. Takes an object path as input.
*
* The first part of the object path has to be a variable in the
* VariableProvider.
*
* @param string $objectPath An Object Path, like object1.object2.object3
*/
public function __construct(string $objectPath)
{
$this->objectPath = $objectPath;
}
/**
* @internal Internally used for building up cached templates; do not use directly!
* @return string
*/
public function getObjectPath(): string
{
return $this->objectPath;
}
/**
* Evaluate this node and return the correct object.
*
* Handles each part (denoted by .) in $this->objectPath in the following order:
* - call appropriate getter
* - call public property, if exists
* - fail
*
* The first part of the object path has to be a variable in the
* VariableProvider.
*
* @return mixed The evaluated object, can be any object type.
*/
public function evaluate(RenderingContextInterface $renderingContext): mixed
{
$variableProvider = $renderingContext->getVariableProvider();
return match (strtolower($this->objectPath)) {
'_all' => $variableProvider->getAll(),
'true' => true,
'false' => false,
'null' => null,
default => $variableProvider->getByPath($this->objectPath),
};
}
public function convert(TemplateCompiler $templateCompiler): array
{
return match (strtolower($this->objectPath)) {
'_all' => [
'initialization' => '',
'execution' => '$renderingContext->getVariableProvider()->getAll()',
],
'true' => [
'initialization' => '',
'execution' => 'true',
],
'false' => [
'initialization' => '',
'execution' => 'false',
],
'null' => [
'initialization' => '',
'execution' => 'null',
],
default => [
'initialization' => '',
'execution' => sprintf(
'$renderingContext->getVariableProvider()->getByPath(\'%s\')',
$this->objectPath,
),
],
};
}
}