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
.
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.
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()
);
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()
);
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.