[-TASK] Extbase (MVC): Removed check for multiple data types. It makes no sense to...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / Controller / Argument.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * A controller argument
30 *
31 * @package Extbase
32 * @subpackage MVC\Controller
33 * @version $ID:$
34 * @scope prototype
35 */
36 class Tx_Extbase_MVC_Controller_Argument {
37
38 /**
39 * @var Tx_Extbase_Persistence_QueryFactory
40 */
41 protected $queryFactory;
42
43 /**
44 * @var Tx_Extbase_Property_Mapper
45 */
46 protected $propertyMapper;
47
48 /**
49 * @var Tx_Extbase_Reflection_Service
50 */
51 protected $reflectionService;
52
53 /**
54 * Name of this argument
55 * @var string
56 */
57 protected $name = '';
58
59 /**
60 * Short name of this argument
61 * @var string
62 */
63 protected $shortName = NULL;
64
65 /**
66 * Data type of this argument's value
67 * @var string
68 */
69 protected $dataType = 'Text';
70
71 /**
72 * If the data type is an object, the class schema of the data type class is resolved
73 * @var Tx_Extbase_Reflection_ClassSchema
74 */
75 protected $dataTypeClassSchema;
76
77 /**
78 * TRUE if this argument is required
79 * @var boolean
80 */
81 protected $isRequired = FALSE;
82
83 /**
84 * Actual value of this argument
85 * @var object
86 */
87 protected $value = NULL;
88
89 /**
90 * Default value. Used if argument is optional.
91 * @var mixed
92 */
93 protected $defaultValue = NULL;
94
95 /**
96 * A custom validator, used supplementary to the base validation
97 * @var Tx_Extbase_Validation_Validator_ValidatorInterface
98 */
99 protected $validator = NULL;
100
101 /**
102 * If validation for this argument is temporarily disabled
103 * @var boolean
104 */
105 protected $validationDisabled = FALSE;
106
107 /**
108 * Uid for the argument, if it has one
109 * @var string
110 */
111 protected $uid = NULL;
112
113 /**
114 * Constructs this controller argument
115 *
116 * @param string $name Name of this argument
117 * @param string $dataType The data type of this argument
118 * @throws InvalidArgumentException if $name is not a string or empty
119 * @api
120 */
121 public function __construct($name, $dataType = 'Text') {
122 $this->reflectionService = t3lib_div::makeInstance('Tx_Extbase_Reflection_Service');
123 $this->propertyMapper = t3lib_div::makeInstance('Tx_Extbase_Property_Mapper');
124 $this->propertyMapper->injectReflectionService(t3lib_div::makeInstance('Tx_Extbase_Reflection_Service'));
125 if (!is_string($name) || strlen($name) < 1) throw new InvalidArgumentException('$name must be of type string, ' . gettype($name) . ' given.', 1187951688);
126 $this->name = $name;
127 $this->setDataType($dataType);
128 }
129
130 /**
131 * Injects the Persistence Manager
132 *
133 * @param Tx_Extbase_Persistence_ManagerInterface
134 * @return void
135 */
136 public function injectPersistenceManager(Tx_Extbase_Persistence_ManagerInterface $persistenceManager) {
137 $this->persistenceManager = $persistenceManager;
138 }
139
140 /**
141 * Injects a QueryFactory instance
142 *
143 * @param Tx_Extbase_Persistence_QueryFactoryInterface $queryFactory
144 * @return void
145 */
146 public function injectQueryFactory(Tx_Extbase_Persistence_QueryFactoryInterface $queryFactory) {
147 $this->queryFactory = $queryFactory;
148 }
149
150 /**
151 * Returns the name of this argument
152 *
153 * @return string This argument's name
154 * @api
155 */
156 public function getName() {
157 return $this->name;
158 }
159
160 /**
161 * Sets the short name of this argument.
162 *
163 * @param string $shortName A "short name" - a single character
164 * @return Tx_Extbase_MVC_Controller_Argument $this
165 * @throws InvalidArgumentException if $shortName is not a character
166 * @api
167 */
168 public function setShortName($shortName) {
169 if ($shortName !== NULL && (!is_string($shortName) || strlen($shortName) !== 1)) throw new InvalidArgumentException('$shortName must be a single character or NULL', 1195824959);
170 $this->shortName = $shortName;
171 return $this;
172 }
173
174 /**
175 * Returns the short name of this argument
176 *
177 * @return string This argument's short name
178 * @api
179 */
180 public function getShortName() {
181 return $this->shortName;
182 }
183
184 /**
185 * Sets the data type of this argument's value
186 *
187 * @param string $dataType The data type. Can be either a built-in type such as "Text" or "Integer" or a fully qualified object name
188 * @return Tx_Extbase_MVC_Controller_Argument $this
189 * @api
190 */
191 public function setDataType($dataType) {
192 $this->dataType = $dataType;
193 $this->dataTypeClassSchema = $this->reflectionService->getClassSchema($this->dataType);
194 return $this;
195 }
196
197 /**
198 * Returns the data type of this argument's value
199 *
200 * @return string The data type
201 * @api
202 */
203 public function getDataType() {
204 return $this->dataType;
205 }
206
207 /**
208 * Marks this argument to be required
209 *
210 * @param boolean $required TRUE if this argument should be required
211 * @return Tx_Extbase_MVC_Controller_Argument $this
212 * @api
213 */
214 public function setRequired($required) {
215 $this->isRequired = (boolean)$required;
216 return $this;
217 }
218
219 /**
220 * Returns TRUE if this argument is required
221 *
222 * @return boolean TRUE if this argument is required
223 * @api
224 */
225 public function isRequired() {
226 return $this->isRequired;
227 }
228
229 /**
230 * Sets the default value of the argument
231 *
232 * @param mixed $defaultValue Default value
233 * @return void
234 * @api
235 */
236 public function setDefaultValue($defaultValue) {
237 $this->defaultValue = $defaultValue;
238 }
239
240 /**
241 * Returns the default value of this argument
242 *
243 * @return mixed The default value
244 * @api
245 */
246 public function getDefaultValue() {
247 return $this->defaultValue;
248 }
249
250 /**
251 * Sets a custom validator which is used supplementary to the base validation
252 *
253 * @param Tx_Extbase_Validation_Validator_ValidatorInterface $validator The actual validator object
254 * @return Tx_Extbase_MVC_Controller_Argument Returns $this (used for fluent interface)
255 * @api
256 */
257 public function setValidator(Tx_Extbase_Validation_Validator_ValidatorInterface $validator) {
258 $this->validator = $validator;
259 return $this;
260 }
261
262 /**
263 * Create and set a validator chain
264 *
265 * @param array Object names of the validators
266 * @return Tx_Extbase_MVC_Controller_Argument Returns $this (used for fluent interface)
267 * @api
268 */
269 public function setNewValidatorConjunction(array $objectNames) {
270 if ($this->validator === NULL) {
271 $this->validator = t3lib_div::makeInstance('Tx_Extbase_Validation_Validator_ConjunctionValidator');
272 }
273 foreach ($objectNames as $objectName) {
274 if (!class_exists($objectName)) $objectName = 'Tx_Extbase_Validation_Validator_' . $objectName;
275 $this->validator->addValidator(t3lib_div::makeInstance($objectName));
276 }
277 return $this;
278 }
279
280 /**
281 * Returns the set validator
282 *
283 * @return Tx_Extbase_Validation_Validator_ValidatorInterface The set validator, NULL if none was set
284 * @api
285 */
286 public function getValidator() {
287 return $this->validator;
288 }
289
290 /**
291 * Returns TRUE if validation is temporarily disabled for this argument and
292 * FALSE if it's enabled.
293 *
294 * Note that this is flag is only informational and does not have any real impact
295 * on other validation methods of this argument.
296 *
297 * @return boolean If validation is disabled
298 * @api
299 */
300 public function isValidationDisabled() {
301 return $this->validationDisabled;
302 }
303
304 /**
305 * Enables validation for this argument.
306 *
307 * @return void
308 * @api
309 */
310 public function enableValidation() {
311 $this->validationDisabled = FALSE;
312 }
313
314 /**
315 * Disables validation for this argument.
316 *
317 * @return void
318 * @api
319 */
320 public function disableValidation() {
321 $this->validationDisabled = TRUE;
322 }
323
324 /**
325 * Sets the value of this argument.
326 *
327 * @param mixed $value: The value of this argument
328 * @return Tx_Extbase_MVC_Controller_Argument $this
329 * @throws Tx_Extbase_MVC_Exception_InvalidArgumentValue if the argument is not a valid object of type $dataType
330 */
331 public function setValue($value) {
332 if ($value !== NULL && $this->dataTypeClassSchema !== NULL) {
333 if (is_numeric($value)) {
334 $value = $this->findObjectByUid($value);
335 } elseif (is_array($value)) {
336 $value = $this->propertyMapper->map(array_keys($value), $value, $this->dataType);
337 }
338
339 if (!($value instanceof $this->dataType)) {
340 throw new Tx_Extbase_MVC_Exception_InvalidArgumentValue('The value must be of type "' . $this->dataType . '".', 1251730701);
341 }
342 }
343 $this->value = $value;
344
345 return $this;
346 }
347
348 /**
349 * Finds an object from the repository by searching for its technical UID.
350 *
351 * @param int $uid The object's uid
352 * @return mixed Either the object matching the uid or, if none or more than one object was found, FALSE
353 */
354 protected function findObjectByUid($uid) {
355 $query = $this->queryFactory->create($this->dataType);
356 $result = $query->matching($query->withUid($uid))->execute();
357 $object = NULL;
358 if (count($result) > 0) {
359 $object = current($result);
360 }
361 return $object;
362 }
363
364 /**
365 * Returns the value of this argument
366 *
367 * @return object The value of this argument - if none was set, NULL is returned
368 * @api
369 */
370 public function getValue() {
371 if ($this->value === NULL) {
372 return $this->defaultValue;
373 } else {
374 return $this->value;
375 }
376 }
377
378 /**
379 * Checks if this argument has a value set.
380 *
381 * @return boolean TRUE if a value was set, otherwise FALSE
382 */
383 public function isValue() {
384 return $this->value !== NULL;
385 }
386
387 /**
388 * Returns a string representation of this argument's value
389 *
390 * @return string
391 * @api
392 */
393 public function __toString() {
394 return (string)$this->value;
395 }
396 }
397 ?>