TYPO3 API  SVNRelease
ArgumentsValidator.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 validator for controller arguments
00030  *
00031  * @package Extbase
00032  * @subpackage MVC\Controller
00033  * @version $ID:$
00034  * @scope prototype
00035  */
00036 class Tx_Extbase_MVC_Controller_ArgumentsValidator extends Tx_Extbase_Validation_Validator_AbstractObjectValidator {
00037 
00038     /**
00039      * Checks if the given value (ie. an Arguments object) is valid.
00040      *
00041      * If at least one error occurred, the result is FALSE and any errors can
00042      * be retrieved through the getErrors() method.
00043      *
00044      * @param object $arguments The arguments object that should be validated
00045      * @return boolean TRUE if all arguments are valid, FALSE if an error occured
00046      */
00047     public function isValid($arguments) {
00048         if (!$arguments instanceof Tx_Extbase_MVC_Controller_Arguments) throw new InvalidArgumentException('Expected Tx_Extbase_MVC_Controller_Arguments, ' . gettype($arguments) . ' given.', 1241079561);
00049         $this->errors = array();
00050 
00051         $result = TRUE;
00052         foreach ($arguments->getArgumentNames() as $argumentName) {
00053             if ($this->isPropertyValid($arguments, $argumentName) === FALSE) {
00054                 $result = FALSE;
00055             }
00056         }
00057         return $result;
00058     }
00059 
00060     /**
00061      * Checks the given object can be validated by the validator implementation
00062      *
00063      * @param object $object The object to be checked
00064      * @return boolean TRUE if this validator can validate instances of the given object or FALSE if it can't
00065      */
00066     public function canValidate($object) {
00067         return ($object instanceof Tx_Extbase_MVC_Controller_Arguments);
00068     }
00069 
00070     /**
00071      * Checks if the specified property (ie. the argument) of the given arguments
00072      * object is valid. Validity is checked by first invoking the validation chain
00073      * defined in the argument object.
00074      *
00075      * If at least one error occurred, the result is FALSE.
00076      *
00077      * @param object $arguments The arguments object containing the property (argument) to validate
00078      * @param string $argumentName Name of the property (ie. name of the argument) to validate
00079      * @return boolean TRUE if the argument is valid, FALSE if an error occured
00080      */
00081     public function isPropertyValid($arguments, $argumentName) {
00082         if (!$arguments instanceof Tx_Extbase_MVC_Controller_Arguments) throw new InvalidArgumentException('Expected Tx_Extbase_MVC_Controller_Arguments, ' . gettype($arguments) . ' given.', 1241079562);
00083         $argument = $arguments[$argumentName];
00084 
00085         $validatorConjunction = $argument->getValidator();
00086         if ($validatorConjunction === NULL) return TRUE;
00087 
00088         $argumentValue = $argument->getValue();
00089         if ($argumentValue === $argument->getDefaultValue() && $argument->isRequired() === FALSE) return TRUE;
00090 
00091         if ($validatorConjunction->isValid($argumentValue) === FALSE) {
00092             $this->addErrorsForArgument($validatorConjunction->getErrors(), $argumentName);
00093             return FALSE;
00094         }
00095         return TRUE;
00096     }
00097 
00098     /**
00099      * Adds the given errors to $this->errors and creates an ArgumentError
00100      * instance if needed.
00101      *
00102      * @param array $errors Array of \F3\FLOW3\Validation\Error
00103      * @param string $argumentName Name of the argument to add errors for
00104      * @return void
00105      */
00106     protected function addErrorsForArgument(array $errors, $argumentName) {
00107         if (!isset($this->errors[$argumentName])) {
00108             $this->errors[$argumentName] = new Tx_Extbase_MVC_Controller_ArgumentError($argumentName);
00109         }
00110         $this->errors[$argumentName]->addErrors($errors);
00111     }
00112 
00113 }
00114 ?>