2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
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.
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
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.
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
29 * A generic object validator which allows for specifying property validators
32 * @subpackage Validation\Validator
36 class Tx_Extbase_Validation_Validator_GenericObjectValidator
extends Tx_Extbase_Validation_Validator_AbstractObjectValidator
{
41 protected $propertyValidators = array();
46 * @var Tx_Extbase_Persistence_ObjectStorage
48 static protected $instancesCurrentlyUnderValidation;
51 * Checks if the given value is valid according to the property validators
53 * If at least one error occurred, the result is FALSE.
55 * @param mixed $value The value that should be validated
56 * @return Tx_Extbase_Error_Result
59 public function validate($object) {
60 $messages = new Tx_Extbase_Error_Result();
62 if (self
::$instancesCurrentlyUnderValidation === NULL) {
63 self
::$instancesCurrentlyUnderValidation = new Tx_Extbase_Persistence_ObjectStorage();
66 if ($object === NULL) {
70 if (!is_object($object)) {
71 $messages->addError(new Tx_Extbase_Validation_Error('Object expected, "%1$d" given.', 1241099149, array(gettype($object))));
75 if (self
::$instancesCurrentlyUnderValidation->contains($object)) {
78 self
::$instancesCurrentlyUnderValidation->attach($object);
81 foreach ($this->propertyValidators
as $propertyName => $validators) {
82 $propertyValue = $this->getPropertyValue($object, $propertyName);
83 $this->checkProperty($propertyValue, $validators, $messages->forProperty($propertyName));
86 self
::$instancesCurrentlyUnderValidation->detach($object);
91 * Load the property value to be used for validation.
93 * In case the object is a doctrine proxy, we need to load the real instance first.
95 * @param object $object
96 * @param string $propertyName
99 protected function getPropertyValue($object, $propertyName) {
100 // TODO: add support for lazy loading proxies, if needed
102 if (Tx_Extbase_Reflection_ObjectAccess
::isPropertyGettable($object, $propertyName)) {
103 return Tx_Extbase_Reflection_ObjectAccess
::getProperty($object, $propertyName);
105 return Tx_Extbase_Reflection_ObjectAccess
::getProperty($object, $propertyName, TRUE);
110 * Checks if the specified property of the given object is valid, and adds
111 * found errors to the $messages object.
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
117 * @author Sebastian Kurfürst <sebastian@typo3.org>
120 protected function checkProperty($value, $validators, Tx_Extbase_Error_Result
$messages) {
121 foreach ($validators as $validator) {
122 $messages->merge($validator->validate($value));
127 * Checks if the given value is valid according to the property validators
129 * If at least one error occurred, the result is FALSE.
131 * @param mixed $value The value that should be validated
132 * @return boolean TRUE if the value is valid, FALSE if an error occured
134 * @deprecated since Extbase 1.4.0, will be removed in Extbase 6.0
136 public function isValid($value) {
137 if (!is_object($value)) {
138 $this->addError('Value is no object.', 1241099148);
143 foreach (array_keys($this->propertyValidators
) as $propertyName) {
144 if ($this->isPropertyValid($value, $propertyName) === FALSE) {
152 * Checks the given object can be validated by the validator implementation
154 * @param object $object The object to be checked
155 * @return boolean TRUE if the given value is an object
158 public function canValidate($object) {
159 return is_object($object);
163 * Checks if the specified property of the given object is valid.
165 * If at least one error occurred, the result is FALSE.
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
171 * @deprecated since Extbase 1.4.0, will be removed in Extbase 6.0
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;
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);
188 * @param array $errors Array of Tx_Extbase_Validation_Error
189 * @param string $propertyName Name of the property to add errors
191 * @deprecated since Extbase 1.4.0, will be removed in Extbase 6.0
193 protected function addErrorsForProperty($errors, $propertyName) {
194 if (!isset($this->errors
[$propertyName])) {
195 $this->errors
[$propertyName] = new Tx_Extbase_Validation_PropertyError($propertyName);
197 $this->errors
[$propertyName]->addErrors($errors);
201 * Adds the given validator for validation of the specified property.
203 * @param string $propertyName Name of the property to validate
204 * @param Tx_Extbase_Validation_Validator_ValidatorInterface $validator The property validator
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
;
212 $this->propertyValidators
[$propertyName]->attach($validator);