ZF-2269: Zend_Filter_Callback
Description
This new filter provides the flexibility to not have to write a custom ZF filter when filtering is already available from another function.
class Zend_Filter_Callback implements Zend_Filter_Interface {
/**
* The callback function/method.
*
* @var callback
*/
protected $_callback;
/**
* Additional parameters to send to the callback function/method.
*
* @var array
*/
protected $_params = array();
/**
* Instantiates the callback filter.
*
* @param $callback The callback function/method.
* @param $params Additional parameters to be sent to the callback function/method.
*/
public function __construct($callback, array $params = array()) {
$this->setCallback($callback)->_params = $params;
}
/**
* Sets the callback function/method.
*
* @param callback $callback
*
* @return Zend_Filter_Callback
*/
public function setCallback($callback) {
if (!is_callable($callback)) {
throw new Zend_Filter_Exception('Invalid callback');
}
$this->_callback = $callback;
return $this;
}
/**
* Calls the callback function/method.
*
* @param mixed $value
*
* @return mixed
*/
public function filter($value) {
$params = array_merge(array($value), $this->_params);
return call_user_func_array($this->_callback, $params);
}
}
Comments
Posted by Wil Sinclair (wil) on 2008-04-18T17:11:46.000+0000
Please evaluate and categorize/assign as necessary.
Posted by Jordan Ryan Moore (jordanryanmoore) on 2008-07-30T16:25:05.000+0000
Attached a new version.
Posted by Wil Sinclair (wil) on 2009-01-19T08:08:25.000+0000
Ralph, can you find a course of action on this issue, please?
Posted by Thomas Weidner (thomas) on 2009-01-19T10:12:26.000+0000
There is already a small Zend_Filter_Callback implemented but it lacks 2 things which are solved in the above code:
Both would be a good addition to the existing class.
Posted by Matthew Weier O'Phinney (matthew) on 2009-04-15T12:29:42.000+0000
I'd suggest one of the following approaches:
or:
with preference going to the latter.
Posted by Thomas Weidner (thomas) on 2009-04-16T23:51:47.000+0000
The mentioned two improvements have been added to trunk. Additionally there exists now a chapter in the manual which was missing before.