millancore/pipeline-iterator

Pipeline Iterator Filter

1.0.0 2024-11-22 15:11 UTC

This package is auto-updated.

Last update: 2024-11-22 17:26:20 UTC


README

PipelineFilterIterator library provides a flexible and powerful way to filter and manipulate PHP Iterators using a pipeline-based approach. It allows developers to apply multiple filters sequentially in a clean and readable manner.

You can use SPL filters like CallbackFilterIterator, RegexIterator or create your own filter by extending the FilterIterator in simple way.

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

Installation

composer require millancore/pipeline-iterator

Usage

use Millancore\PipelineIterator\PipelineFilterIterator;

$iterator = new ArrayIterator([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

$iterator = PipelineFilterIterator::create($iterator)
    ->filter(EventFilter::class)
    ->filter(CallbackFilterIterator::class, [fn($value) => $value > 3])
    ->filter(RangeFilter::class, [5, 10])
    ->getIterator();

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

You can also use the fromArray method to create a new iterator from an array:

$iterator = PipelineFilterIterator::fromArray([1, 2, 3])
    ->filter(EventFilter::class)
    ...