TYPO3 API  SVNRelease
GenericObjectValidator.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
00006 *  All rights reserved
00007 *
00008 *  This class is a backport of the corresponding class of FLOW3.
00009 *  All credits go to the v5 team.
00010 *
00011 *  This script is part of the TYPO3 project. The TYPO3 project is
00012 *  free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  The GNU General Public License can be found at
00018 *  http://www.gnu.org/copyleft/gpl.html.
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00028 /**
00029  * A generic object validator which allows for specifying property validators
00030  *
00031  * @package Extbase
00032  * @subpackage Validation\Validator
00033  * @version $Id: GenericObjectValidator.php 1948 2010-03-04 06:40:53Z jocrau $
00034  * @scope prototype
00035  */
00036 class Tx_Extbase_Validation_Validator_GenericObjectValidator extends Tx_Extbase_Validation_Validator_AbstractObjectValidator {
00037 
00038     /**
00039      * @var array
00040      */
00041     protected $propertyValidators = array();
00042 
00043     /**
00044      * Checks if the given value is valid according to the property validators
00045      *
00046      * If at least one error occurred, the result is FALSE.
00047      *
00048      * @param mixed $value The value that should be validated
00049      * @return boolean TRUE if the value is valid, FALSE if an error occured
00050      * @api
00051      */
00052     public function isValid($value) {
00053         if (!is_object($value)) {
00054             $this->addError('Value is no object.', 1241099148);
00055             return FALSE;
00056         }
00057 
00058         $result = TRUE;
00059         foreach (array_keys($this->propertyValidators) as $propertyName) {
00060             if ($this->isPropertyValid($value, $propertyName) === FALSE) {
00061                 $result = FALSE;
00062             }
00063         }
00064         return $result;
00065     }
00066 
00067     /**
00068      * Checks the given object can be validated by the validator implementation
00069      *
00070      * @param object $object The object to be checked
00071      * @return boolean TRUE if the given value is an object
00072      * @api
00073      */
00074     public function canValidate($object) {
00075         return is_object($object);
00076     }
00077 
00078     /**
00079      * Checks if the specified property of the given object is valid.
00080      *
00081      * If at least one error occurred, the result is FALSE.
00082      *
00083      * @param object $object The object containing the property to validate
00084      * @param string $propertyName Name of the property to validate
00085      * @return boolean TRUE if the property value is valid, FALSE if an error occured
00086      * @api
00087      */
00088     public function isPropertyValid($object, $propertyName) {
00089         if (!is_object($object)) throw new InvalidArgumentException('Object expected, ' . gettype($object) . ' given.', 1241099149);
00090         if (!isset($this->propertyValidators[$propertyName])) return TRUE;
00091 
00092         $result = TRUE;
00093         foreach ($this->propertyValidators[$propertyName] as $validator) {
00094             if ($validator->isValid(Tx_Extbase_Reflection_ObjectAccess::getProperty($object, $propertyName)) === FALSE) {
00095                 $this->addErrorsForProperty($validator->getErrors(), $propertyName);
00096                 $result = FALSE;
00097             }
00098         }
00099         return $result;
00100     }
00101 
00102     /**
00103      * @param array $errors Array of Tx_Extbase_Validation_Error
00104      * @param string $propertyName Name of the property to add errors
00105      * @return void
00106      */
00107     protected function addErrorsForProperty($errors, $propertyName) {
00108         if (!isset($this->errors[$propertyName])) {
00109             $this->errors[$propertyName] = new Tx_Extbase_Validation_PropertyError($propertyName);
00110         }
00111         $this->errors[$propertyName]->addErrors($errors);
00112     }
00113 
00114     /**
00115      * Adds the given validator for validation of the specified property.
00116      *
00117      * @param string $propertyName Name of the property to validate
00118      * @param Tx_Extbase_Validation_Validator_ValidatorInterface $validator The property validator
00119      * @return void
00120      * @api
00121      */
00122     public function addPropertyValidator($propertyName, Tx_Extbase_Validation_Validator_ValidatorInterface $validator) {
00123         if (!isset($this->propertyValidators[$propertyName])) {
00124             $this->propertyValidators[$propertyName] = new Tx_Extbase_Persistence_ObjectStorage;
00125         }
00126         $this->propertyValidators[$propertyName]->attach($validator);
00127     }
00128 }
00129 
00130 ?>