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.
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
23 * @see Zend_Filter_Interface
25 require_once 'Zend/Filter/Interface.php';
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
33 class Zend_Filter_Callback
implements Zend_Filter_Interface
36 * Callback in a call_user_func format
40 protected $_callback = null;
43 * Default options to set for the filter
47 protected $_options = null;
52 * @param string|array $callback Callback in a call_user_func format
53 * @param mixed $options (Optional) Default options for this filter
55 public function __construct($options)
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);
69 if (!array_key_exists('callback', $options)) {
70 require_once 'Zend/Filter/Exception.php';
71 throw new Zend_Filter_Exception('Missing callback to use');
74 $this->setCallback($options['callback']);
75 if (array_key_exists('options', $options)) {
76 $this->setOptions($options['options']);
81 * Returns the set callback
83 * @return string|array Set callback
85 public function getCallback()
87 return $this->_callback
;
91 * Sets a new callback for this filter
93 * @param unknown_type $callback
96 public function setCallback($callback, $options = null)
98 if (!is_callable($callback)) {
99 require_once 'Zend/Filter/Exception.php';
100 throw new Zend_Filter_Exception('Callback can not be accessed');
103 $this->_callback
= $callback;
104 $this->setOptions($options);
109 * Returns the set default options
113 public function getOptions()
115 return $this->_options
;
119 * Sets new default options to the callback filter
121 * @param mixed $options Default options to set
122 * @return Zend_Filter_Callback
124 public function setOptions($options)
126 $this->_options
= $options;
131 * Calls the filter per callback
133 * @param $value mixed Options for the set callback
134 * @return mixed Result from the filter which was callbacked
136 public function filter($value)
140 if ($this->_options
!== null) {
141 if (!is_array($this->_options
)) {
142 $options = array($this->_options
);
144 $options = $this->_options
;
148 array_unshift($options, $value);
150 return call_user_func_array($this->_callback
, $options);