web/lib/Zend/Validate.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Validate
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Validate.php 21339 2010-03-05 15:32:25Z thomas $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Validate_Interface
       
    24  */
       
    25 require_once 'Zend/Validate/Interface.php';
       
    26 
       
    27 /**
       
    28  * @category   Zend
       
    29  * @package    Zend_Validate
       
    30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    32  */
       
    33 class Zend_Validate implements Zend_Validate_Interface
       
    34 {
       
    35     /**
       
    36      * Validator chain
       
    37      *
       
    38      * @var array
       
    39      */
       
    40     protected $_validators = array();
       
    41 
       
    42     /**
       
    43      * Array of validation failure messages
       
    44      *
       
    45      * @var array
       
    46      */
       
    47     protected $_messages = array();
       
    48 
       
    49     /**
       
    50      * Default Namespaces
       
    51      *
       
    52      * @var array
       
    53      */
       
    54     protected static $_defaultNamespaces = array();
       
    55 
       
    56     /**
       
    57      * Array of validation failure message codes
       
    58      *
       
    59      * @var array
       
    60      * @deprecated Since 1.5.0
       
    61      */
       
    62     protected $_errors = array();
       
    63 
       
    64     /**
       
    65      * Adds a validator to the end of the chain
       
    66      *
       
    67      * If $breakChainOnFailure is true, then if the validator fails, the next validator in the chain,
       
    68      * if one exists, will not be executed.
       
    69      *
       
    70      * @param  Zend_Validate_Interface $validator
       
    71      * @param  boolean                 $breakChainOnFailure
       
    72      * @return Zend_Validate Provides a fluent interface
       
    73      */
       
    74     public function addValidator(Zend_Validate_Interface $validator, $breakChainOnFailure = false)
       
    75     {
       
    76         $this->_validators[] = array(
       
    77             'instance' => $validator,
       
    78             'breakChainOnFailure' => (boolean) $breakChainOnFailure
       
    79             );
       
    80         return $this;
       
    81     }
       
    82 
       
    83     /**
       
    84      * Returns true if and only if $value passes all validations in the chain
       
    85      *
       
    86      * Validators are run in the order in which they were added to the chain (FIFO).
       
    87      *
       
    88      * @param  mixed $value
       
    89      * @return boolean
       
    90      */
       
    91     public function isValid($value)
       
    92     {
       
    93         $this->_messages = array();
       
    94         $this->_errors   = array();
       
    95         $result = true;
       
    96         foreach ($this->_validators as $element) {
       
    97             $validator = $element['instance'];
       
    98             if ($validator->isValid($value)) {
       
    99                 continue;
       
   100             }
       
   101             $result = false;
       
   102             $messages = $validator->getMessages();
       
   103             $this->_messages = array_merge($this->_messages, $messages);
       
   104             $this->_errors   = array_merge($this->_errors,   array_keys($messages));
       
   105             if ($element['breakChainOnFailure']) {
       
   106                 break;
       
   107             }
       
   108         }
       
   109         return $result;
       
   110     }
       
   111 
       
   112     /**
       
   113      * Defined by Zend_Validate_Interface
       
   114      *
       
   115      * Returns array of validation failure messages
       
   116      *
       
   117      * @return array
       
   118      */
       
   119     public function getMessages()
       
   120     {
       
   121         return $this->_messages;
       
   122     }
       
   123 
       
   124     /**
       
   125      * Defined by Zend_Validate_Interface
       
   126      *
       
   127      * Returns array of validation failure message codes
       
   128      *
       
   129      * @return array
       
   130      * @deprecated Since 1.5.0
       
   131      */
       
   132     public function getErrors()
       
   133     {
       
   134         return $this->_errors;
       
   135     }
       
   136 
       
   137     /**
       
   138      * Returns the set default namespaces
       
   139      *
       
   140      * @return array
       
   141      */
       
   142     public static function getDefaultNamespaces()
       
   143     {
       
   144         return self::$_defaultNamespaces;
       
   145     }
       
   146 
       
   147     /**
       
   148      * Sets new default namespaces
       
   149      *
       
   150      * @param array|string $namespace
       
   151      * @return null
       
   152      */
       
   153     public static function setDefaultNamespaces($namespace)
       
   154     {
       
   155         if (!is_array($namespace)) {
       
   156             $namespace = array((string) $namespace);
       
   157         }
       
   158 
       
   159         self::$_defaultNamespaces = $namespace;
       
   160     }
       
   161 
       
   162     /**
       
   163      * Adds a new default namespace
       
   164      *
       
   165      * @param array|string $namespace
       
   166      * @return null
       
   167      */
       
   168     public static function addDefaultNamespaces($namespace)
       
   169     {
       
   170         if (!is_array($namespace)) {
       
   171             $namespace = array((string) $namespace);
       
   172         }
       
   173 
       
   174         self::$_defaultNamespaces = array_unique(array_merge(self::$_defaultNamespaces, $namespace));
       
   175     }
       
   176 
       
   177     /**
       
   178      * Returns true when defaultNamespaces are set
       
   179      *
       
   180      * @return boolean
       
   181      */
       
   182     public static function hasDefaultNamespaces()
       
   183     {
       
   184         return (!empty(self::$_defaultNamespaces));
       
   185     }
       
   186 
       
   187     /**
       
   188      * @param  mixed    $value
       
   189      * @param  string   $classBaseName
       
   190      * @param  array    $args          OPTIONAL
       
   191      * @param  mixed    $namespaces    OPTIONAL
       
   192      * @return boolean
       
   193      * @throws Zend_Validate_Exception
       
   194      */
       
   195     public static function is($value, $classBaseName, array $args = array(), $namespaces = array())
       
   196     {
       
   197         $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('Zend_Validate'));
       
   198         $className  = ucfirst($classBaseName);
       
   199         try {
       
   200             if (!class_exists($className, false)) {
       
   201                 require_once 'Zend/Loader.php';
       
   202                 foreach($namespaces as $namespace) {
       
   203                     $class = $namespace . '_' . $className;
       
   204                     $file  = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
       
   205                     if (Zend_Loader::isReadable($file)) {
       
   206                         Zend_Loader::loadClass($class);
       
   207                         $className = $class;
       
   208                         break;
       
   209                     }
       
   210                 }
       
   211             }
       
   212 
       
   213             $class = new ReflectionClass($className);
       
   214             if ($class->implementsInterface('Zend_Validate_Interface')) {
       
   215                 if ($class->hasMethod('__construct')) {
       
   216                     $keys    = array_keys($args);
       
   217                     $numeric = false;
       
   218                     foreach($keys as $key) {
       
   219                         if (is_numeric($key)) {
       
   220                             $numeric = true;
       
   221                             break;
       
   222                         }
       
   223                     }
       
   224 
       
   225                     if ($numeric) {
       
   226                         $object = $class->newInstanceArgs($args);
       
   227                     } else {
       
   228                         $object = $class->newInstance($args);
       
   229                     }
       
   230                 } else {
       
   231                     $object = $class->newInstance();
       
   232                 }
       
   233 
       
   234                 return $object->isValid($value);
       
   235             }
       
   236         } catch (Zend_Validate_Exception $ze) {
       
   237             // if there is an exception while validating throw it
       
   238             throw $ze;
       
   239         } catch (Exception $e) {
       
   240             // fallthrough and continue for missing validation classes
       
   241         }
       
   242 
       
   243         require_once 'Zend/Validate/Exception.php';
       
   244         throw new Zend_Validate_Exception("Validate class not found from basename '$classBaseName'");
       
   245     }
       
   246 
       
   247     /**
       
   248      * Returns the maximum allowed message length
       
   249      *
       
   250      * @return integer
       
   251      */
       
   252     public static function getMessageLength()
       
   253     {
       
   254         require_once 'Zend/Validate/Abstract.php';
       
   255         return Zend_Validate_Abstract::getMessageLength();
       
   256     }
       
   257 
       
   258     /**
       
   259      * Sets the maximum allowed message length
       
   260      *
       
   261      * @param integer $length
       
   262      */
       
   263     public static function setMessageLength($length = -1)
       
   264     {
       
   265         require_once 'Zend/Validate/Abstract.php';
       
   266         Zend_Validate_Abstract::setMessageLength($length);
       
   267     }
       
   268 
       
   269     /**
       
   270      * Returns the default translation object
       
   271      *
       
   272      * @return Zend_Translate_Adapter|null
       
   273      */
       
   274     public static function getDefaultTranslator($translator = null)
       
   275     {
       
   276         require_once 'Zend/Validate/Abstract.php';
       
   277         return Zend_Validate_Abstract::getDefaultTranslator();
       
   278     }
       
   279 
       
   280     /**
       
   281      * Sets a default translation object for all validation objects
       
   282      *
       
   283      * @param Zend_Translate|Zend_Translate_Adapter|null $translator
       
   284      */
       
   285     public static function setDefaultTranslator($translator = null)
       
   286     {
       
   287         require_once 'Zend/Validate/Abstract.php';
       
   288         Zend_Validate_Abstract::setDefaultTranslator($translator);
       
   289     }
       
   290 }