Add Extbase 1.0.1 to TYPO3core. Do NOT make changes inside! See misc/core_svn_rules...
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / MVC / Controller / ArgumentsValidator.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 validator for controller arguments
30 *
31 * @package Extbase
32 * @subpackage MVC\Controller
33 * @version $ID:$
34 * @scope prototype
35 */
36 class Tx_Extbase_MVC_Controller_ArgumentsValidator extends Tx_Extbase_Validation_Validator_AbstractObjectValidator {
37
38 /**
39 * Checks if the given value (ie. an Arguments object) is valid.
40 *
41 * If at least one error occurred, the result is FALSE and any errors can
42 * be retrieved through the getErrors() method.
43 *
44 * @param object $arguments The arguments object that should be validated
45 * @return boolean TRUE if all arguments are valid, FALSE if an error occured
46 */
47 public function isValid($arguments) {
48 if (!$arguments instanceof Tx_Extbase_MVC_Controller_Arguments) throw new InvalidArgumentException('Expected Tx_Extbase_MVC_Controller_Arguments, ' . gettype($arguments) . ' given.', 1241079561);
49 $this->errors = array();
50
51 $result = TRUE;
52 foreach ($arguments->getArgumentNames() as $argumentName) {
53 if ($this->isPropertyValid($arguments, $argumentName) === FALSE) {
54 $result = FALSE;
55 }
56 }
57 return $result;
58 }
59
60 /**
61 * Checks the given object can be validated by the validator implementation
62 *
63 * @param object $object The object to be checked
64 * @return boolean TRUE if this validator can validate instances of the given object or FALSE if it can't
65 */
66 public function canValidate($object) {
67 return ($object instanceof Tx_Extbase_MVC_Controller_Arguments);
68 }
69
70 /**
71 * Checks if the specified property (ie. the argument) of the given arguments
72 * object is valid. Validity is checked by first invoking the validation chain
73 * defined in the argument object.
74 *
75 * If at least one error occurred, the result is FALSE.
76 *
77 * @param object $arguments The arguments object containing the property (argument) to validate
78 * @param string $argumentName Name of the property (ie. name of the argument) to validate
79 * @return boolean TRUE if the argument is valid, FALSE if an error occured
80 */
81 public function isPropertyValid($arguments, $argumentName) {
82 if (!$arguments instanceof Tx_Extbase_MVC_Controller_Arguments) throw new InvalidArgumentException('Expected Tx_Extbase_MVC_Controller_Arguments, ' . gettype($arguments) . ' given.', 1241079562);
83 $argument = $arguments[$argumentName];
84
85 $validatorConjunction = $argument->getValidator();
86 if ($validatorConjunction === NULL) return TRUE;
87
88 $argumentValue = $argument->getValue();
89 if ($argumentValue === $argument->getDefaultValue() && $argument->isRequired() === FALSE) return TRUE;
90
91 if ($validatorConjunction->isValid($argumentValue) === FALSE) {
92 $this->addErrorsForArgument($validatorConjunction->getErrors(), $argumentName);
93 return FALSE;
94 }
95 return TRUE;
96 }
97
98 /**
99 * Adds the given errors to $this->errors and creates an ArgumentError
100 * instance if needed.
101 *
102 * @param array $errors Array of \F3\FLOW3\Validation\Error
103 * @param string $argumentName Name of the argument to add errors for
104 * @return void
105 */
106 protected function addErrorsForArgument(array $errors, $argumentName) {
107 if (!isset($this->errors[$argumentName])) {
108 $this->errors[$argumentName] = t3lib_div::makeInstance('Tx_Extbase_MVC_Controller_ArgumentError', $argumentName);
109 }
110 $this->errors[$argumentName]->addErrors($errors);
111 }
112
113 }
114 ?>