[CLEANUP] Replace wrong/old file copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Controller / ArgumentsValidator.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Mvc\Controller;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2012 Extbase Team (http://forge.typo3.org/projects/typo3v4-mvc)
8 * Extbase is a backport of TYPO3 Flow. All credits go to the TYPO3 Flow team.
9 * All rights reserved
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 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * A validator for controller arguments
32 *
33 * @deprecated since Extbase 1.4.0, will be removed in Extbase 6.1 Is only needed for old property mapper.
34 */
35 class ArgumentsValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractObjectValidator {
36
37 /**
38 * Checks if the given value (ie. an Arguments object) is valid.
39 *
40 * If at least one error occurred, the result is FALSE and any errors can
41 * be retrieved through the getErrors() method.
42 *
43 * @param object $arguments The arguments object that should be validated
44 * @throws \InvalidArgumentException
45 * @return boolean TRUE if all arguments are valid, FALSE if an error occured
46 */
47 public function isValid($arguments) {
48 if (!$arguments instanceof \TYPO3\CMS\Extbase\Mvc\Controller\Arguments) {
49 throw new \InvalidArgumentException('Expected TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments, ' . gettype($arguments) . ' given.', 1241079561);
50 }
51 $this->errors = array();
52 $result = TRUE;
53 foreach ($arguments->getArgumentNames() as $argumentName) {
54 if ($this->isPropertyValid($arguments, $argumentName) === FALSE) {
55 $result = FALSE;
56 }
57 }
58 return $result;
59 }
60
61 /**
62 * Checks the given object can be validated by the validator implementation
63 *
64 * @param object $object The object to be checked
65 * @return boolean TRUE if this validator can validate instances of the given object or FALSE if it can't
66 */
67 public function canValidate($object) {
68 return $object instanceof \TYPO3\CMS\Extbase\Mvc\Controller\Arguments;
69 }
70
71 /**
72 * Checks if the specified property (ie. the argument) of the given arguments
73 * object is valid. Validity is checked by first invoking the validation chain
74 * defined in the argument object.
75 *
76 * If at least one error occurred, the result is FALSE.
77 *
78 * @param object $arguments The arguments object containing the property (argument) to validate
79 * @param string $argumentName Name of the property (ie. name of the argument) to validate
80 * @throws \InvalidArgumentException
81 * @return boolean TRUE if the argument is valid, FALSE if an error occured
82 */
83 public function isPropertyValid($arguments, $argumentName) {
84 if (!$arguments instanceof \TYPO3\CMS\Extbase\Mvc\Controller\Arguments) {
85 throw new \InvalidArgumentException('Expected TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Arguments, ' . gettype($arguments) . ' given.', 1241079562);
86 }
87 $argument = $arguments[$argumentName];
88 $validatorConjunction = $argument->getValidator();
89 if ($validatorConjunction === NULL) {
90 return TRUE;
91 }
92 $argumentValue = $argument->getValue();
93 if ($argumentValue === $argument->getDefaultValue() && $argument->isRequired() === FALSE) {
94 return TRUE;
95 }
96 if ($validatorConjunction->isValid($argumentValue) === FALSE) {
97 $this->addErrorsForArgument($validatorConjunction->getErrors(), $argumentName);
98 return FALSE;
99 }
100 return TRUE;
101 }
102
103 /**
104 * Adds the given errors to $this->errors and creates an ArgumentError
105 * instance if needed.
106 *
107 * @param array $errors Array of \TYPO3\CMS\Extbase\Validation\Error
108 * @param string $argumentName Name of the argument to add errors for
109 * @return void
110 */
111 protected function addErrorsForArgument(array $errors, $argumentName) {
112 if (!isset($this->errors[$argumentName])) {
113 $this->errors[$argumentName] = new \TYPO3\CMS\Extbase\Mvc\Controller\ArgumentError($argumentName);
114 }
115 $this->errors[$argumentName]->addErrors($errors);
116 }
117 }
118
119 ?>