1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace TYPO3Fluid\Fluid\Component\Argument; |
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\ComponentInterface; |
11
|
|
|
use TYPO3Fluid\Fluid\Core\Parser\Exception; |
12
|
|
|
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ArrayNode; |
13
|
|
|
use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\BooleanNode; |
14
|
|
|
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Argument Collection |
18
|
|
|
* |
19
|
|
|
* Acts as container around a set of arguments and associated |
20
|
|
|
* ArgumentDefinition and their values. |
21
|
|
|
* |
22
|
|
|
* Contains the API used for validating and converting arguments. |
23
|
|
|
*/ |
24
|
|
|
class ArgumentCollection extends \ArrayObject |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var ArgumentDefinition[] |
28
|
|
|
*/ |
29
|
|
|
protected $definitions = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var RenderingContextInterface |
33
|
|
|
*/ |
34
|
|
|
protected $renderingContext; |
35
|
|
|
|
36
|
|
|
public function setRenderingContext(RenderingContextInterface $renderingContext): self |
37
|
|
|
{ |
38
|
|
|
$this->renderingContext = $renderingContext; |
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getRenderingContext(): RenderingContextInterface |
43
|
|
|
{ |
44
|
|
|
return $this->renderingContext; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getDefinitions(): iterable |
48
|
|
|
{ |
49
|
|
|
return $this->definitions; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function assignAll(iterable $values): ArgumentCollection |
53
|
|
|
{ |
54
|
|
|
foreach ($values as $name => $value) { |
55
|
|
|
$this[$name] = $value; |
56
|
|
|
} |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getAllRaw(): iterable |
61
|
|
|
{ |
62
|
|
|
return parent::getArrayCopy(); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getRaw(string $argumentName) |
66
|
|
|
{ |
67
|
|
|
$value = $this[$argumentName] ?? null; |
68
|
|
|
return $value; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function addDefinition(ArgumentDefinition $definition): ArgumentCollection |
72
|
|
|
{ |
73
|
|
|
$argumentName = $definition->getName(); |
74
|
|
|
$this->definitions[$argumentName] = $definition; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param iterable|ArgumentDefinition[] $definitions |
80
|
|
|
* @return ArgumentCollection |
81
|
|
|
*/ |
82
|
|
|
public function setDefinitions(iterable $definitions): ArgumentCollection |
83
|
|
|
{ |
84
|
|
|
$this->definitions = $definitions; |
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function isArgumentBoolean(string $argumentName): bool |
89
|
|
|
{ |
90
|
|
|
return isset($this->definitions[$argumentName]) && $this->definitions[$argumentName]->getType() === 'boolean'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function toArrayNode(): ArrayNode |
94
|
|
|
{ |
95
|
|
|
return new ArrayNode((array) $this->getAllRaw()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function offsetSet($index, $value) |
99
|
|
|
{ |
100
|
|
|
if (isset($this->definitions[$index]) && !$value instanceof BooleanNode && $this->definitions[$index]->getType() === 'boolean' && $value !== false && $value !== true) { |
101
|
|
|
// Note: a switch() was not used here because it makes PHP attempt type coercion and we need strict comparisons. |
102
|
|
|
if ($value === 1 || $value === 'true' || $value === 'TRUE') { |
103
|
|
|
$value = true; |
104
|
|
|
} elseif ($value === null || $value === 0 || $value === 'false' || $value === 'FALSE') { |
105
|
|
|
$value = false; |
106
|
|
|
} else { |
107
|
|
|
$value = new BooleanNode($value); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
parent::offsetSet($index, $value); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function offsetGet($offset) |
114
|
|
|
{ |
115
|
|
|
if (isset($this->definitions[$offset]) && !parent::offsetExists($offset)) { |
|
|
|
|
116
|
|
|
return $this->definitions[$offset]->getDefaultValue(); |
117
|
|
|
} |
118
|
|
|
$value = parent::offsetGet($offset); |
119
|
|
|
if ($value instanceof ComponentInterface) { |
120
|
|
|
$value = $value->evaluate($this->renderingContext); |
121
|
|
|
} |
122
|
|
|
return $value; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getArrayCopy(): array |
126
|
|
|
{ |
127
|
|
|
$data = []; |
128
|
|
|
foreach (parent::getArrayCopy() + $this->definitions as $name => $_) { |
129
|
|
|
$data[$name] = $this[$name]; |
130
|
|
|
} |
131
|
|
|
return $data; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Creates arguments by padding with missing+optional arguments |
136
|
|
|
* and casting or creating BooleanNode where appropriate. Input |
137
|
|
|
* array may not contain all arguments - output array will. |
138
|
|
|
*/ |
139
|
|
|
public function validate(): self |
140
|
|
|
{ |
141
|
|
|
$missingArguments = []; |
142
|
|
|
foreach ($this->definitions as $name => $definition) { |
143
|
|
|
if ($definition->isRequired() && !parent::offsetExists($name)) { |
|
|
|
|
144
|
|
|
// Required but missing argument, causes failure (delayed, to report all missing arguments at once) |
145
|
|
|
$missingArguments[] = $name; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
if (!empty($missingArguments)) { |
149
|
|
|
throw new Exception('Required argument(s) not provided: ' . implode(', ', $missingArguments), 1558533510); |
150
|
|
|
} |
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.