2e8e37798a03d68b9ac59590585b6e1dad9df907
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Validation / Validator / GenericObjectValidator.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 generic object validator which allows for specifying property validators
30 *
31 * @package Extbase
32 * @subpackage Validation\Validator
33 * @version $Id$
34 * @scope prototype
35 */
36 class Tx_Extbase_Validation_Validator_GenericObjectValidator extends Tx_Extbase_Validation_Validator_AbstractObjectValidator {
37
38 /**
39 * @var array
40 */
41 protected $propertyValidators = array();
42
43
44 /**
45 *
46 * @var Tx_Extbase_Persistence_ObjectStorage
47 */
48 static protected $instancesCurrentlyUnderValidation;
49
50 /**
51 * Checks if the given value is valid according to the property validators
52 *
53 * If at least one error occurred, the result is FALSE.
54 *
55 * @param mixed $value The value that should be validated
56 * @return Tx_Extbase_Error_Result
57 * @api
58 */
59 public function validate($object) {
60 $messages = new Tx_Extbase_Error_Result();
61
62 if (self::$instancesCurrentlyUnderValidation === NULL) {
63 self::$instancesCurrentlyUnderValidation = new Tx_Extbase_Persistence_ObjectStorage();
64 }
65
66 if ($object === NULL) {
67 return $messages;
68 }
69
70 if (!is_object($object)) {
71 $messages->addError(new Tx_Extbase_Validation_Error('Object expected, ' . gettype($object) . ' given.', 1241099149));
72 return $messages;
73 }
74
75 if (self::$instancesCurrentlyUnderValidation->contains($object)) {
76 return $messages;
77 } else {
78 self::$instancesCurrentlyUnderValidation->attach($object);
79 }
80
81 foreach ($this->propertyValidators as $propertyName => $validators) {
82 $propertyValue = $this->getPropertyValue($object, $propertyName);
83 $this->checkProperty($propertyValue, $validators, $messages->forProperty($propertyName));
84 }
85
86 self::$instancesCurrentlyUnderValidation->detach($object);
87 return $messages;
88 }
89
90 /**
91 * Load the property value to be used for validation.
92 *
93 * In case the object is a doctrine proxy, we need to load the real instance first.
94 *
95 * @param object $object
96 * @param string $propertyName
97 * @return mixed
98 */
99 protected function getPropertyValue($object, $propertyName) {
100 // TODO: add support for lazy loading proxies, if needed
101
102 if (Tx_Extbase_Reflection_ObjectAccess::isPropertyGettable($object, $propertyName)) {
103 return Tx_Extbase_Reflection_ObjectAccess::getProperty($object, $propertyName);
104 } else {
105 return Tx_Extbase_Reflection_ObjectAccess::getProperty($object, $propertyName, TRUE);
106 }
107 }
108
109 /**
110 * Checks if the specified property of the given object is valid, and adds
111 * found errors to the $messages object.
112 *
113 * @param mixed $value The value to be validated
114 * @param array $validators The validators to be called on the value
115 * @param Tx_Extbase_Error_Result $messages the result object to which the validation errors should be added
116 * @return void
117 * @author Sebastian Kurf├╝rst <sebastian@typo3.org>
118 */
119
120 protected function checkProperty($value, $validators, Tx_Extbase_Error_Result $messages) {
121 foreach ($validators as $validator) {
122 $messages->merge($validator->validate($value));
123 }
124 }
125
126 /**
127 * Checks if the given value is valid according to the property validators
128 *
129 * If at least one error occurred, the result is FALSE.
130 *
131 * @param mixed $value The value that should be validated
132 * @return boolean TRUE if the value is valid, FALSE if an error occured
133 * @api
134 * @deprecated since Extbase 1.4.0, will be removed in Extbase 6.0
135 */
136 public function isValid($value) {
137 if (!is_object($value)) {
138 $this->addError('Value is no object.', 1241099148);
139 return FALSE;
140 }
141
142 $result = TRUE;
143 foreach (array_keys($this->propertyValidators) as $propertyName) {
144 if ($this->isPropertyValid($value, $propertyName) === FALSE) {
145 $result = FALSE;
146 }
147 }
148 return $result;
149 }
150
151 /**
152 * Checks the given object can be validated by the validator implementation
153 *
154 * @param object $object The object to be checked
155 * @return boolean TRUE if the given value is an object
156 * @api
157 */
158 public function canValidate($object) {
159 return is_object($object);
160 }
161
162 /**
163 * Checks if the specified property of the given object is valid.
164 *
165 * If at least one error occurred, the result is FALSE.
166 *
167 * @param object $object The object containing the property to validate
168 * @param string $propertyName Name of the property to validate
169 * @return boolean TRUE if the property value is valid, FALSE if an error occured
170 * @api
171 * @deprecated since Extbase 1.4.0, will be removed in Extbase 6.0
172 */
173 public function isPropertyValid($object, $propertyName) {
174 if (!is_object($object)) throw new InvalidArgumentException('Object expected, ' . gettype($object) . ' given.', 1241099149);
175 if (!isset($this->propertyValidators[$propertyName])) return TRUE;
176
177 $result = TRUE;
178 foreach ($this->propertyValidators[$propertyName] as $validator) {
179 if ($validator->isValid(Tx_Extbase_Reflection_ObjectAccess::getProperty($object, $propertyName)) === FALSE) {
180 $this->addErrorsForProperty($validator->getErrors(), $propertyName);
181 $result = FALSE;
182 }
183 }
184 return $result;
185 }
186
187 /**
188 * @param array $errors Array of Tx_Extbase_Validation_Error
189 * @param string $propertyName Name of the property to add errors
190 * @return void
191 * @deprecated since Extbase 1.4.0, will be removed in Extbase 6.0
192 */
193 protected function addErrorsForProperty($errors, $propertyName) {
194 if (!isset($this->errors[$propertyName])) {
195 $this->errors[$propertyName] = new Tx_Extbase_Validation_PropertyError($propertyName);
196 }
197 $this->errors[$propertyName]->addErrors($errors);
198 }
199
200 /**
201 * Adds the given validator for validation of the specified property.
202 *
203 * @param string $propertyName Name of the property to validate
204 * @param Tx_Extbase_Validation_Validator_ValidatorInterface $validator The property validator
205 * @return void
206 * @api
207 */
208 public function addPropertyValidator($propertyName, Tx_Extbase_Validation_Validator_ValidatorInterface $validator) {
209 if (!isset($this->propertyValidators[$propertyName])) {
210 $this->propertyValidators[$propertyName] = new Tx_Extbase_Persistence_ObjectStorage;
211 }
212 $this->propertyValidators[$propertyName]->attach($validator);
213 }
214 }
215
216 ?>