Download the PHP package crazycodr/data-filter without Composer
On this page you can find all versions of the php package crazycodr/data-filter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download crazycodr/data-filter
More information about crazycodr/data-filter
Files in crazycodr/data-filter
Package data-filter
Short Description Used to filter out data from an enumerable source using more flexible patterns than the filter iterator spl class
License MIT
Informations about the package data-filter
CrazyCodr/Data/Filter
This package contains facilities to easily filter live iterated data from any enumerable source.
This class features a filtering iterator accompagnied by different filters/filter groups that you can use to filter incoming data from any iteratable data-source. With this class package, you can create simple or complex groups of filters and filters an even be modified on the fly because it is an iterator over an iterator.
Table of contents
- Installation
- Creating a basic filtering iterator
- Supporting many filters at once
- Building complex filtering groups
- Using components outside of the iterator context
- Creating your own testable classes
Installation
To install it, just include this requirement into your composer.json
And then run composer install/update as necessary.
Creating a basic filtering iterator
Creating a filtering iterator requires at least three items:
- A FilterGroup used to contain the different filters for your iterator
- A FilterIterator used to iterate your data and provide filtering features
- A Filter used to detect if the current data should be kept or discarded from the iteration
(Note: This code assumes that you have an array based datasource with columns: Name, Type, Sex and Age)
Supporting many filters at once
The library supports multiple filters at once and will also respect the short-circuiting pattern. (A condition is only fully evaluated if necessary)
In this case, we have to switch the FilterGroup to "ANY" mode or else nothing will go through.
Building complex filtering groups
Last example showed us a closure filter that add many conditions. What if you want to explode that or combine many different closure filters in groups and sub groups such as:
- MaleOf30OrLess
- Male Only
- 30 or less
- FemaleOf30OrMore
- Female Only
- 30 or more
The important aspect to remember is that, by default, FilterGroups are "ALL" groups and must be changed to "ANY" groups when necessary.
Using components outside of the iterator context
You don't need to use a filtering iterator... The ClosureFilter and FilterGroup can be used outside of a loop. Build conditions normally using concrete/non-concrete classes and call "shouldKeep" with some data.
Creating your own testable classes
The point of this library is not to have to create the iterators and they sub-components each time and be able to test the lot easily. To this end, simply create concrete extensions of your iterators and sub-components and then test them.
It might look extreme but this way you are creating a concrete functional class that can be reused and tested. Note that DataProviders are a great way to test your components but it will look strange to use a DataProvider when testing the iterators.