|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the TYPO3 CMS project. |
|
5
|
|
|
* |
|
6
|
|
|
* It is free software; you can redistribute it and/or modify it under |
|
7
|
|
|
* the terms of the GNU General Public License, either version 2 |
|
8
|
|
|
* of the License, or any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please read the |
|
11
|
|
|
* LICENSE.txt file that was distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* The TYPO3 project - inspiring people to share! |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace TYPO3\CMS\Extbase\Mvc\Controller; |
|
17
|
|
|
|
|
18
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
19
|
|
|
use TYPO3\CMS\Extbase\Error\Result; |
|
20
|
|
|
use TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* A composite of controller arguments |
|
24
|
|
|
* @internal only to be used within Extbase, not part of TYPO3 Core API. |
|
25
|
|
|
*/ |
|
26
|
|
|
class Arguments extends \ArrayObject |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var array Names of the arguments contained by this object |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $argumentNames = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $argumentShortNames = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Constructor. If this one is removed, reflection breaks. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct() |
|
42
|
|
|
{ |
|
43
|
|
|
parent::__construct(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Adds or replaces the argument specified by $value. The argument's name is taken from the |
|
48
|
|
|
* argument object itself, therefore the $offset does not have any meaning in this context. |
|
49
|
|
|
* |
|
50
|
|
|
* @param mixed $offset Offset - not used here |
|
51
|
|
|
* @param mixed $value The argument |
|
52
|
|
|
* @throws \InvalidArgumentException if the argument is not a valid Controller Argument object |
|
53
|
|
|
*/ |
|
54
|
|
|
public function offsetSet($offset, $value) |
|
55
|
|
|
{ |
|
56
|
|
|
if (!$value instanceof Argument) { |
|
57
|
|
|
throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953786); |
|
58
|
|
|
} |
|
59
|
|
|
$argumentName = $value->getName(); |
|
60
|
|
|
parent::offsetSet($argumentName, $value); |
|
61
|
|
|
$this->argumentNames[$argumentName] = true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Sets an argument, aliased to offsetSet() |
|
66
|
|
|
* |
|
67
|
|
|
* @param mixed $value The value |
|
68
|
|
|
* @throws \InvalidArgumentException if the argument is not a valid Controller Argument object |
|
69
|
|
|
*/ |
|
70
|
|
|
public function append($value) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!$value instanceof Argument) { |
|
73
|
|
|
throw new \InvalidArgumentException('Controller arguments must be valid TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument objects.', 1187953787); |
|
74
|
|
|
} |
|
75
|
|
|
$this->offsetSet(null, $value); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Unsets an argument |
|
80
|
|
|
* |
|
81
|
|
|
* @param mixed $offset Offset |
|
82
|
|
|
*/ |
|
83
|
|
|
public function offsetUnset($offset) |
|
84
|
|
|
{ |
|
85
|
|
|
$translatedOffset = $this->translateToLongArgumentName($offset); |
|
86
|
|
|
parent::offsetUnset($translatedOffset); |
|
87
|
|
|
unset($this->argumentNames[$translatedOffset]); |
|
88
|
|
|
if ($offset != $translatedOffset) { |
|
89
|
|
|
unset($this->argumentShortNames[$offset]); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns whether the requested index exists |
|
95
|
|
|
* |
|
96
|
|
|
* @param mixed $offset Offset |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
public function offsetExists($offset) |
|
100
|
|
|
{ |
|
101
|
|
|
$translatedOffset = $this->translateToLongArgumentName($offset); |
|
102
|
|
|
return parent::offsetExists($translatedOffset); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns the value at the specified index |
|
107
|
|
|
* |
|
108
|
|
|
* @param mixed $offset Offset |
|
109
|
|
|
* @return Argument The requested argument object |
|
110
|
|
|
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException if the argument does not exist |
|
111
|
|
|
*/ |
|
112
|
|
|
public function offsetGet($offset) |
|
113
|
|
|
{ |
|
114
|
|
|
$translatedOffset = $this->translateToLongArgumentName($offset); |
|
115
|
|
|
if ($translatedOffset === '') { |
|
116
|
|
|
throw new NoSuchArgumentException('The argument "' . $offset . '" does not exist.', 1216909923); |
|
117
|
|
|
} |
|
118
|
|
|
return parent::offsetGet($translatedOffset); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Creates, adds and returns a new controller argument to this composite object. |
|
123
|
|
|
* If an argument with the same name exists already, it will be replaced by the |
|
124
|
|
|
* new argument object. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $name Name of the argument |
|
127
|
|
|
* @param string $dataType Name of one of the built-in data types |
|
128
|
|
|
* @param bool $isRequired TRUE if this argument should be marked as required |
|
129
|
|
|
* @param mixed $defaultValue Default value of the argument. Only makes sense if $isRequired==FALSE |
|
130
|
|
|
* @return Argument The new argument |
|
131
|
|
|
*/ |
|
132
|
|
|
public function addNewArgument($name, $dataType = 'Text', $isRequired = false, $defaultValue = null) |
|
133
|
|
|
{ |
|
134
|
|
|
/** @var Argument $argument */ |
|
135
|
|
|
$argument = GeneralUtility::makeInstance(Argument::class, $name, $dataType); |
|
136
|
|
|
$argument->setRequired($isRequired); |
|
137
|
|
|
$argument->setDefaultValue($defaultValue); |
|
138
|
|
|
$this->addArgument($argument); |
|
139
|
|
|
return $argument; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Adds the specified controller argument to this composite object. |
|
144
|
|
|
* If an argument with the same name exists already, it will be replaced by the |
|
145
|
|
|
* new argument object. |
|
146
|
|
|
* |
|
147
|
|
|
* Note that the argument will be cloned, not referenced. |
|
148
|
|
|
* |
|
149
|
|
|
* @param Argument $argument The argument to add |
|
150
|
|
|
*/ |
|
151
|
|
|
public function addArgument(Argument $argument) |
|
152
|
|
|
{ |
|
153
|
|
|
$this->offsetSet(null, $argument); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Returns an argument specified by name |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $argumentName Name of the argument to retrieve |
|
160
|
|
|
* @return Argument |
|
161
|
|
|
* @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException |
|
162
|
|
|
*/ |
|
163
|
|
|
public function getArgument($argumentName) |
|
164
|
|
|
{ |
|
165
|
|
|
if (!$this->offsetExists($argumentName)) { |
|
166
|
|
|
throw new NoSuchArgumentException('An argument "' . $argumentName . '" does not exist.', 1195815178); |
|
167
|
|
|
} |
|
168
|
|
|
return $this->offsetGet($argumentName); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Checks if an argument with the specified name exists |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $argumentName Name of the argument to check for |
|
175
|
|
|
* @return bool TRUE if such an argument exists, otherwise FALSE |
|
176
|
|
|
* @see offsetExists() |
|
177
|
|
|
*/ |
|
178
|
|
|
public function hasArgument($argumentName) |
|
179
|
|
|
{ |
|
180
|
|
|
return $this->offsetExists($argumentName); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns the names of all arguments contained in this object |
|
185
|
|
|
* |
|
186
|
|
|
* @return array Argument names |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getArgumentNames() |
|
189
|
|
|
{ |
|
190
|
|
|
return array_keys($this->argumentNames); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Returns the short names of all arguments contained in this object that have one. |
|
195
|
|
|
* |
|
196
|
|
|
* @return array Argument short names |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getArgumentShortNames() |
|
199
|
|
|
{ |
|
200
|
|
|
$argumentShortNames = []; |
|
201
|
|
|
/** @var Argument $argument */ |
|
202
|
|
|
foreach ($this as $argument) { |
|
203
|
|
|
$argumentShortNames[$argument->getShortName()] = true; |
|
204
|
|
|
} |
|
205
|
|
|
return array_keys($argumentShortNames); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Magic setter method for the argument values. Each argument |
|
210
|
|
|
* value can be set by just calling the setArgumentName() method. |
|
211
|
|
|
* |
|
212
|
|
|
* @param string $methodName Name of the method |
|
213
|
|
|
* @param array $arguments Method arguments |
|
214
|
|
|
* @throws \LogicException |
|
215
|
|
|
*/ |
|
216
|
|
|
public function __call($methodName, array $arguments) |
|
217
|
|
|
{ |
|
218
|
|
|
if (strpos($methodName, 'set') !== 0) { |
|
219
|
|
|
throw new \LogicException('Unknown method "' . $methodName . '".', 1210858451); |
|
220
|
|
|
} |
|
221
|
|
|
$firstLowerCaseArgumentName = $this->translateToLongArgumentName(strtolower($methodName[3]) . substr($methodName, 4)); |
|
222
|
|
|
$firstUpperCaseArgumentName = $this->translateToLongArgumentName(ucfirst(substr($methodName, 3))); |
|
223
|
|
|
if (in_array($firstLowerCaseArgumentName, $this->getArgumentNames())) { |
|
224
|
|
|
$argument = parent::offsetGet($firstLowerCaseArgumentName); |
|
225
|
|
|
$argument->setValue($arguments[0]); |
|
226
|
|
|
} elseif (in_array($firstUpperCaseArgumentName, $this->getArgumentNames())) { |
|
227
|
|
|
$argument = parent::offsetGet($firstUpperCaseArgumentName); |
|
228
|
|
|
$argument->setValue($arguments[0]); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Translates a short argument name to its corresponding long name. If the |
|
234
|
|
|
* specified argument name is a real argument name already, it will be returned again. |
|
235
|
|
|
* |
|
236
|
|
|
* If an argument with the specified name or short name does not exist, an empty |
|
237
|
|
|
* string is returned. |
|
238
|
|
|
* |
|
239
|
|
|
* @param string $argumentName argument name |
|
240
|
|
|
* @return string long argument name or empty string |
|
241
|
|
|
*/ |
|
242
|
|
|
protected function translateToLongArgumentName($argumentName) |
|
243
|
|
|
{ |
|
244
|
|
|
if (in_array($argumentName, $this->getArgumentNames())) { |
|
245
|
|
|
return $argumentName; |
|
246
|
|
|
} |
|
247
|
|
|
/** @var Argument $argument */ |
|
248
|
|
|
foreach ($this as $argument) { |
|
249
|
|
|
if ($argumentName === $argument->getShortName()) { |
|
250
|
|
|
return $argument->getName(); |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
return ''; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Remove all arguments and resets this object |
|
258
|
|
|
*/ |
|
259
|
|
|
public function removeAll() |
|
260
|
|
|
{ |
|
261
|
|
|
foreach ($this->argumentNames as $argumentName => $booleanValue) { |
|
262
|
|
|
parent::offsetUnset($argumentName); |
|
263
|
|
|
} |
|
264
|
|
|
$this->argumentNames = []; |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* @return \TYPO3\CMS\Extbase\Error\Result |
|
269
|
|
|
*/ |
|
270
|
|
|
public function validate(): Result |
|
271
|
|
|
{ |
|
272
|
|
|
$results = new Result(); |
|
273
|
|
|
/** @var Argument $argument */ |
|
274
|
|
|
foreach ($this as $argument) { |
|
275
|
|
|
$argumentValidationResults = $argument->validate(); |
|
276
|
|
|
if ($argumentValidationResults === null) { |
|
277
|
|
|
continue; |
|
278
|
|
|
} |
|
279
|
|
|
$results->forProperty($argument->getName())->merge($argumentValidationResults); |
|
280
|
|
|
} |
|
281
|
|
|
return $results; |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|