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