[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Filter / Callback.php
blobd12e023d6f36e0faa97160c8d499a1c71266a849
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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$
22 /**
23 * @see Zend_Filter_Interface
25 require_once 'Zend/Filter/Interface.php';
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
33 class Zend_Filter_Callback implements Zend_Filter_Interface
35 /**
36 * Callback in a call_user_func format
38 * @var string|array
40 protected $_callback = null;
42 /**
43 * Default options to set for the filter
45 * @var mixed
47 protected $_options = null;
49 /**
50 * Constructor
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);
66 $options = $temp;
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']);
80 /**
81 * Returns the set callback
83 * @return string|array Set callback
85 public function getCallback()
87 return $this->_callback;
90 /**
91 * Sets a new callback for this filter
93 * @param unknown_type $callback
94 * @return unknown
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);
105 return $this;
109 * Returns the set default options
111 * @return mixed
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;
127 return $this;
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)
138 $options = array();
140 if ($this->_options !== null) {
141 if (!is_array($this->_options)) {
142 $options = array($this->_options);
143 } else {
144 $options = $this->_options;
148 array_unshift($options, $value);
150 return call_user_func_array($this->_callback, $options);