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\Rendering\RenderingContextInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Argument Collection |
16
|
|
|
* |
17
|
|
|
* Acts as container around a set of arguments and associated |
18
|
|
|
* ArgumentDefinition and their values. |
19
|
|
|
* |
20
|
|
|
* Contains the API used for validating and converting arguments. |
21
|
|
|
*/ |
22
|
|
|
class ArgumentCollection extends \ArrayObject |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ArgumentDefinition[] |
26
|
|
|
*/ |
27
|
|
|
protected $definitions = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var RenderingContextInterface |
31
|
|
|
*/ |
32
|
|
|
protected $renderingContext; |
33
|
|
|
|
34
|
|
|
public function setRenderingContext(RenderingContextInterface $renderingContext): self |
35
|
|
|
{ |
36
|
|
|
$this->renderingContext = $renderingContext; |
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getRenderingContext(): RenderingContextInterface |
41
|
|
|
{ |
42
|
|
|
return $this->renderingContext; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getDefinitions(): iterable |
46
|
|
|
{ |
47
|
|
|
return $this->definitions; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function assignAll(iterable $values): ArgumentCollection |
51
|
|
|
{ |
52
|
|
|
foreach ($values as $name => $value) { |
53
|
|
|
$this[$name] = $value; |
54
|
|
|
} |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getAllRaw(): iterable |
59
|
|
|
{ |
60
|
|
|
return parent::getArrayCopy(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getRaw(string $argumentName) |
64
|
|
|
{ |
65
|
|
|
$value = $this[$argumentName] ?? null; |
66
|
|
|
return $value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function addDefinition(ArgumentDefinition $definition): ArgumentCollection |
70
|
|
|
{ |
71
|
|
|
$argumentName = $definition->getName(); |
72
|
|
|
$this->definitions[$argumentName] = $definition; |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param iterable|ArgumentDefinition[] $definitions |
78
|
|
|
* @return ArgumentCollection |
79
|
|
|
*/ |
80
|
|
|
public function setDefinitions(iterable $definitions): ArgumentCollection |
81
|
|
|
{ |
82
|
|
|
$this->definitions = $definitions; |
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function offsetGet($offset) |
87
|
|
|
{ |
88
|
|
|
if (isset($this->definitions[$offset]) && !parent::offsetExists($offset)) { |
|
|
|
|
89
|
|
|
return $this->definitions[$offset]->getDefaultValue(); |
90
|
|
|
} |
91
|
|
|
$value = parent::offsetGet($offset); |
92
|
|
|
if ($value instanceof ComponentInterface) { |
93
|
|
|
$value = $value->evaluate($this->renderingContext); |
94
|
|
|
} |
95
|
|
|
return $value; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getArrayCopy(): array |
99
|
|
|
{ |
100
|
|
|
$data = []; |
101
|
|
|
foreach (parent::getArrayCopy() + $this->definitions as $name => $_) { |
102
|
|
|
$data[$name] = $this[$name]; |
103
|
|
|
} |
104
|
|
|
return $data; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Creates arguments by padding with missing+optional arguments |
109
|
|
|
* and casting or creating BooleanNode where appropriate. Input |
110
|
|
|
* array may not contain all arguments - output array will. |
111
|
|
|
*/ |
112
|
|
|
public function validate(): self |
113
|
|
|
{ |
114
|
|
|
$missingArguments = []; |
115
|
|
|
foreach ($this->definitions as $name => $definition) { |
116
|
|
|
if ($definition->isRequired() && !parent::offsetExists($name)) { |
|
|
|
|
117
|
|
|
// Required but missing argument, causes failure (delayed, to report all missing arguments at once) |
118
|
|
|
$missingArguments[] = $name; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
if (!empty($missingArguments)) { |
122
|
|
|
throw new Exception('Required argument(s) not provided: ' . implode(', ', $missingArguments), 1558533510); |
123
|
|
|
} |
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
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.