Application specific filters

Applies to Stubbles 1.3.0 or greater. For Stubbles versions before 1.3.0 please see Filters before 1.3.0.

Create your own filter class

While Stubbles already provides a list of filters by default and those are very easy to use via the Request API, sometimes it is required to create your own filter for application specific needs. To create a filter one has to implement the net::stubbles::ipo::request::filter::stubFilter interface. This interface consists of a single method execute($value) which does the actual filtering.

The execute($value) is allowed to throw exceptions of the type net::stubbles::ipo::request::filter::stubFilterException. This exception expects an instance of net::stubbles::ipo::request::stubRequestValueError. See Request Errors for more details.

Make the filter available

In order to make the filter available via the Request API it has to be introduced to the filter creation mechanism. To do this, simply call the net::stubbles::ipo::ioc::stubIpoBindingModule::addFilterForType() method when you create the bindings:

$ipoBindingModule->addFilterForType('my::package::filter::ExampleFilter', 'example');

After the binding process is complete, the filter is available via the Request API:

$fooExample = $request->readParam('foo')->asType('example');

If used this way the filter class is also applicable for dependency injection. In case you have specific options for the filter to be set at runtime:

$fooExample = $request->readParam('foo')->withFilter($filterFactory->createForType('example')->asRequired()->setExampleConfig(313));

Here you need to have an instance of net::stubbles::ipo::request::filter::stubFilterFactory. It's createForType() method returns an instance of net::stubbles::ipo::request::filter::stubFilterBuilder which decorates your own filter class. You can still call all methods of your own filter class - the net::stubbles::ipo::request::filter::stubFilterBuilder has a magic __call() method which passes the method call to the decorated filter.