e2957914e012ff4b3b7df626b2df2e60c61dcf61
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Controller / Argument.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Controller;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extbase\Property\Exception\TargetNotFoundException;
18 use TYPO3\CMS\Extbase\Utility\TypeHandlingUtility;
19
20 /**
21 * A controller argument
22 *
23 * @api
24 */
25 class Argument
26 {
27 /**
28 * @var \TYPO3\CMS\Extbase\Property\PropertyMapper
29 */
30 protected $propertyMapper;
31
32 /**
33 * @var MvcPropertyMappingConfiguration
34 */
35 protected $propertyMappingConfiguration;
36
37 /**
38 * Name of this argument
39 *
40 * @var string
41 */
42 protected $name = '';
43
44 /**
45 * Short name of this argument
46 *
47 * @var string
48 */
49 protected $shortName = null;
50
51 /**
52 * Data type of this argument's value
53 *
54 * @var string
55 */
56 protected $dataType = null;
57
58 /**
59 * TRUE if this argument is required
60 *
61 * @var bool
62 */
63 protected $isRequired = false;
64
65 /**
66 * Actual value of this argument
67 *
68 * @var mixed
69 */
70 protected $value = null;
71
72 /**
73 * Default value. Used if argument is optional.
74 *
75 * @var mixed
76 */
77 protected $defaultValue = null;
78
79 /**
80 * A custom validator, used supplementary to the base validation
81 *
82 * @var \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
83 */
84 protected $validator = null;
85
86 /**
87 * The validation results. This can be asked if the argument has errors.
88 *
89 * @var \TYPO3\CMS\Extbase\Error\Result
90 */
91 protected $validationResults = null;
92
93 /**
94 * @param \TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper
95 */
96 public function injectPropertyMapper(\TYPO3\CMS\Extbase\Property\PropertyMapper $propertyMapper)
97 {
98 $this->propertyMapper = $propertyMapper;
99 }
100
101 /**
102 * @param \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration $propertyMappingConfiguration
103 */
104 public function injectPropertyMappingConfiguration(MvcPropertyMappingConfiguration $propertyMappingConfiguration)
105 {
106 $this->propertyMappingConfiguration = $propertyMappingConfiguration;
107 }
108
109 /**
110 * Constructs this controller argument
111 *
112 * @param string $name Name of this argument
113 * @param string $dataType The data type of this argument
114 * @throws \InvalidArgumentException if $name is not a string or empty
115 * @api
116 */
117 public function __construct($name, $dataType)
118 {
119 if (!is_string($name)) {
120 throw new \InvalidArgumentException('$name must be of type string, ' . gettype($name) . ' given.', 1187951688);
121 }
122 if ($name === '') {
123 throw new \InvalidArgumentException('$name must be a non-empty string.', 1232551853);
124 }
125 $this->name = $name;
126 $this->dataType = TypeHandlingUtility::normalizeType($dataType);
127 }
128
129 /**
130 * Returns the name of this argument
131 *
132 * @return string This argument's name
133 * @api
134 */
135 public function getName()
136 {
137 return $this->name;
138 }
139
140 /**
141 * Sets the short name of this argument.
142 *
143 * @param string $shortName A "short name" - a single character
144 * @throws \InvalidArgumentException if $shortName is not a character
145 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument $this
146 * @api
147 */
148 public function setShortName($shortName)
149 {
150 if ($shortName !== null && (!is_string($shortName) || strlen($shortName) !== 1)) {
151 throw new \InvalidArgumentException('$shortName must be a single character or NULL', 1195824959);
152 }
153 $this->shortName = $shortName;
154 return $this;
155 }
156
157 /**
158 * Returns the short name of this argument
159 *
160 * @return string This argument's short name
161 * @api
162 */
163 public function getShortName()
164 {
165 return $this->shortName;
166 }
167
168 /**
169 * Returns the data type of this argument's value
170 *
171 * @return string The data type
172 * @api
173 */
174 public function getDataType()
175 {
176 return $this->dataType;
177 }
178
179 /**
180 * Marks this argument to be required
181 *
182 * @param bool $required TRUE if this argument should be required
183 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument $this
184 * @api
185 */
186 public function setRequired($required)
187 {
188 $this->isRequired = (bool)$required;
189 return $this;
190 }
191
192 /**
193 * Returns TRUE if this argument is required
194 *
195 * @return bool TRUE if this argument is required
196 * @api
197 */
198 public function isRequired()
199 {
200 return $this->isRequired;
201 }
202
203 /**
204 * Sets the default value of the argument
205 *
206 * @param mixed $defaultValue Default value
207 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument $this
208 * @api
209 */
210 public function setDefaultValue($defaultValue)
211 {
212 $this->defaultValue = $defaultValue;
213 return $this;
214 }
215
216 /**
217 * Returns the default value of this argument
218 *
219 * @return mixed The default value
220 * @api
221 */
222 public function getDefaultValue()
223 {
224 return $this->defaultValue;
225 }
226
227 /**
228 * Sets a custom validator which is used supplementary to the base validation
229 *
230 * @param \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator The actual validator object
231 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument Returns $this (used for fluent interface)
232 * @api
233 */
234 public function setValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator)
235 {
236 $this->validator = $validator;
237 return $this;
238 }
239
240 /**
241 * Returns the set validator
242 *
243 * @return \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface The set validator, NULL if none was set
244 * @api
245 */
246 public function getValidator()
247 {
248 return $this->validator;
249 }
250
251 /**
252 * Sets the value of this argument.
253 *
254 * @param mixed $rawValue The value of this argument
255 *
256 * @return \TYPO3\CMS\Extbase\Mvc\Controller\Argument
257 * @throws \TYPO3\CMS\Extbase\Property\Exception
258 */
259 public function setValue($rawValue)
260 {
261 if ($rawValue === null) {
262 $this->value = null;
263 return $this;
264 }
265 if (is_object($rawValue) && $rawValue instanceof $this->dataType) {
266 $this->value = $rawValue;
267 return $this;
268 }
269 try {
270 $this->value = $this->propertyMapper->convert($rawValue, $this->dataType, $this->propertyMappingConfiguration);
271 } catch (TargetNotFoundException $e) {
272 // for optional arguments no exeption is thrown.
273 if ($this->isRequired()) {
274 throw $e;
275 }
276 }
277 $this->validationResults = $this->propertyMapper->getMessages();
278 if ($this->validator !== null) {
279 // @todo Validation API has also changed!!!
280 $validationMessages = $this->validator->validate($this->value);
281 $this->validationResults->merge($validationMessages);
282 }
283 return $this;
284 }
285
286 /**
287 * Returns the value of this argument
288 *
289 * @return mixed The value of this argument - if none was set, NULL is returned
290 * @api
291 */
292 public function getValue()
293 {
294 if ($this->value === null) {
295 return $this->defaultValue;
296 } else {
297 return $this->value;
298 }
299 }
300
301 /**
302 * Return the Property Mapping Configuration used for this argument; can be used by the initialize*action to modify the Property Mapping.
303 *
304 * @return \TYPO3\CMS\Extbase\Mvc\Controller\MvcPropertyMappingConfiguration
305 * @api
306 */
307 public function getPropertyMappingConfiguration()
308 {
309 return $this->propertyMappingConfiguration;
310 }
311
312 /**
313 * @return bool TRUE if the argument is valid, FALSE otherwise
314 * @api
315 */
316 public function isValid()
317 {
318 return !$this->validationResults->hasErrors();
319 }
320
321 /**
322 * @return \TYPO3\CMS\Extbase\Error\Result Validation errors which have occurred.
323 * @api
324 */
325 public function getValidationResults()
326 {
327 return $this->validationResults;
328 }
329
330 /**
331 * Returns a string representation of this argument's value
332 *
333 * @return string
334 * @api
335 */
336 public function __toString()
337 {
338 return (string)$this->value;
339 }
340 }