File indexing completed on 2024-04-28 06:00:03

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Validate
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Validate_Interface
0024  */
0025 // require_once 'Zend/Validate/Interface.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Validate
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Validate implements Zend_Validate_Interface
0034 {
0035     /**
0036      * Validator chain
0037      *
0038      * @var array
0039      */
0040     protected $_validators = array();
0041 
0042     /**
0043      * Array of validation failure messages
0044      *
0045      * @var array
0046      */
0047     protected $_messages = array();
0048 
0049     /**
0050      * Default Namespaces
0051      *
0052      * @var array
0053      */
0054     protected static $_defaultNamespaces = array();
0055 
0056     /**
0057      * Array of validation failure message codes
0058      *
0059      * @var array
0060      * @deprecated Since 1.5.0
0061      */
0062     protected $_errors = array();
0063 
0064     /**
0065      * Adds a validator to the end of the chain
0066      *
0067      * If $breakChainOnFailure is true, then if the validator fails, the next validator in the chain,
0068      * if one exists, will not be executed.
0069      *
0070      * @param  Zend_Validate_Interface $validator
0071      * @param  boolean                 $breakChainOnFailure
0072      * @return Zend_Validate Provides a fluent interface
0073      */
0074     public function addValidator(Zend_Validate_Interface $validator, $breakChainOnFailure = false)
0075     {
0076         $this->_validators[] = array(
0077             'instance' => $validator,
0078             'breakChainOnFailure' => (boolean) $breakChainOnFailure
0079             );
0080         return $this;
0081     }
0082 
0083     /**
0084      * Returns true if and only if $value passes all validations in the chain
0085      *
0086      * Validators are run in the order in which they were added to the chain (FIFO).
0087      *
0088      * @param  mixed $value
0089      * @return boolean
0090      */
0091     public function isValid($value)
0092     {
0093         $this->_messages = array();
0094         $this->_errors   = array();
0095         $result = true;
0096         foreach ($this->_validators as $element) {
0097             $validator = $element['instance'];
0098             if ($validator->isValid($value)) {
0099                 continue;
0100             }
0101             $result = false;
0102             $messages = $validator->getMessages();
0103             $this->_messages = array_merge($this->_messages, $messages);
0104             $this->_errors   = array_merge($this->_errors,   array_keys($messages));
0105             if ($element['breakChainOnFailure']) {
0106                 break;
0107             }
0108         }
0109         return $result;
0110     }
0111 
0112     /**
0113      * Defined by Zend_Validate_Interface
0114      *
0115      * Returns array of validation failure messages
0116      *
0117      * @return array
0118      */
0119     public function getMessages()
0120     {
0121         return $this->_messages;
0122     }
0123 
0124     /**
0125      * Defined by Zend_Validate_Interface
0126      *
0127      * Returns array of validation failure message codes
0128      *
0129      * @return array
0130      * @deprecated Since 1.5.0
0131      */
0132     public function getErrors()
0133     {
0134         return $this->_errors;
0135     }
0136 
0137     /**
0138      * Returns the set default namespaces
0139      *
0140      * @return array
0141      */
0142     public static function getDefaultNamespaces()
0143     {
0144         return self::$_defaultNamespaces;
0145     }
0146 
0147     /**
0148      * Sets new default namespaces
0149      *
0150      * @param array|string $namespace
0151      * @return null
0152      */
0153     public static function setDefaultNamespaces($namespace)
0154     {
0155         if (!is_array($namespace)) {
0156             $namespace = array((string) $namespace);
0157         }
0158 
0159         self::$_defaultNamespaces = $namespace;
0160     }
0161 
0162     /**
0163      * Adds a new default namespace
0164      *
0165      * @param array|string $namespace
0166      * @return null
0167      */
0168     public static function addDefaultNamespaces($namespace)
0169     {
0170         if (!is_array($namespace)) {
0171             $namespace = array((string) $namespace);
0172         }
0173 
0174         self::$_defaultNamespaces = array_unique(array_merge(self::$_defaultNamespaces, $namespace));
0175     }
0176 
0177     /**
0178      * Returns true when defaultNamespaces are set
0179      *
0180      * @return boolean
0181      */
0182     public static function hasDefaultNamespaces()
0183     {
0184         return (!empty(self::$_defaultNamespaces));
0185     }
0186 
0187     /**
0188      * @param  mixed    $value
0189      * @param  string   $classBaseName
0190      * @param  array    $args          OPTIONAL
0191      * @param  mixed    $namespaces    OPTIONAL
0192      * @return boolean
0193      * @throws Zend_Validate_Exception
0194      */
0195     public static function is($value, $classBaseName, array $args = array(), $namespaces = array())
0196     {
0197         $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('Zend_Validate'));
0198         $className  = ucfirst($classBaseName);
0199         try {
0200             if (!class_exists($className, false)) {
0201                 // require_once 'Zend/Loader.php';
0202                 foreach($namespaces as $namespace) {
0203                     $class = $namespace . '_' . $className;
0204                     $file  = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
0205                     if (Zend_Loader::isReadable($file)) {
0206                         Zend_Loader::loadClass($class);
0207                         $className = $class;
0208                         break;
0209                     }
0210                 }
0211             }
0212 
0213             $class = new ReflectionClass($className);
0214             if ($class->implementsInterface('Zend_Validate_Interface')) {
0215                 if ($class->hasMethod('__construct')) {
0216                     $keys    = array_keys($args);
0217                     $numeric = false;
0218                     foreach($keys as $key) {
0219                         if (is_numeric($key)) {
0220                             $numeric = true;
0221                             break;
0222                         }
0223                     }
0224 
0225                     if ($numeric) {
0226                         $object = $class->newInstanceArgs($args);
0227                     } else {
0228                         $object = $class->newInstance($args);
0229                     }
0230                 } else {
0231                     $object = $class->newInstance();
0232                 }
0233 
0234                 return $object->isValid($value);
0235             }
0236         } catch (Zend_Validate_Exception $ze) {
0237             // if there is an exception while validating throw it
0238             throw $ze;
0239         } catch (Exception $e) {
0240             // fallthrough and continue for missing validation classes
0241         }
0242 
0243         // require_once 'Zend/Validate/Exception.php';
0244         throw new Zend_Validate_Exception("Validate class not found from basename '$classBaseName'");
0245     }
0246 
0247     /**
0248      * Returns the maximum allowed message length
0249      *
0250      * @return integer
0251      */
0252     public static function getMessageLength()
0253     {
0254         // require_once 'Zend/Validate/Abstract.php';
0255         return Zend_Validate_Abstract::getMessageLength();
0256     }
0257 
0258     /**
0259      * Sets the maximum allowed message length
0260      *
0261      * @param integer $length
0262      */
0263     public static function setMessageLength($length = -1)
0264     {
0265         // require_once 'Zend/Validate/Abstract.php';
0266         Zend_Validate_Abstract::setMessageLength($length);
0267     }
0268 
0269     /**
0270      * Returns the default translation object
0271      *
0272      * @return Zend_Translate_Adapter|null
0273      */
0274     public static function getDefaultTranslator($translator = null)
0275     {
0276         // require_once 'Zend/Validate/Abstract.php';
0277         return Zend_Validate_Abstract::getDefaultTranslator();
0278     }
0279 
0280     /**
0281      * Sets a default translation object for all validation objects
0282      *
0283      * @param Zend_Translate|Zend_Translate_Adapter|null $translator
0284      */
0285     public static function setDefaultTranslator($translator = null)
0286     {
0287         // require_once 'Zend/Validate/Abstract.php';
0288         Zend_Validate_Abstract::setDefaultTranslator($translator);
0289     }
0290 }