-
Notifications
You must be signed in to change notification settings - Fork 0
Docs Request Filters
Applies to Stubbles 1.3.0 or greater. For Stubbles versions before 1.3.0 please see Filters before 1.3.0.
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.
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.
In case you have a module which provides filters you can add them to be automatically available and detected by the framework. To enable this you need to have a file ipo/filter.ini in your star file. The content of the file should look like this:
[filter] example=my::package::ExampleFilter
With this and the star module within the lib directory of an application the application can use the filter with
$example = $request->readParam('foo')->withFilter($filterFactory->createForType('example');
without adding it manually as described above.