PHP code example of delboy1978uk / collection-filter

1. Go to this page and download the library: Download delboy1978uk/collection-filter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

delboy1978uk / collection-filter example snippets




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


$results = $filter->filterResults($arrayIteratorClass);



// 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;
    }
    
}




use Del\Filter\CollectionFilter;
use Your\CustomFilter;

$filter = new CollectionFilter();
$customFilter = new CustomFilter();

$filter->getFilterCollection()->append($customFilter);