PHP code example of millancore / pipeline-iterator

1. Go to this page and download the library: Download millancore/pipeline-iterator 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/ */

    

millancore / pipeline-iterator example snippets


class EvenFilter extends FilterIterator
{
    public function accept(): bool
    {
        return $this->current() % 2 === 0;
    }
}

use Millancore\PipelineIterator\PipelineFilterIterator;

$arrayData = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

/** 
* Use with Iterator
* $iterator = PipelineFilterIterator::create(new ArrayIterator($arrayData))
*/

// Use with Array directly
$iterator = PipelineFilterIterator::createFromArray($arrayData)
    ->filter(EvenFilter::class)
    ->filter(CallbackFilterIterator::class, fn($value) => $value > 3)
    ->filter(RangeFilter::class, 5, 10);

foreach ($iterator as $value) {
    echo $value; // Output: 6, 8, 10
}

class RangeFilter extends FilterIterator
{
    public function __construct(
        Iterator             $iterator,
        private readonly int $start,
        private readonly int $end
    ) {
        parent::__construct($iterator);
    }

    public function accept(): bool
    {
        return $this->current() >= $this->start && $this->current() <= $this->end;
    }
}