Issues

ZF-2269: Zend_Filter_Callback

Issue Type: New Feature Created: 2007-12-05T12:46:49.000+0000 Last Updated: 2009-04-16T23:53:07.000+0000 Status: Resolved Fix version(s): - 1.8.0 (30/Apr/09)

Reporter: Jordan Ryan Moore (jordanryanmoore) Assignee: Thomas Weidner (thomas) Tags: - Zend_Filter

Related issues: Attachments: - Callback.php

Description

This new filter provides the flexibility to not have to write a custom ZF filter when filtering is already available from another function.

<pre class="highlight">
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:

  • It does not check if the callback can be called
  • It does not provide default parameters

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:

<pre class="highlight">
$params = $this->_params;
call_user_func_array($this->_callback, array($value, $params));

or:

<pre class="highlight">
$params = $this->_params;
array_unshift($params, $value);
call_user_func_array($this->_callback, $params);

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.

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts