/
Argument.php
338 lines (302 loc) · 8.7 KB
/
Argument.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Extbase\Mvc\Controller;
use TYPO3\CMS\Extbase\Error\Result;
use TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException;
use TYPO3\CMS\Extbase\Property\PropertyMapper;
use TYPO3\CMS\Extbase\Utility\TypeHandlingUtility;
use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface;
/**
* A controller argument
*/
class Argument
{
/**
* @var \TYPO3\CMS\Extbase\Property\PropertyMapper
*/
protected $propertyMapper;
/**
* @var MvcPropertyMappingConfiguration
*/
protected $propertyMappingConfiguration;
/**
* Name of this argument
*
* @var string
*/
protected $name = '';
/**
* Short name of this argument
*
* @var string
*/
protected $shortName;
/**
* Data type of this argument's value
*
* @var string
*/
protected $dataType;
/**
* TRUE if this argument is required
*
* @var bool
*/
protected $isRequired = false;
/**
* Actual value of this argument
*
* @var mixed|null
*/
protected $value;
/**
* Default value. Used if argument is optional.
*
* @var mixed
*/
protected $defaultValue;
/**
* A custom validator, used supplementary to the base validation
*
* @var \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
*/
protected $validator;
/**
* The validation results. This can be asked if the argument has errors.
*
* @var \TYPO3\CMS\Extbase\Error\Result
*/
protected $validationResults;
/**
* @var bool
*/
private $hasBeenValidated = false;
/**
* @param \TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper
*/
public function injectPropertyMapper(PropertyMapper $propertyMapper)
{
$this->propertyMapper = $propertyMapper;
}
/**
* @param \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration $propertyMappingConfiguration
*/
public function injectPropertyMappingConfiguration(MvcPropertyMappingConfiguration $propertyMappingConfiguration)
{
$this->propertyMappingConfiguration = $propertyMappingConfiguration;
}
/**
* Constructs this controller argument
*
* @param string $name Name of this argument
* @param string $dataType The data type of this argument
* @throws \InvalidArgumentException if $name is not a string or empty
*/
public function __construct($name, $dataType)
{
if (!is_string($name)) {
throw new \InvalidArgumentException('$name must be of type string, ' . gettype($name) . ' given.', 1187951688);
}
if ($name === '') {
throw new \InvalidArgumentException('$name must be a non-empty string.', 1232551853);
}
$this->name = $name;
$this->dataType = TypeHandlingUtility::normalizeType($dataType);
$this->validationResults = new Result();
}
/**
* Returns the name of this argument
*
* @return string This argument's name
*/
public function getName()
{
return $this->name;
}
/**
* Sets the short name of this argument.
*
* @param string $shortName A "short name" - a single character
* @throws \InvalidArgumentException if $shortName is not a character
* @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument $this
*/
public function setShortName($shortName)
{
if ($shortName !== null && (!is_string($shortName) || strlen($shortName) !== 1)) {
throw new \InvalidArgumentException('$shortName must be a single character or NULL', 1195824959);
}
$this->shortName = $shortName;
return $this;
}
/**
* Returns the short name of this argument
*
* @return string This argument's short name
*/
public function getShortName()
{
return $this->shortName;
}
/**
* Returns the data type of this argument's value
*
* @return string The data type
*/
public function getDataType()
{
return $this->dataType;
}
/**
* Marks this argument to be required
*
* @param bool $required TRUE if this argument should be required
* @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument $this
*/
public function setRequired($required)
{
$this->isRequired = (bool)$required;
return $this;
}
/**
* Returns TRUE if this argument is required
*
* @return bool TRUE if this argument is required
*/
public function isRequired()
{
return $this->isRequired;
}
/**
* Sets the default value of the argument
*
* @param mixed $defaultValue Default value
* @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument $this
*/
public function setDefaultValue($defaultValue)
{
$this->defaultValue = $defaultValue;
return $this;
}
/**
* Returns the default value of this argument
*
* @return mixed The default value
*/
public function getDefaultValue()
{
return $this->defaultValue;
}
/**
* Sets a custom validator which is used supplementary to the base validation
*
* @param \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator The actual validator object
* @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument Returns $this (used for fluent interface)
*/
public function setValidator(ValidatorInterface $validator)
{
$this->validator = $validator;
return $this;
}
/**
* Returns the set validator
*
* @return \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface The set validator, NULL if none was set
*/
public function getValidator()
{
return $this->validator;
}
/**
* Sets the value of this argument.
*
* @param mixed $rawValue The value of this argument
*
* @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument
* @throws \TYPO3\CMS\Extbase\Property\Exception
*/
public function setValue($rawValue)
{
if ($rawValue === null) {
$this->value = null;
return $this;
}
if (is_object($rawValue) && $rawValue instanceof $this->dataType) {
$this->value = $rawValue;
return $this;
}
try {
$this->value = $this->propertyMapper->convert($rawValue, $this->dataType, $this->propertyMappingConfiguration);
} catch (TargetNotFoundException $e) {
// for optional arguments no exception is thrown.
if ($this->isRequired()) {
throw $e;
}
}
$this->validationResults->merge($this->propertyMapper->getMessages());
$this->propertyMapper->resetMessages();
return $this;
}
/**
* Returns the value of this argument
*
* @return mixed The value of this argument - if none was set, NULL is returned
*/
public function getValue()
{
if ($this->value === null) {
return $this->defaultValue;
}
return $this->value;
}
/**
* Return the Property Mapping Configuration used for this argument; can be used by the initialize*action to modify the Property Mapping.
*
* @return \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration
*/
public function getPropertyMappingConfiguration()
{
return $this->propertyMappingConfiguration;
}
/**
* @return bool TRUE if the argument is valid, FALSE otherwise
*/
public function isValid(): bool
{
return !$this->validate()->hasErrors();
}
/**
* Returns a string representation of this argument's value
*
* @return string
*/
public function __toString()
{
return (string)$this->value;
}
/**
* @return \TYPO3\CMS\Extbase\Error\Result
*/
public function validate(): Result
{
if ($this->hasBeenValidated) {
return $this->validationResults;
}
if ($this->validator !== null) {
$validationMessages = $this->validator->validate($this->value);
$this->validationResults->merge($validationMessages);
}
$this->hasBeenValidated = true;
return $this->validationResults;
}
}