* added some fixtures for the unit tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Controller / TX_EXTMVC_Controller_Argument.php
1 <?php
2 declare(ENCODING = 'utf-8');
3
4 /* *
5 * This script belongs to the FLOW3 framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 /**
25 * A controller argument
26 *
27 * @version $Id:$
28 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
29 * @scope prototype
30 */
31 class Argument {
32
33 /**
34 * @var F3_FLOW3_Object_ManagerInterface
35 */
36 protected $objectManager;
37
38 /**
39 * @var F3_FLOW3_Object_FactoryInterface
40 */
41 protected $objectFactory;
42
43 /**
44 * Name of this argument
45 * @var string
46 */
47 protected $name = '';
48
49 /**
50 * Short name of this argument
51 * @var string
52 */
53 protected $shortName = NULL;
54
55 /**
56 * Data type of this argument's value
57 * @var string
58 */
59 protected $dataType = 'Text';
60
61 /**
62 * TRUE if this argument is required
63 * @var boolean
64 */
65 protected $isRequired = FALSE;
66
67 /**
68 * Actual value of this argument
69 * @var object
70 */
71 protected $value = NULL;
72
73 /**
74 * Short help message for this argument
75 * @var string Short help message for this argument
76 */
77 protected $shortHelpMessage = NULL;
78
79 /**
80 * The argument is valid
81 * @var boolean
82 */
83 protected $isValid = TRUE;
84
85 /**
86 * Any error (F3_FLOW3_Error_Error) that occured while initializing this argument (e.g. a mapping error)
87 * @var array
88 */
89 protected $errors = array();
90
91 /**
92 * Any warning (F3_FLOW3_Error_Warning) that occured while initializing this argument (e.g. a mapping warning)
93 * @var array
94 */
95 protected $warnings = array();
96
97 /**
98 * The property validator for this argument
99 * @var F3_FLOW3_Validation_ValidatorInterface
100 */
101 protected $validator = NULL;
102
103 /**
104 * The property validator for this arguments datatype
105 * @var F3_FLOW3_Validation_ValidatorInterface
106 */
107 protected $datatypeValidator = NULL;
108
109 /**
110 * The filter for this argument
111 * @var F3_FLOW3_Validation_FilterInterface
112 */
113 protected $filter = NULL;
114
115 /**
116 * The property converter for this argument
117 * @var F3_FLOW3_Property_ConverterInterface
118 */
119 protected $propertyConverter = NULL;
120
121 /**
122 * The property converter's input format for this argument
123 * @var string
124 */
125 protected $propertyConverterInputFormat = 'string';
126
127 /**
128 * Uid for the argument, if it has one
129 * @var string
130 */
131 protected $uid = NULL;
132
133 /**
134 * Constructs this controller argument
135 *
136 * @param string $name Name of this argument
137 * @param string $dataType The data type of this argument
138 * @param F3_FLOW3_Object_ManagerInterface The object manager
139 * @throws InvalidArgumentException if $name is not a string or empty
140 * @author Robert Lemke <robert@typo3.org>
141 */
142 public function __construct($name, $dataType = 'Text', F3_FLOW3_Object_ManagerInterface $objectManager) {
143 if (!is_string($name) || strlen($name) < 1) throw new InvalidArgumentException('$name must be of type string, ' . gettype($name) . ' given.', 1187951688);
144 $this->objectManager = $objectManager;
145 $this->objectFactory = $this->objectManager->getObjectFactory();
146 $this->name = $name;
147
148 $this->setDataType($dataType);
149 }
150
151 /**
152 * Returns the name of this argument
153 *
154 * @return string This argument's name
155 * @author Robert Lemke <robert@typo3.org>
156 */
157 public function getName() {
158 return $this->name;
159 }
160
161 /**
162 * Sets the short name of this argument.
163 *
164 * @param string $shortName A "short name" - a single character
165 * @return TX_EXTMVC_Controller_Argument $this
166 * @throws InvalidArgumentException if $shortName is not a character
167 * @author Robert Lemke <robert@typo3.org>
168 */
169 public function setShortName($shortName) {
170 if ($shortName !== NULL && (!is_string($shortName) || strlen($shortName) != 1)) throw new InvalidArgumentException('$shortName must be a single character or NULL', 1195824959);
171 $this->shortName = $shortName;
172 return $this;
173 }
174
175 /**
176 * Returns the short name of this argument
177 *
178 * @return string This argument's short name
179 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
180 */
181 public function getShortName() {
182 return $this->shortName;
183 }
184
185 /**
186 * Sets the data type of this argument's value
187 *
188 * @param string $dataType: Name of the data type
189 * @return TX_EXTMVC_Controller_Argument $this
190 * @author Robert Lemke <robert@typo3.org>
191 */
192 public function setDataType($dataType) {
193 $this->dataType = ($dataType != '' ? $dataType : 'Text');
194
195 $dataTypeValidatorClassName = $this->dataType;
196 if (!$this->objectManager->isObjectRegistered($dataTypeValidatorClassName)) $dataTypeValidatorClassName = 'TX_EXTMVC_Validation_Validator_' . $this->dataType;
197 $this->datatypeValidator = $this->objectManager->getObject($dataTypeValidatorClassName);
198
199 return $this;
200 }
201
202 /**
203 * Returns the data type of this argument's value
204 *
205 * @return string The data type
206 * @author Robert Lemke <robert@typo3.org>
207 */
208 public function getDataType() {
209 return $this->dataType;
210 }
211
212 /**
213 * Marks this argument to be required
214 *
215 * @param boolean $required TRUE if this argument should be required
216 * @return TX_EXTMVC_Controller_Argument $this
217 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
218 */
219 public function setRequired($required) {
220 $this->isRequired = $required;
221 return $this;
222 }
223
224 /**
225 * Returns TRUE if this argument is required
226 *
227 * @return boolean TRUE if this argument is required
228 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
229 */
230 public function isRequired() {
231 return $this->isRequired;
232 }
233
234 /**
235 * Sets the value of this argument.
236 *
237 * @param mixed $value: The value of this argument
238 * @return TX_EXTMVC_Controller_Argument $this
239 * @throws TX_EXTMVC_Exception_InvalidArgumentValue if the argument is not a valid object of type $dataType
240 * @author Robert Lemke <robert@typo3.org>
241 */
242 public function setValue($value) {
243 $this->value = $value;
244 return $this;
245 }
246
247 /**
248 * Returns the value of this argument
249 *
250 * @return object The value of this argument - if none was set, NULL is returned
251 * @author Robert Lemke <robert@typo3.org>
252 */
253 public function getValue() {
254 return $this->value;
255 }
256
257 /**
258 * Checks if this argument has a value set.
259 *
260 * @return boolean TRUE if a value was set, otherwise FALSE
261 * @author Robert Lemke <robert@typo3.org>
262 */
263 public function isValue() {
264 return $this->value !== NULL;
265 }
266
267 /**
268 * Sets a short help message for this argument. Mainly used at the command line, but maybe
269 * used elsewhere, too.
270 *
271 * @param string $message: A short help message
272 * @return TX_EXTMVC_Controller_Argument $this
273 * @author Robert Lemke <robert@typo3.org>
274 */
275 public function setShortHelpMessage($message) {
276 if (!is_string($message)) throw new InvalidArgumentException('The help message must be of type string, ' . gettype($message) . 'given.', 1187958170);
277 $this->shortHelpMessage = $message;
278 return $this;
279 }
280
281 /**
282 * Returns the short help message
283 *
284 * @return string The short help message
285 * @author Robert Lemke <robert@typo3.org>
286 */
287 public function getShortHelpMessage() {
288 return $this->shortHelpMessage;
289 }
290
291 /**
292 * Set the validity status of the argument
293 *
294 * @param boolean TRUE if the argument is valid, FALSE otherwise
295 * @return void
296 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
297 */
298 public function setValidity($isValid) {
299 $this->isValid = $isValid;
300 }
301
302 /**
303 * Returns TRUE when the argument is valid
304 *
305 * @return boolean TRUE if the argument is valid
306 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
307 */
308 public function isValid() {
309 return $this->isValid;
310 }
311
312 /**
313 * Add an initialization error (e.g. a mapping error)
314 *
315 * @param F3_FLOW3_Error_Error An error object
316 * @return void
317 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
318 */
319 public function addError(F3_FLOW3_Error_Error $error) {
320 $this->errors[] = $error;
321 }
322
323 /**
324 * Get all initialization errors
325 *
326 * @return array An array containing F3_FLOW3_Error_Error objects
327 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
328 * @see addError(F3_FLOW3_Error_Error $error)
329 */
330 public function getErrors() {
331 return $this->errors;
332 }
333
334 /**
335 * Add an initialization warning (e.g. a mapping warning)
336 *
337 * @param F3_FLOW3_Error_Warning A warning object
338 * @return void
339 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
340 */
341 public function addWarning(F3_FLOW3_Error_Warning $warning) {
342 $this->warnings[] = $warning;
343 }
344
345 /**
346 * Get all initialization warnings
347 *
348 * @return array An array containing F3_FLOW3_Error_Warning objects
349 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
350 * @see addWarning(F3_FLOW3_Error_Warning $warning)
351 */
352 public function getWarnings() {
353 return $this->warnings;
354 }
355
356 /**
357 * Set an additional validator
358 *
359 * @param string Class name of a validator
360 * @return TX_EXTMVC_Controller_Argument Returns $this (used for fluent interface)
361 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
362 */
363 public function setValidator($className) {
364 $this->validator = $this->objectManager->getObject($className);
365 return $this;
366 }
367
368 /**
369 * Returns the set validator
370 *
371 * @return F3_FLOW3_Validation_ValidatorInterface The set validator, NULL if none was set
372 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
373 */
374 public function getValidator() {
375 return $this->validator;
376 }
377
378 /**
379 * Returns the set datatype validator
380 *
381 * @return F3_FLOW3_Validation_ValidatorInterface The set datatype validator
382 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
383 */
384 public function getDatatypeValidator() {
385 return $this->datatypeValidator;
386 }
387
388 /**
389 * Set a filter
390 *
391 * @param string Class name of a filter
392 * @return TX_EXTMVC_Controller_Argument Returns $this (used for fluent interface)
393 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
394 */
395 public function setFilter($className) {
396 $this->filter = $this->objectManager->getObject($className);
397 return $this;
398 }
399
400 /**
401 * Create and set a filter chain
402 *
403 * @param array Class names of the filters
404 * @return TX_EXTMVC_Controller_Argument Returns $this (used for fluent interface)
405 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
406 */
407 public function setNewFilterChain(array $classNames) {
408 $this->filter = $this->createNewFilterChainObject();
409
410 foreach ($classNames as $className) {
411 if (!$this->objectManager->isObjectRegistered($className)) $className = 'F3_FLOW3_Validation_Filter\\' . $className;
412 $this->filter->addFilter($this->objectManager->getObject($className));
413 }
414
415 return $this;
416 }
417
418 /**
419 * Create and set a validator chain
420 *
421 * @param array Class names of the validators
422 * @return TX_EXTMVC_Controller_Argument Returns $this (used for fluent interface)
423 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
424 */
425 public function setNewValidatorChain(array $classNames) {
426 $this->validator = $this->createNewValidatorChainObject();
427
428 foreach ($classNames as $className) {
429 if (!$this->objectManager->isObjectRegistered($className)) $className = 'F3_FLOW3_Validation_Validator\\' . $className;
430 $this->validator->addValidator($this->objectManager->getObject($className));
431 }
432
433 return $this;
434 }
435
436 /**
437 * Returns the set filter
438 *
439 * @return F3_FLOW3_Validation_FilterInterface The set filter, NULL if none was set
440 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
441 */
442 public function getFilter() {
443 return $this->filter;
444 }
445
446 /**
447 * Set a property converter
448 *
449 * @param string Class name of a property converter
450 * @return TX_EXTMVC_Controller_Argument Returns $this (used for fluent interface)
451 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
452 */
453 public function setPropertyConverter($className) {
454 if (is_string($className)) {
455 $this->propertyConverter = t3lib_div::makeInstance($className);
456 } else {
457 $this->propertyConverter = $className;
458 }
459 return $this;
460 }
461
462 /**
463 * Returns the set property converter
464 *
465 * @return F3_FLOW3_Property_ConverterInterface The set property convertr, NULL if none was set
466 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
467 */
468 public function getPropertyConverter() {
469 return $this->propertyConverter;
470 }
471
472 /**
473 * Set a property converter input format
474 *
475 * @param string Input format the property converter should use
476 * @return TX_EXTMVC_Controller_Argument Returns $this (used for fluent interface)
477 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
478 */
479 public function setPropertyConverterInputFormat($format) {
480 $this->propertyConverterInputFormat = $format;
481 return $this;
482 }
483
484 /**
485 * Returns the set property converter input format
486 *
487 * @return string The set property converter input format
488 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
489 */
490 public function getPropertyConverterInputFormat() {
491 return $this->propertyConverterInputFormat;
492 }
493
494 /**
495 * Set the uid for the argument.
496 *
497 * @param string $uid The uid for the argument.
498 * @return void
499 * @author Sebastian Kurfürst <sebastian@typo3.org>
500 */
501 public function setUid($uid) {
502 $this->uid = $uid;
503 }
504
505 /**
506 * Get the uid of the argument, if it has one.
507 *
508 * @return string Uid of the argument. If none set, returns NULL.
509 * @author Sebastian Kurfürst <sebastian@typo3.org>
510 */
511 public function getUid() {
512 return $this->uid;
513 }
514
515 /**
516 * Factory method that creates a new filter chain
517 *
518 * @return F3_FLOW3_Validation_Filter_Chain A new filter chain
519 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
520 */
521 public function createNewFilterChainObject() {
522 return t3lib_div::makeInstance('F3_FLOW3_Validation_Filter_Chain');
523 }
524
525 /**
526 * Factory method that creates a new validator chain
527 *
528 * @return F3_FLOW3_Validation_Validator_Chain A new validator chain
529 * @author Andreas Förthner <andreas.foerthner@netlogix.de>
530 */
531 public function createNewValidatorChainObject() {
532 return t3lib_div::makeInstance('F3_FLOW3_Validation_Validator_Chain');
533 }
534
535 /**
536 * Returns a string representation of this argument's value
537 *
538 * @return string
539 * @author Robert Lemke <robert@typo3.org>
540 */
541 public function __toString() {
542 return (string)$this->value;
543 }
544 }
545 ?>