3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Filter
;
14 class Callback
extends AbstractFilter
19 protected $options = array(
21 'callback_params' => array()
25 * @param callable|array|Traversable $callbackOrOptions
26 * @param array $callbackParams
28 public function __construct($callbackOrOptions, $callbackParams = array())
30 if (is_callable($callbackOrOptions)) {
31 $this->setCallback($callbackOrOptions);
32 $this->setCallbackParams($callbackParams);
34 $this->setOptions($callbackOrOptions);
39 * Sets a new callback for this filter
41 * @param callable $callback
42 * @throws Exception\InvalidArgumentException
45 public function setCallback($callback)
47 if (!is_callable($callback)) {
48 throw new Exception\
InvalidArgumentException(
49 'Invalid parameter for callback: must be callable'
53 $this->options
['callback'] = $callback;
58 * Returns the set callback
62 public function getCallback()
64 return $this->options
['callback'];
68 * Sets parameters for the callback
70 * @param array $params
73 public function setCallbackParams($params)
75 $this->options
['callback_params'] = (array) $params;
80 * Get parameters for the callback
84 public function getCallbackParams()
86 return $this->options
['callback_params'];
90 * Calls the filter per callback
92 * @param mixed $value Options for the set callable
93 * @return mixed Result from the filter which was called
95 public function filter($value)
97 $params = (array) $this->options
['callback_params'];
98 array_unshift($params, $value);
100 return call_user_func_array($this->options
['callback'], $params);