-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Argument.php
278 lines (250 loc) · 6.8 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
<?php
namespace Neos\Flow\Mvc\Controller;
/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Error\Messages\Result;
use Neos\Flow\Property\PropertyMapper;
use Neos\Utility\TypeHandling;
use Neos\Flow\Validation\Validator\ValidatorInterface;
/**
* A controller argument
*
* @api
*/
class Argument
{
/**
* Name of this argument
* @var string
*/
protected $name = '';
/**
* Data type of this argument's value
* @var string
*/
protected $dataType = null;
/**
* true if this argument is required
* @var boolean
*/
protected $isRequired = false;
/**
* Actual value of this argument
* @var object
*/
protected $value = null;
/**
* Default value. Used if argument is optional.
* @var mixed
*/
protected $defaultValue = null;
/**
* A custom validator, used supplementary to the base validation
* @var ValidatorInterface|null
*/
protected $validator = null;
/**
* The validation results. This can be asked if the argument has errors.
* @var Result
*/
protected $validationResults = null;
/**
* If the request body should be mapped into this argument.
* @var bool
*/
protected $mapRequestBody = false;
/**
* @var MvcPropertyMappingConfiguration
*/
protected $propertyMappingConfiguration;
/**
* @Flow\Inject
* @var PropertyMapper
*/
protected $propertyMapper;
/**
* 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
* @api
*/
public function __construct(string $name, string $dataType)
{
if ($name === '') {
throw new \InvalidArgumentException('$name must be a non-empty string, ' . strlen($name) . ' characters given.', 1232551853);
}
$this->name = $name;
$this->dataType = TypeHandling::normalizeType($dataType);
}
/**
* Returns the name of this argument
*
* @return string This argument's name
* @api
*/
public function getName(): string
{
return $this->name;
}
/**
* Returns the data type of this argument's value
*
* @return string The data type
* @api
*/
public function getDataType(): string
{
return $this->dataType;
}
/**
* Marks this argument to be required
*
* @param boolean $required true if this argument should be required
* @return Argument $this
* @api
*/
public function setRequired(bool $required): Argument
{
$this->isRequired = $required;
return $this;
}
/**
* Returns true if this argument is required
*
* @return boolean true if this argument is required
* @api
*/
public function isRequired(): bool
{
return $this->isRequired;
}
/**
* Sets the default value of the argument
*
* @param mixed $defaultValue Default value
* @return Argument $this
* @api
*/
public function setDefaultValue($defaultValue): Argument
{
$this->defaultValue = $defaultValue;
return $this;
}
/**
* Returns the default value of this argument
*
* @return mixed The default value
* @api
*/
public function getDefaultValue()
{
return $this->defaultValue;
}
/**
* Sets a custom validator which is used supplementary to the base validation
*
* @param ValidatorInterface $validator The actual validator object
* @return Argument Returns $this (used for fluent interface)
* @api
*/
public function setValidator(ValidatorInterface $validator): Argument
{
$this->validator = $validator;
return $this;
}
/**
* Returns the set validator
*
* @return ValidatorInterface|null The set validator, NULL if none was set
* @api
*/
public function getValidator(): ?ValidatorInterface
{
return $this->validator;
}
/**
* Sets the value of this argument.
*
* @param mixed $rawValue The value of this argument
* @return Argument $this
* @throws \Neos\Flow\Property\Exception
* @throws \Neos\Flow\Security\Exception
*/
public function setValue($rawValue): Argument
{
if ($rawValue === null) {
$this->value = null;
return $this;
}
if (is_object($rawValue) && $rawValue instanceof $this->dataType) {
$this->value = $rawValue;
return $this;
}
$this->value = $this->propertyMapper->convert($rawValue, $this->dataType, $this->getPropertyMappingConfiguration());
$this->validationResults = $this->propertyMapper->getMessages();
if ($this->validator !== null) {
$validationMessages = $this->validator->validate($this->value);
$this->validationResults->merge($validationMessages);
}
return $this;
}
/**
* Returns the value of this argument. If the value is NULL, we use the defaultValue.
*
* @return object The value of this argument - if none was set, the default value is returned
* @api
*/
public function getValue()
{
return $this->value ?? $this->defaultValue;
}
/**
* Return the Property Mapping Configuration used for this argument; can be used by the initialize*action to modify the Property Mapping.
*
* @return MvcPropertyMappingConfiguration
* @api
*/
public function getPropertyMappingConfiguration(): MvcPropertyMappingConfiguration
{
if ($this->propertyMappingConfiguration === null) {
$this->propertyMappingConfiguration = new MvcPropertyMappingConfiguration();
}
return $this->propertyMappingConfiguration;
}
/**
* @return Result|null Validation errors which have occurred.
* @api
*/
public function getValidationResults(): ?Result
{
return $this->validationResults;
}
/**
* @return bool
*/
public function getMapRequestBody(): bool
{
return $this->mapRequestBody;
}
/**
* Set if the request body should be mapped into this argument.
*
* @param bool $mapRequestBody
* @return Argument $this
*/
public function setMapRequestBody(bool $mapRequestBody): Argument
{
$this->mapRequestBody = $mapRequestBody;
return $this;
}
}