Filter Chains

Often multiple filters should be applied to some value in a particular order. For example, a login form accepts a username that should be only lowercase, alphabetic characters. Zend\Filter\FilterChain provides a simple method by which filters may be chained together. The following code illustrates how to chain together two filters for the submitted username:

1
2
3
4
5
6
7
// Create a filter chain and add filters to the chain
$filterChain = new Zend\Filter\FilterChain();
$filterChain->attach(new Zend\Filter\Alpha())
            ->attach(new Zend\Filter\StringToLower());

// Filter the username
$username = $filterChain->filter($_POST['username']);

Filters are run in the order they were added to Zend\Filter\FilterChain. In the above example, the username is first removed of any non-alphabetic characters, and then any uppercase characters are converted to lowercase.

Any object that implements Zend\Filter\FilterInterface may be used in a filter chain.

Setting Filter Chain Order

For each filter added to the FilterChain you can set a priority to define the chain order. The default value is 1000. In the following example, any uppercase characters are converted to lowercase before any non-alphabetic characters are removed.

1
2
3
4
// Create a filter chain and add filters to the chain
$filterChain = new Zend\Filter\FilterChain();
$filterChain->attach(new Zend\Filter\Alpha())
            ->attach(new Zend\Filter\StringToLower(), 500);

Using the Plugin Manager

To every FilterChain object an instance of the FilterPluginManager is attached. Every filter that is used in a FilterChain must be know by this FilterPluginManager. To add a filter that is known by the FilterPluginManager you can use the attachByName() method. The first parameter is the name of the filter within the FilterPluginManager. The second parameter takes any options for creating the filter instance. The third parameter is the priority.

1
2
3
4
// Create a filter chain and add filters to the chain
$filterChain = new Zend\Filter\FilterChain();
$filterChain->attachByName('alpha')
            ->attachByName('stringtolower', array('encoding' => 'utf-8'), 500);

The following example shows how to add a custom filter to the FilterPluginManager and the FilterChain.

1
2
3
4
5
6
$filterChain = new Zend\Filter\FilterChain();
$filterChain->getPluginManager()->setInvokableClass(
    'myNewFilter', 'MyCustom\Filter\MyNewFilter'
);
$filterChain->attachByName('alpha')
            ->attachByName('myNewFilter');

You can also add your own FilterPluginManager implementation.

1
2
3
4
$filterChain = new Zend\Filter\FilterChain();
$filterChain->setPluginManager(new MyFilterPluginManager());
$filterChain->attach(new Zend\Filter\Alpha())
            ->attach(new MyCustom\Filter\MyNewFilter());

Table Of Contents

Previous topic

File Filter Classes

Next topic

Zend\Filter\Inflector

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Filter Chains on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.