README.md
1<p align="center">
2 <img src="https://static.hoa-project.net/Image/Hoa.svg" alt="Hoa" width="250px" />
3</p>
4
5---
6
7<p align="center">
8 <a href="https://travis-ci.org/hoaproject/iterator"><img src="https://img.shields.io/travis/hoaproject/iterator/master.svg" alt="Build status" /></a>
9 <a href="https://coveralls.io/github/hoaproject/iterator?branch=master"><img src="https://img.shields.io/coveralls/hoaproject/iterator/master.svg" alt="Code coverage" /></a>
10 <a href="https://packagist.org/packages/hoa/iterator"><img src="https://img.shields.io/packagist/dt/hoa/iterator.svg" alt="Packagist" /></a>
11 <a href="https://hoa-project.net/LICENSE"><img src="https://img.shields.io/packagist/l/hoa/iterator.svg" alt="License" /></a>
12</p>
13<p align="center">
14 Hoa is a <strong>modular</strong>, <strong>extensible</strong> and
15 <strong>structured</strong> set of PHP libraries.<br />
16 Moreover, Hoa aims at being a bridge between industrial and research worlds.
17</p>
18
19# Hoa\Iterator
20
21[![Help on IRC](https://img.shields.io/badge/help-%23hoaproject-ff0066.svg)](https://webchat.freenode.net/?channels=#hoaproject)
22[![Help on Gitter](https://img.shields.io/badge/help-gitter-ff0066.svg)](https://gitter.im/hoaproject/central)
23[![Documentation](https://img.shields.io/badge/documentation-hack_book-ff0066.svg)](https://central.hoa-project.net/Documentation/Library/Iterator)
24[![Board](https://img.shields.io/badge/organisation-board-ff0066.svg)](https://waffle.io/hoaproject/iterator)
25
26This library provides a set of useful iterator (compatible with PHP iterators).
27Existing PHP iterators have been updated to get new features and prior PHP
28versions compatibility.
29
30[Learn more](https://central.hoa-project.net/Documentation/Library/Iterator).
31
32## Installation
33
34With [Composer](https://getcomposer.org/), to include this library into
35your dependencies, you need to
36require [`hoa/iterator`](https://packagist.org/packages/hoa/iterator):
37
38```sh
39$ composer require hoa/iterator '~2.0'
40```
41
42For more installation procedures, please read [the Source
43page](https://hoa-project.net/Source.html).
44
45## Testing
46
47Before running the test suites, the development dependencies must be installed:
48
49```sh
50$ composer install
51```
52
53Then, to run all the test suites:
54
55```sh
56$ vendor/bin/hoa test:run
57```
58
59For more information, please read the [contributor
60guide](https://hoa-project.net/Literature/Contributor/Guide.html).
61
62## Quick usage
63
64We propose a quick overview of all iterators.
65
66### The One
67
68`Hoa\Iterator\Iterator` defines the basis of an iterator. It extends
69[`Iterator`](http://php.net/class.iterator).
70
71### External iterator
72
73`Hoa\Iterator\Aggregate` allows a class to use an external iterator through the
74`getIterator` method. It extends
75[`IteratorAggregate`](http://php.net/iteratoraggregate)
76
77### Traversable to iterator
78
79`Hoa\Iterator\IteratorIterator` transforms anything that is
80[traversable](http://php.net/traversable) into an iterator. It extends
81[`IteratorIterator`](http://php.net/iteratoriterator).
82
83### Iterator of iterators
84
85`Hoa\Iterator\Outer` represents an iterator that iterates over iterators. It
86extends [`OuterIterator`](http://php.net/outeriterator).
87
88### Mock
89
90`Hoa\Iterator\Mock` represents an empty iterator. It extends
91[`EmptyIterator`](http://php.net/emptyiterator).
92
93### Seekable
94
95`Hoa\Iterator\Seekable` represents an iterator that can be seeked. It extends
96[`SeekableIterator`](http://php.net/seekableiterator).
97
98### Map
99
100`Hoa\Iterator\Map` allows to iterate an array. It extends
101[`ArrayIterator`](http://php.net/arrayiterator).
102
103```php
104$foobar = new Hoa\Iterator\Map(['f', 'o', 'o', 'b', 'a', 'r']);
105
106foreach ($foobar as $value) {
107 echo $value;
108}
109
110/**
111 * Will output:
112 * foobar
113 */
114```
115
116### Filters
117
118`Hoa\Iterator\Filter` and `Hoa\Iterator\CallbackFilter` allows to filter the
119content of an iterator. It extends
120[`FilterIterator`](http://php.net/filteriterator) and
121[`CallbackFilterIterator`](http://php.net/callbackfilteriterator).
122
123```php
124$filter = new Hoa\Iterator\CallbackFilter(
125 $foobar,
126 function ($value, $key, $iterator) {
127 return false === in_array($value, ['a', 'e', 'i', 'o', 'u']);
128 }
129);
130
131foreach ($filter as $value) {
132 echo $value;
133}
134
135/**
136 * Will output:
137 * fbr
138 */
139```
140
141Also, `Hoa\Iterator\RegularExpression` allows to filter based on a regular
142expression.
143
144### Limit
145
146`Hoa\Iterator\Limit` allows to iterate *n* elements of an iterator starting from
147a specific offset. It extends [`LimitIterator`](http://php.net/limititerator).
148
149```php
150$limit = new Hoa\Iterator\Limit($foobar, 2, 3);
151
152foreach ($limit as $value) {
153 echo $value;
154}
155
156/**
157 * Will output:
158 * oba
159 */
160```
161
162### Infinity
163
164`Hoa\Iterator\Infinite` allows to iterate over and over again the same iterator.
165It extends [`InfiniteIterator`](http://php.net/infiniteiterator).
166
167```php
168$infinite = new Hoa\Iterator\Infinite($foobar);
169$limit = new Hoa\Iterator\Limit($infinite, 0, 21);
170
171foreach ($limit as $value) {
172 echo $value;
173}
174
175/**
176 * Will output:
177 * foobarfoobarfoobarfoo
178 */
179```
180
181Also, `Hoa\Iterator\NoRewind` is an iterator that does not rewind. It extends
182[`NoRewindIterator`](http://php.net/norewinditerator).
183
184### Repeater
185
186`Hoa\Iterator\Repeater` allows to repeat an iterator *n* times.
187
188```php
189$repeater = new Hoa\Iterator\Repeater(
190 $foobar,
191 3,
192 function ($i) {
193 echo "\n";
194 }
195);
196
197foreach ($repeater as $value) {
198 echo $value;
199}
200
201/**
202 * Will output:
203 * foobar
204 * foobar
205 * foobar
206 */
207```
208
209### Counter
210
211`Hoa\Iterator\Counter` is equivalent to a `for($i = $from, $i < $to, $i +=
212$step)` loop.
213
214```php
215$counter = new Hoa\Iterator\Counter(0, 12, 3);
216
217foreach ($counter as $value) {
218 echo $value, ' ';
219}
220
221/**
222 * Will output:
223 * 0 3 6 9
224 */
225```
226
227### Union of iterators
228
229`Hoa\Iterator\Append` allows to iterate over iterators one after another. It
230extends [`AppendIterator`](http://php.net/appenditerator).
231
232```php
233$counter1 = new Hoa\Iterator\Counter(0, 12, 3);
234$counter2 = new Hoa\Iterator\Counter(13, 23, 2);
235$append = new Hoa\Iterator\Append();
236$append->append($counter1);
237$append->append($counter2);
238
239foreach ($append as $value) {
240 echo $value, ' ';
241}
242
243/**
244 * Will output:
245 * 0 3 6 9 13 15 17 19 21
246 */
247```
248
249### Multiple
250
251`Hoa\Iterator\Multiple` allows to iterate over several iterator at the same
252times. It extends [`MultipleIterator`](http://php.net/multipleiterator).
253
254```php
255$foobar = new Hoa\Iterator\Map(['f', 'o', 'o', 'b', 'a', 'r']);
256$baz = new Hoa\Iterator\Map(['b', 'a', 'z']);
257$multiple = new Hoa\Iterator\Multiple(
258 Hoa\Iterator\Multiple::MIT_NEED_ANY
259 | Hoa\Iterator\Multiple::MIT_KEYS_ASSOC
260);
261$multiple->attachIterator($foobar, 'one', '!');
262$multiple->attachIterator($baz, 'two', '?');
263
264foreach ($multiple as $iterators) {
265 echo $iterators['one'], ' | ', $iterators['two'], "\n";
266}
267
268/**
269 * Will output:
270 * f | b
271 * o | a
272 * o | z
273 * b | ?
274 * a | ?
275 * r | ?
276 */
277```
278
279### Demultiplexer
280
281`Hoa\Iterator\Demultiplexer` demuxes result from another iterator. This iterator
282is somehow the opposite of the `Hoa\Iterator\Multiple` iterator.
283
284```php
285$counter = new Hoa\Iterator\Counter(0, 10, 1);
286$multiple = new Hoa\Iterator\Multiple();
287$multiple->attachIterator($counter);
288$multiple->attachIterator(clone $counter);
289$demultiplexer = new Hoa\Iterator\Demultiplexer(
290 $multiple,
291 function ($current) {
292 return $current[0] * $current[1];
293 }
294);
295
296foreach ($demultiplexer as $value) {
297 echo $value, ' ';
298}
299
300/**
301 * Will output:
302 * 0 1 4 9 16 25 36 49 64 81
303 */
304```
305
306### File system
307
308`Hoa\Iterator\Directory` and `Hoa\Iterator\FileSystem` allow to iterate the file
309system where files are represented by instances of `Hoa\Iterator\SplFileInfo`.
310They respectively extend
311[`DirectoryIterator`](http://php.net/directoryiterator),
312[`FilesystemIterator`](http://php.net/filesystemiterator) and
313[`SplFileInfo`](http://php.net/splfileinfo).
314
315```php
316$directory = new Hoa\Iterator\Directory(resolve('hoa://Library/Iterator'));
317
318foreach ($directory as $value) {
319 echo $value->getFilename(), "\n";
320}
321
322/**
323 * Will output:
324 * .
325 * ..
326 * .State
327 * Aggregate.php
328 * Append.php
329 * CallbackFilter.php
330 * composer.json
331 * Counter.php
332 * Demultiplexer.php
333 * …
334 */
335```
336
337Also, the `Hoa\Iterator\Glob` allows to iterator with the glob strategy. It
338extends [`GlobIterator`](http://php.net/globiterator). Thus:
339
340```php
341$glob = new Hoa\Iterator\Glob(resolve('hoa://Library/Iterator') . '/M*.php');
342
343foreach ($glob as $value) {
344 echo $value->getFilename(), "\n";
345}
346
347/**
348 * Will output:
349 * Map.php
350 * Mock.php
351 * Multiple.php
352 */
353```
354
355### Look ahead
356
357`Hoa\Iterator\Lookahead` allows to look ahead for the next element:
358
359```php
360$counter = new Hoa\Iterator\Counter(0, 5, 1);
361$lookahead = new Hoa\Iterator\Lookahead($counter);
362
363foreach ($lookahead as $value) {
364 echo $value;
365
366 if (true === $lookahead->hasNext()) {
367 echo ' (next: ', $lookahead->getNext(), ')';
368 }
369
370 echo "\n";
371}
372
373/**
374 * Will output:
375 * 0 (next: 1)
376 * 1 (next: 2)
377 * 2 (next: 3)
378 * 3 (next: 4)
379 * 4
380 */
381```
382
383The `Hoa\Iterator\Lookbehind` also exists and allows to look behind for the
384previous element.
385
386### Buffer
387
388`Hoa\Iterator\Buffer` allows to move forward as usual but also backward up to a
389given buffer size over another iterator:
390
391```php
392$abcde = new Hoa\Iterator\Map(['a', 'b', 'c', 'd', 'e']);
393$buffer = new Hoa\Iterator\Buffer($abcde, 3);
394
395$buffer->rewind();
396echo $buffer->current(); // a
397
398$buffer->next();
399echo $buffer->current(); // b
400
401$buffer->next();
402echo $buffer->current(); // c
403
404$buffer->previous();
405echo $buffer->current(); // b
406
407$buffer->previous();
408echo $buffer->current(); // a
409
410$buffer->next();
411echo $buffer->current(); // b
412
413/**
414 * Will output:
415 * abcbab
416 */
417```
418
419### Callback generator
420
421`Hoa\Iterator\CallbackGenerator` allows to transform any callable into an
422iterator. This is very useful when combined with other iterators, for instance
423with `Hoa\Iterator\Limit`:
424
425```php
426$generator = new Hoa\Iterator\CallbackGenerator(function ($key) {
427 return mt_rand($key, $key * 2);
428});
429$limit = new Hoa\Iterator\Limit($generator, 0, 10);
430
431foreach ($limit as $value) {
432 echo $value, ' ';
433}
434
435/**
436 * Could output:
437 * 0 2 3 4 4 7 8 10 12 18
438 */
439```
440
441### Recursive iterators
442
443A recursive iterator is an iterator where its values is other iterators. The
444most important interface is `Hoa\Iterator\Recursive\Recursive` (it extends
445[`RecursiveIterator`](http://php.net/recursiveiterator)). Then we find (in
446alphabetic order):
447
448 * `Hoa\Iterator\Recursive\CallbackFilter` (it extends
449 [`RecursiveCallbackFilterIterator`](http://php.net/recursivecallbackfilteriterator)),
450 * `Hoa\Iterator\Recursive\Directory` (it extends
451 [`RecursiveDirectoryIterator`](http://php.net/recursivedirectoryiterator)),
452 * `Hoa\Iterator\Recursive\Filter` (it extends
453 [`RecursiveFilterIterator`](http://php.net/recursivefilteriterator)),
454 * `Hoa\Iterator\Recursive\Iterator` (it extends
455 [`RecursiveIteratorIterator`](http://php.net/recursiveiteratoriterator)),
456 * `Hoa\Iterator\Recursive\Map` (it extends
457 [`RecursiveArrayIterator`](http://php.net/recursivearrayiterator)),
458 * `Hoa\Iterator\Recursive\Mock`,
459 * `Hoa\Iterator\Recursive\RegularExpression`
460 (it extends [`RecursiveRegularExpression`](http://php.net/recursiveregexiterator)).
461
462## Documentation
463
464The
465[hack book of `Hoa\Iterator`](https://central.hoa-project.net/Documentation/Library/Iterator) contains
466detailed information about how to use this library and how it works.
467
468To generate the documentation locally, execute the following commands:
469
470```sh
471$ composer require --dev hoa/devtools
472$ vendor/bin/hoa devtools:documentation --open
473```
474
475More documentation can be found on the project's website:
476[hoa-project.net](https://hoa-project.net/).
477
478## Getting help
479
480There are mainly two ways to get help:
481
482 * On the [`#hoaproject`](https://webchat.freenode.net/?channels=#hoaproject)
483 IRC channel,
484 * On the forum at [users.hoa-project.net](https://users.hoa-project.net).
485
486## Contribution
487
488Do you want to contribute? Thanks! A detailed [contributor
489guide](https://hoa-project.net/Literature/Contributor/Guide.html) explains
490everything you need to know.
491
492## License
493
494Hoa is under the New BSD License (BSD-3-Clause). Please, see
495[`LICENSE`](https://hoa-project.net/LICENSE) for details.
496