File indexing completed on 2024-12-22 05:36:41
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_Filter 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_Filter_Interface 0024 */ 0025 // require_once 'Zend/Filter/Interface.php'; 0026 0027 /** 0028 * @category Zend 0029 * @package Zend_Filter 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_Filter_Callback implements Zend_Filter_Interface 0034 { 0035 /** 0036 * Callback in a call_user_func format 0037 * 0038 * @var string|array 0039 */ 0040 protected $_callback = null; 0041 0042 /** 0043 * Default options to set for the filter 0044 * 0045 * @var mixed 0046 */ 0047 protected $_options = null; 0048 0049 /** 0050 * Constructor 0051 * 0052 * @param string|array $callback Callback in a call_user_func format 0053 * @param mixed $options (Optional) Default options for this filter 0054 */ 0055 public function __construct($options) 0056 { 0057 if ($options instanceof Zend_Config) { 0058 $options = $options->toArray(); 0059 } else if (!is_array($options) || !array_key_exists('callback', $options)) { 0060 $options = func_get_args(); 0061 $temp['callback'] = array_shift($options); 0062 if (!empty($options)) { 0063 $temp['options'] = array_shift($options); 0064 } 0065 0066 $options = $temp; 0067 } 0068 0069 if (!array_key_exists('callback', $options)) { 0070 // require_once 'Zend/Filter/Exception.php'; 0071 throw new Zend_Filter_Exception('Missing callback to use'); 0072 } 0073 0074 $this->setCallback($options['callback']); 0075 if (array_key_exists('options', $options)) { 0076 $this->setOptions($options['options']); 0077 } 0078 } 0079 0080 /** 0081 * Returns the set callback 0082 * 0083 * @return string|array Set callback 0084 */ 0085 public function getCallback() 0086 { 0087 return $this->_callback; 0088 } 0089 0090 /** 0091 * Sets a new callback for this filter 0092 * 0093 * @param unknown_type $callback 0094 * @return unknown 0095 */ 0096 public function setCallback($callback, $options = null) 0097 { 0098 if (!is_callable($callback)) { 0099 // require_once 'Zend/Filter/Exception.php'; 0100 throw new Zend_Filter_Exception('Callback can not be accessed'); 0101 } 0102 0103 $this->_callback = $callback; 0104 $this->setOptions($options); 0105 return $this; 0106 } 0107 0108 /** 0109 * Returns the set default options 0110 * 0111 * @return mixed 0112 */ 0113 public function getOptions() 0114 { 0115 return $this->_options; 0116 } 0117 0118 /** 0119 * Sets new default options to the callback filter 0120 * 0121 * @param mixed $options Default options to set 0122 * @return Zend_Filter_Callback 0123 */ 0124 public function setOptions($options) 0125 { 0126 $this->_options = $options; 0127 return $this; 0128 } 0129 0130 /** 0131 * Calls the filter per callback 0132 * 0133 * @param mixed $value Options for the set callback 0134 * @return mixed Result from the filter which was callbacked 0135 */ 0136 public function filter($value) 0137 { 0138 $options = array(); 0139 0140 if ($this->_options !== null) { 0141 if (!is_array($this->_options)) { 0142 $options = array($this->_options); 0143 } else { 0144 $options = $this->_options; 0145 } 0146 } 0147 0148 array_unshift($options, $value); 0149 0150 return call_user_func_array($this->_callback, $options); 0151 } 0152 }