|
1 | | -# saving-iterator |
2 | | -True Caching Iterator for PHP. |
| 1 | +[](https://www.elegantobjects.org) |
| 2 | + |
| 3 | + |
| 4 | +[](https://packagist.org/packages/maxgoryunov/saving-iterator) |
| 5 | +[](https://codeclimate.com/github/MaxGoryunov/saving-iterator/maintainability) |
| 6 | +[](https://scrutinizer-ci.com/g/MaxGoryunov/saving-iterator/?branch=main) |
| 7 | +[](https://github.com/MaxGoryunov/saving-iterator/blob/main/LICENSE) |
| 8 | + |
| 9 | +[](https://hitsofcode.com/github/MaxGoryunov/saving-iterator/view) |
| 10 | + |
| 11 | + |
| 12 | +**Saving Iterator** is a true caching iterator for PHP. It aims to solve the same problems as PHP's [Caching Iterator](https://www.php.net/manual/ru/class.cachingiterator.php) but with a [better encapsulation of data](https://www.yegor256.com/2016/11/21/naked-data.html) in mind. It has properties of both `Iterator` and `array`. |
| 13 | + |
| 14 | +## How to use |
| 15 | + |
| 16 | +Require it with Composer: |
| 17 | + |
| 18 | +```bash |
| 19 | +composer require maxgoryunov/saving-iterator |
| 20 | +``` |
| 21 | + |
| 22 | +Then include this in your `index.php` or any other main file: |
| 23 | + |
| 24 | +```PHP |
| 25 | +require __DIR__ . "./vendor/autoload.php"; |
| 26 | +``` |
| 27 | + |
| 28 | +If you have any questions, ask them at [Discussions](https://github.com/MaxGoryunov/saving-iterator/discussions). |
| 29 | + |
| 30 | +## Decorating Iterators |
| 31 | + |
| 32 | +Any object with `Iterator` interface is suitable: |
| 33 | + |
| 34 | +```PHP |
| 35 | +$squares = new SavingIterator( |
| 36 | + new SquaringIterator( |
| 37 | + [1, 2, 3, 4, 5, 6] |
| 38 | + ) |
| 39 | +); |
| 40 | +``` |
| 41 | + |
| 42 | +If the origin object is not an `Iterator` then wrap it in `TransparentIterator`: |
| 43 | + |
| 44 | +```PHP |
| 45 | +$wrapped = new SavingIterator( |
| 46 | + new TransparentIterator($origin) |
| 47 | +); |
| 48 | +``` |
| 49 | + |
| 50 | +## Decorating Generators |
| 51 | + |
| 52 | +You can also use it with `Generators`. If the iterator is called twice, rewind exception will **not** be thrown. |
| 53 | + |
| 54 | +**Attention**: it is not (currently) possible to pass callable as a parameter. You have to manually invoke `Generator` function: |
| 55 | + |
| 56 | +```PHP |
| 57 | +function numerals(): Generator { |
| 58 | + for ($i = 0; $i < 10; $i++) { |
| 59 | + yield $i; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +$numerals = new SavingIterator(numerals()); |
| 64 | +``` |
| 65 | + |
| 66 | +## How to contribute |
| 67 | + |
| 68 | +[Fork this repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo), then create a folder for it and install [Composer](https://getcomposer.org/download/) if you do not have it. |
| 69 | + |
| 70 | +Clone this repository: |
| 71 | + |
| 72 | +```git |
| 73 | +git clone https://github.com/MaxGoryunov/saving-iterator |
| 74 | +``` |
| 75 | + |
| 76 | +Then run: |
| 77 | + |
| 78 | +```bash |
| 79 | +composer install |
| 80 | +``` |
| 81 | + |
| 82 | +This command will install all dependencies required for development. Make changes and [open a pull request](https://github.com/MaxGoryunov/saving-iterator/pulls). Your PR will be reviewed and accepted if it does not fail our build. |
0 commit comments