maxgoryunov/saving-iterator

There is no license information available for the latest version (0.3) of this package.

True Caching Iterator for PHP

0.3 2021-09-29 19:22 UTC

README

EO principles respected here DevOps By Rultor.com

PHP-Composer-Build Build Status Build status wercker status MaxGoryunov

PDD status codecov Mutation testing badge Latest Stable Version License: MIT

Maintainability codebeat badge Codacy Badge Maintainability Rating Scrutinizer Code Quality CodeFactor

Hits-of-Code Lines-of-Code PHP Version

Saving Iterator is a true caching iterator for PHP. It aims to solve the same problems as PHP's Caching Iterator but with a better encapsulation of data in mind. It has properties of both Iterator and array.

How to use

Require it with Composer:

composer require maxgoryunov/saving-iterator

Then include this in your index.php or any other main file:

require __DIR__ . "./vendor/autoload.php";

If you have any questions, ask them at Discussions.

Decorating Iterators

In order to use SavingIterator you need to provide a source and a target. Any object with Iterator interface is a suitable source. Target needs to be an AddingIterator(usually ArrayAddingIterator is enough):

$squares = new SavingIterator(
    new SquaringIterator(
        [1, 2, 3, 4, 5, 6]
    ),
    new ArrayAddingIterator()
);

If the origin object is not an Iterator then wrap it in TransparentIterator:

$wrapped = new SavingIterator(
    new TransparentIterator($origin),
    new ArrayAddingIterator()
);

If you do not want to store nulls in your AddingIterator then use ValidAddingIterator:

$valid = new ValidAddingIterator(
    new ArrayAddingIterator()
);

Decorating Generators

You can also use it with Generators. If the iterator is called twice, rewind exception will not be thrown.

Attention: it is not (currently) possible to pass callable as a parameter. You have to manually invoke Generator function:

function numerals(): Generator {
    for ($i = 0; $i < 10; $i++) {
        yield $i;
    }
}

$numerals = new SavingIterator(
    numerals(),
    new ArrayAddingIterator()
);

How to contribute

Fork this repository, then create a folder for it and install Composer if you do not have it.

Clone this repository:

git clone https://github.com/MaxGoryunov/saving-iterator

Then run:

composer install

This command will install all dependencies required for development. Make changes and open a pull request. Your PR will be reviewed and accepted if it does not fail our build.