/iterators

The missing PHP iterators.

Primary LanguagePHPMIT LicenseMIT

Latest Stable Version GitHub stars Total Downloads GitHub Workflow Status Scrutinizer code quality Type Coverage Code Coverage License Donate!

PHP Iterators

Description

The missing PHP iterators.

Features

7 Iterators:

  • CachingIteratorAggregate
  • ClosureIterator: ClosureIterator(callable $callable, array $arguments = [])
  • ClosureIteratorAggregate: ClosureIteratorAggregate(callable $callable, array $arguments = [])
  • IterableIterator: IterableIterator(iterable $iterable)
  • IterableIteratorAggregate: IterableIteratorAggregate(iterable $iterable)
  • PackIterableAggregate
  • UnpackIterableAggregate

Installation

composer require loophp/iterators

Usage

CachingIteratorAggregate

Let you cache any iterator. You then get \Generators rewindable for free.

This implementation does not use internal state to keep track of the current position of the iterator. The underlying mechanism is based on SPL \CachingIterator.

The pros of using that iterator is performance. It's blazing fast, it cannot compare to any other stateful custom implementations.

This iterator will cache keys and values, of any type.

<?php

// Generator
$generator = static function (): \Generator {
    yield true => 'foo';
    yield false => 'bar';
    yield ['foo', 'bar'] => 'foobar';
};

$iterator = new CachingIteratorAggregate($generator());

foreach ($iterator as $key => $value); // This will work.
foreach ($iterator as $key => $value); // This will also work.

SimpleCachingIteratorAggregate

This iterator has the same features as CachingIteratorAggregate.

The only difference is that this iterator only cache the values, not the keys.

PackIterableAggregate

<?php

// Generator
$generator = static function (): \Generator {
    yield true => 'foo';
    yield false => 'bar';
    yield ['foo', 'bar'] => 'foobar';
};

$iterator = new PackIterableAggregate($generator());

foreach ($iterator as $value);
/*
$value will yield the following values:

- [true, 'foo']
- [false, 'bar']
- [['foo', 'bar'], 'foobar']
*/

UnpackIterableAggregate

<?php

// Generator
$generator = static function (): \Generator {
    yield [true, 'foo'];
    yield [false, 'bar'];
    yield [['foo', 'bar'], 'foobar'];
};

$iterator = new UnpackIterableAggregate($generator());

foreach ($iterator as $key => $value);
/*
$key and $value will yield the following values:

- true => 'foo'
- false => 'bar'
- ['foo', 'bar'] => 'foobar'
*/

ClosureIterator

<?php

$callable = static fn (int $from, int $to) => yield from range($from, $to);

$iterator = new ClosureIterator($callable(10, 20));

IterableIterator

<?php

$iterator = new IterableIterator(range(1, 10));

Documentation

Code quality, tests, benchmarks

Every time changes are introduced into the library, Github runs the tests.

The library has tests written with PHPSpec. Feel free to check them out in the spec directory. Run composer phpspec to trigger the tests.

Before each commit, some inspections are executed with GrumPHP; run composer grumphp to check manually.

The quality of the tests is tested with Infection a PHP Mutation testing framework - run composer infection to try it.

Static analyzers are also controlling the code. PHPStan and PSalm are enabled to their maximum level.

Contributing

Feel free to contribute by sending Github pull requests. I'm quite responsive :-)

If you can't contribute to the code, you can also sponsor me on Github or Paypal.

Changelog

See CHANGELOG.md for a changelog based on git commits.

For more detailed changelogs, please check the release changelogs.