File indexing completed on 2024-12-22 05:37:12

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_Abstract
0024  */
0025 // require_once 'Zend/Validate/Abstract.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_Callback extends Zend_Validate_Abstract
0034 {
0035     /**
0036      * Invalid callback
0037      */
0038     const INVALID_CALLBACK = 'callbackInvalid';
0039 
0040     /**
0041      * Invalid value
0042      */
0043     const INVALID_VALUE = 'callbackValue';
0044 
0045     /**
0046      * Validation failure message template definitions
0047      *
0048      * @var array
0049      */
0050     protected $_messageTemplates = array(
0051         self::INVALID_VALUE    => "'%value%' is not valid",
0052         self::INVALID_CALLBACK => "An exception has been raised within the callback",
0053     );
0054 
0055     /**
0056      * Callback in a call_user_func format
0057      *
0058      * @var string|array
0059      */
0060     protected $_callback = null;
0061 
0062     /**
0063      * Default options to set for the filter
0064      *
0065      * @var mixed
0066      */
0067     protected $_options = array();
0068 
0069     /**
0070      * Sets validator options
0071      *
0072      * @param  mixed $callback
0073      * @throws Zend_Validate_Exception
0074      */
0075     public function __construct($callback = null)
0076     {
0077         if (is_callable($callback)) {
0078             $this->setCallback($callback);
0079         } elseif (is_array($callback)) {
0080             if (isset($callback['callback'])) {
0081                 $this->setCallback($callback['callback']);
0082             }
0083             if (isset($callback['options'])) {
0084                 $this->setOptions($callback['options']);
0085             }
0086         }
0087 
0088         if (null === ($initializedCallack = $this->getCallback())) {
0089             // require_once 'Zend/Validate/Exception.php';
0090             throw new Zend_Validate_Exception('No callback registered');
0091         }
0092     }
0093 
0094     /**
0095      * Returns the set callback
0096      *
0097      * @return mixed
0098      */
0099     public function getCallback()
0100     {
0101         return $this->_callback;
0102     }
0103 
0104     /**
0105      * Sets the callback
0106      *
0107      * @param  string|array $callback
0108      * @throws Zend_Validate_Exception
0109      * @return Zend_Validate_Callback Provides a fluent interface
0110      */
0111     public function setCallback($callback)
0112     {
0113         if (!is_callable($callback)) {
0114             // require_once 'Zend/Validate/Exception.php';
0115             throw new Zend_Validate_Exception('Invalid callback given');
0116         }
0117         $this->_callback = $callback;
0118         return $this;
0119     }
0120 
0121     /**
0122      * Returns the set options for the callback
0123      *
0124      * @return mixed
0125      */
0126     public function getOptions()
0127     {
0128         return $this->_options;
0129     }
0130 
0131     /**
0132      * Sets options for the callback
0133      *
0134      * @param  mixed $options
0135      * @return Zend_Validate_Callback Provides a fluent interface
0136      */
0137     public function setOptions($options)
0138     {
0139         $this->_options = (array) $options;
0140         return $this;
0141     }
0142 
0143     /**
0144      * Defined by Zend_Validate_Interface
0145      *
0146      * Returns true if and only if the set callback returns
0147      * for the provided $value
0148      *
0149      * @param  mixed $value
0150      * @return boolean
0151      */
0152     public function isValid($value)
0153     {
0154         $this->_setValue($value);
0155 
0156         $options  = $this->getOptions();
0157         $callback = $this->getCallback();
0158         $args     = func_get_args();
0159         $options  = array_merge($args, $options);
0160 
0161         try {
0162             if (!call_user_func_array($callback, $options)) {
0163                 $this->_error(self::INVALID_VALUE);
0164                 return false;
0165             }
0166         } catch (Exception $e) {
0167             $this->_error(self::INVALID_CALLBACK);
0168             return false;
0169         }
0170 
0171         return true;
0172     }
0173 }