delboy1978uk / collection-filter
Filter your arrays and collections with this extendable filter class
Installs: 1 988
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.0.0
Requires (Dev)
- codeception/codeception: dev-master
This package is auto-updated.
Last update: 2024-11-04 22:06:19 UTC
README
An easy and extendable way of filtering down full result sets. Good for paging, amongst other filters.
installation
composer require delboy1978uk/collection-filter
usage
The CollectionFilter class comes with a paginator to filter results. See below for creating your own filters
<?php use Del\Filter\CollectionFilter; $data = [1,2,3,4,5,6,7,8,9]; $filter = new CollectionFilter(); $filter->getPaginationFilter() ->setPage(2) ->setNumPerPage(3); $results = $filter->filterArrayResults($data); // returns array[4,5,6] $totalPages = $filter->getPaginationFilter()->getTotalPages(); // returns 3
You can also use any class implementing ArrayIterator:
<?php $results = $filter->filterResults($arrayIteratorClass);
create your own filters
Creating your own filters is easy. Just implement Del\Filter\Filter\FilterInterface
. There are two test filters in
tests/unit
. Here's how you build one:
<?php // firstly, use these use ArrayIterator; use Del\Filter\Filter\FilterInterface; // next, make your class implement FilterInterface class NumberSixFilter implements FilterInterface { /** * Then create this method * * @param ArrayIterator $collection * @return ArrayIterator */ public function filter(ArrayIterator $collection) : ArrayIterator { // We need to pass back results $results = new ArrayIterator(); // loop through the collection passed in while ($collection->valid()) { // Get the current row $current = $collection->current(); // This is my actual example filter. // If the current row is an integer that ISN'T 6, add it! // And if it isn't an integer add it regardless if (is_integer($current) && $current != 6) { $results->append($current); } elseif (!is_integer($current)) { $results->append($current); } // Move on to the next item $collection->next(); } // Finally, return the results return $results; } }
Then to add your new filter:
<?php use Del\Filter\CollectionFilter; use Your\CustomFilter; $filter = new CollectionFilter(); $customFilter = new CustomFilter(); $filter->getFilterCollection()->append($customFilter);
contributing
Feel free to add issues in Github, or pick up an issue and send a pull request! All contributions welcome!