web/Zend/Filter/Callback.php
changeset 0 4eba9c11703f
equal deleted inserted replaced
-1:000000000000 0:4eba9c11703f
       
     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_Filter
       
    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: Callback.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Filter_Interface
       
    24  */
       
    25 require_once 'Zend/Filter/Interface.php';
       
    26 
       
    27 /**
       
    28  * @category   Zend
       
    29  * @package    Zend_Filter
       
    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_Filter_Callback implements Zend_Filter_Interface
       
    34 {
       
    35     /**
       
    36      * Callback in a call_user_func format
       
    37      *
       
    38      * @var string|array
       
    39      */
       
    40     protected $_callback = null;
       
    41 
       
    42     /**
       
    43      * Default options to set for the filter
       
    44      *
       
    45      * @var mixed
       
    46      */
       
    47     protected $_options = null;
       
    48 
       
    49     /**
       
    50      * Constructor
       
    51      *
       
    52      * @param string|array $callback Callback in a call_user_func format
       
    53      * @param mixed        $options  (Optional) Default options for this filter
       
    54      */
       
    55     public function __construct($options)
       
    56     {
       
    57         if ($options instanceof Zend_Config) {
       
    58             $options = $options->toArray();
       
    59         } else if (!is_array($options) || !array_key_exists('callback', $options)) {
       
    60             $options          = func_get_args();
       
    61             $temp['callback'] = array_shift($options);
       
    62             if (!empty($options)) {
       
    63                 $temp['options'] = array_shift($options);
       
    64             }
       
    65 
       
    66             $options = $temp;
       
    67         }
       
    68 
       
    69         if (!array_key_exists('callback', $options)) {
       
    70             require_once 'Zend/Filter/Exception.php';
       
    71             throw new Zend_Filter_Exception('Missing callback to use');
       
    72         }
       
    73 
       
    74         $this->setCallback($options['callback']);
       
    75         if (array_key_exists('options', $options)) {
       
    76             $this->setOptions($options['options']);
       
    77         }
       
    78     }
       
    79 
       
    80     /**
       
    81      * Returns the set callback
       
    82      *
       
    83      * @return string|array Set callback
       
    84      */
       
    85     public function getCallback()
       
    86     {
       
    87         return $this->_callback;
       
    88     }
       
    89 
       
    90     /**
       
    91      * Sets a new callback for this filter
       
    92      *
       
    93      * @param unknown_type $callback
       
    94      * @return unknown
       
    95      */
       
    96     public function setCallback($callback, $options = null)
       
    97     {
       
    98         if (!is_callable($callback)) {
       
    99             require_once 'Zend/Filter/Exception.php';
       
   100             throw new Zend_Filter_Exception('Callback can not be accessed');
       
   101         }
       
   102 
       
   103         $this->_callback = $callback;
       
   104         $this->setOptions($options);
       
   105         return $this;
       
   106     }
       
   107 
       
   108     /**
       
   109      * Returns the set default options
       
   110      *
       
   111      * @return mixed
       
   112      */
       
   113     public function getOptions()
       
   114     {
       
   115         return $this->_options;
       
   116     }
       
   117 
       
   118     /**
       
   119      * Sets new default options to the callback filter
       
   120      *
       
   121      * @param mixed $options Default options to set
       
   122      * @return Zend_Filter_Callback
       
   123      */
       
   124     public function setOptions($options)
       
   125     {
       
   126         $this->_options = $options;
       
   127         return $this;
       
   128     }
       
   129 
       
   130     /**
       
   131      * Calls the filter per callback
       
   132      *
       
   133      * @param $value mixed Options for the set callback
       
   134      * @return mixed       Result from the filter which was callbacked
       
   135      */
       
   136     public function filter($value)
       
   137     {
       
   138         $options = array();
       
   139 
       
   140         if ($this->_options !== null) {
       
   141             if (!is_array($this->_options)) {
       
   142                 $options = array($this->_options);
       
   143             } else {
       
   144                 $options = $this->_options;
       
   145             }
       
   146         }
       
   147 
       
   148         array_unshift($options, $value);
       
   149 
       
   150         return call_user_func_array($this->_callback, $options);
       
   151     }
       
   152 }