PHPackages                             trunglv/iterator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. trunglv/iterator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

trunglv/iterator
================

The Hoa\\Iterator library.

02PHP

Since May 13Pushed 2y agoCompare

[ Source](https://github.com/trunglv/Iterator)[ Packagist](https://packagist.org/packages/trunglv/iterator)[ RSS](/packages/trunglv-iterator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

 [![Hoa](https://camo.githubusercontent.com/2b5c32c5d4bc5e9298821b22d364a522e2dbc0295c1c011b1f9f86a4d07df07e/68747470733a2f2f7374617469632e686f612d70726f6a6563742e6e65742f496d6167652f486f612e737667)](https://camo.githubusercontent.com/2b5c32c5d4bc5e9298821b22d364a522e2dbc0295c1c011b1f9f86a4d07df07e/68747470733a2f2f7374617469632e686f612d70726f6a6563742e6e65742f496d6167652f486f612e737667)

---

 [![Build status](https://camo.githubusercontent.com/ee2b785dfe3893319c1dc0ed8bc54a2c6b82dbb38151eddbae204bb13f10fa8c/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f686f6170726f6a6563742f4974657261746f722f6d61737465722e737667)](https://travis-ci.org/hoaproject/Iterator) [![Code coverage](https://camo.githubusercontent.com/9b83278ac0ba00ededee763d90476049d68a523dea9e44e11aa6aa8bc63dc204/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f686f6170726f6a6563742f4974657261746f722f6d61737465722e737667)](https://coveralls.io/github/hoaproject/Iterator?branch=master) [![Packagist](https://camo.githubusercontent.com/34f491c42b774d022c413ec5677464edcf2b87cfce8f8fa31a4095696610dca5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f686f612f6974657261746f722e737667)](https://packagist.org/packages/hoa/iterator) [![License](https://camo.githubusercontent.com/c46f90b746aa9e8a6746701b7afb79753dcc74c4806fdecf43866bbf53d045a4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f686f612f6974657261746f722e737667)](https://hoa-project.net/LICENSE)

 Hoa is a **modular**, **extensible** and **structured** set of PHP libraries.
 Moreover, Hoa aims at being a bridge between industrial and research worlds.

Hoa\\Iterator
=============

[](#hoaiterator)

[![Help on IRC](https://camo.githubusercontent.com/4dbc9c9d28c30cf1ab591f4bb8212fe4dbddc734145df532a9bb86b09878d4c6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f68656c702d253233686f6170726f6a6563742d6666303036362e737667)](https://webchat.freenode.net/?channels=#hoaproject)[![Help on Gitter](https://camo.githubusercontent.com/8c4c85951788ff606b1268cb3dd946be05e3054795455d0a7b9250711bc2ac05/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f68656c702d6769747465722d6666303036362e737667)](https://gitter.im/hoaproject/central)[![Documentation](https://camo.githubusercontent.com/7059ad5f1a363f9098686c59d432f01d7330aed9d4b6c8111d985fd64cfc6c60/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f63756d656e746174696f6e2d6861636b5f626f6f6b2d6666303036362e737667)](https://central.hoa-project.net/Documentation/Library/Iterator)[![Board](https://camo.githubusercontent.com/fd81654ba14b3aca3a713e1b471bc3fc3ba7b5bb3761ccffd6eea2e2ed1fa5ca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6f7267616e69736174696f6e2d626f6172642d6666303036362e737667)](https://waffle.io/hoaproject/iterator)

This library provides a set of useful iterator (compatible with PHP iterators). Existing PHP iterators have been updated to get new features and prior PHP versions compatibility.

[Learn more](https://central.hoa-project.net/Documentation/Library/Iterator).

Installation
------------

[](#installation)

With [Composer](https://getcomposer.org/), to include this library into your dependencies, you need to require [`hoa/iterator`](https://packagist.org/packages/hoa/iterator):

```
$ composer require hoa/iterator '~2.0'
```

For more installation procedures, please read [the Source page](https://hoa-project.net/Source.html).

Testing
-------

[](#testing)

Before running the test suites, the development dependencies must be installed:

```
$ composer install
```

Then, to run all the test suites:

```
$ vendor/bin/hoa test:run
```

For more information, please read the [contributor guide](https://hoa-project.net/Literature/Contributor/Guide.html).

Quick usage
-----------

[](#quick-usage)

We propose a quick overview of all iterators.

### The One

[](#the-one)

`Hoa\Iterator\Iterator` defines the basis of an iterator. It extends [`Iterator`](http://php.net/class.iterator).

### External iterator

[](#external-iterator)

`Hoa\Iterator\Aggregate` allows a class to use an external iterator through the `getIterator` method. It extends [`IteratorAggregate`](http://php.net/iteratoraggregate)

### Traversable to iterator

[](#traversable-to-iterator)

`Hoa\Iterator\IteratorIterator` transforms anything that is [traversable](http://php.net/traversable) into an iterator. It extends [`IteratorIterator`](http://php.net/iteratoriterator).

### Iterator of iterators

[](#iterator-of-iterators)

`Hoa\Iterator\Outer` represents an iterator that iterates over iterators. It extends [`OuterIterator`](http://php.net/outeriterator).

### Mock

[](#mock)

`Hoa\Iterator\Mock` represents an empty iterator. It extends [`EmptyIterator`](http://php.net/emptyiterator).

### Seekable

[](#seekable)

`Hoa\Iterator\Seekable` represents an iterator that can be seeked. It extends [`SeekableIterator`](http://php.net/seekableiterator).

### Map

[](#map)

`Hoa\Iterator\Map` allows to iterate an array. It extends [`ArrayIterator`](http://php.net/arrayiterator).

```
$foobar = new Hoa\Iterator\Map(['f', 'o', 'o', 'b', 'a', 'r']);

foreach ($foobar as $value) {
    echo $value;
}

/**
 * Will output:
 *     foobar
 */
```

### Filters

[](#filters)

`Hoa\Iterator\Filter` and `Hoa\Iterator\CallbackFilter` allows to filter the content of an iterator. It extends [`FilterIterator`](http://php.net/filteriterator) and [`CallbackFilterIterator`](http://php.net/callbackfilteriterator).

```
$filter = new Hoa\Iterator\CallbackFilter(
    $foobar,
    function ($value, $key, $iterator) {
        return false === in_array($value, ['a', 'e', 'i', 'o', 'u']);
    }
);

foreach ($filter as $value) {
    echo $value;
}

/**
 * Will output:
 *     fbr
 */
```

Also, `Hoa\Iterator\RegularExpression` allows to filter based on a regular expression.

### Limit

[](#limit)

`Hoa\Iterator\Limit` allows to iterate *n* elements of an iterator starting from a specific offset. It extends [`LimitIterator`](http://php.net/limititerator).

```
$limit = new Hoa\Iterator\Limit($foobar, 2, 3);

foreach ($limit as $value) {
    echo $value;
}

/**
 * Will output:
 *     oba
 */
```

### Infinity

[](#infinity)

`Hoa\Iterator\Infinite` allows to iterate over and over again the same iterator. It extends [`InfiniteIterator`](http://php.net/infiniteiterator).

```
$infinite = new Hoa\Iterator\Infinite($foobar);
$limit    = new Hoa\Iterator\Limit($infinite, 0, 21);

foreach ($limit as $value) {
    echo $value;
}

/**
 * Will output:
 *     foobarfoobarfoobarfoo
 */
```

Also, `Hoa\Iterator\NoRewind` is an iterator that does not rewind. It extends [`NoRewindIterator`](http://php.net/norewinditerator).

### Repeater

[](#repeater)

`Hoa\Iterator\Repeater` allows to repeat an iterator *n* times.

```
$repeater = new Hoa\Iterator\Repeater(
    $foobar,
    3,
    function ($i) {
        echo "\n";
    }
);

foreach ($repeater as $value) {
    echo $value;
}

/**
 * Will output:
 *     foobar
 *     foobar
 *     foobar
 */
```

### Counter

[](#counter)

`Hoa\Iterator\Counter` is equivalent to a `for($i = $from, $i < $to, $i += $step)` loop.

```
$counter = new Hoa\Iterator\Counter(0, 12, 3);

foreach ($counter as $value) {
    echo $value, ' ';
}

/**
 * Will output:
 *     0 3 6 9
 */
```

### Union of iterators

[](#union-of-iterators)

`Hoa\Iterator\Append` allows to iterate over iterators one after another. It extends [`AppendIterator`](http://php.net/appenditerator).

```
$counter1 = new Hoa\Iterator\Counter(0, 12, 3);
$counter2 = new Hoa\Iterator\Counter(13, 23, 2);
$append   = new Hoa\Iterator\Append();
$append->append($counter1);
$append->append($counter2);

foreach ($append as $value) {
    echo $value, ' ';
}

/**
 * Will output:
 *     0 3 6 9 13 15 17 19 21
 */
```

### Multiple

[](#multiple)

`Hoa\Iterator\Multiple` allows to iterate over several iterator at the same times. It extends [`MultipleIterator`](http://php.net/multipleiterator).

```
$foobar   = new Hoa\Iterator\Map(['f', 'o', 'o', 'b', 'a', 'r']);
$baz      = new Hoa\Iterator\Map(['b', 'a', 'z']);
$multiple = new Hoa\Iterator\Multiple(
    Hoa\Iterator\Multiple::MIT_NEED_ANY
  | Hoa\Iterator\Multiple::MIT_KEYS_ASSOC
);
$multiple->attachIterator($foobar, 'one', '!');
$multiple->attachIterator($baz,    'two', '?');

foreach ($multiple as $iterators) {
    echo $iterators['one'], ' | ', $iterators['two'], "\n";
}

/**
 * Will output:
 *     f | b
 *     o | a
 *     o | z
 *     b | ?
 *     a | ?
 *     r | ?
 */
```

### Demultiplexer

[](#demultiplexer)

`Hoa\Iterator\Demultiplexer` demuxes result from another iterator. This iterator is somehow the opposite of the `Hoa\Iterator\Multiple` iterator.

```
$counter  = new Hoa\Iterator\Counter(0, 10, 1);
$multiple = new Hoa\Iterator\Multiple();
$multiple->attachIterator($counter);
$multiple->attachIterator(clone $counter);
$demultiplexer = new Hoa\Iterator\Demultiplexer(
    $multiple,
    function ($current) {
        return $current[0] * $current[1];
    }
);

foreach ($demultiplexer as $value) {
    echo $value, ' ';
}

/**
 * Will output:
 *     0 1 4 9 16 25 36 49 64 81
 */
```

### File system

[](#file-system)

`Hoa\Iterator\Directory` and `Hoa\Iterator\FileSystem` allow to iterate the file system where files are represented by instances of `Hoa\Iterator\SplFileInfo`. They respectively extend [`DirectoryIterator`](http://php.net/directoryiterator), [`FilesystemIterator`](http://php.net/filesystemiterator) and [`SplFileInfo`](http://php.net/splfileinfo).

```
$directory = new Hoa\Iterator\Directory(resolve('hoa://Library/Iterator'));

foreach ($directory as $value) {
    echo $value->getFilename(), "\n";
}

/**
 * Will output:
 *     .
 *     ..
 *     .State
 *     Aggregate.php
 *     Append.php
 *     CallbackFilter.php
 *     composer.json
 *     Counter.php
 *     Demultiplexer.php
 *     …
 */
```

Also, the `Hoa\Iterator\Glob` allows to iterator with the glob strategy. It extends [`GlobIterator`](http://php.net/globiterator). Thus:

```
$glob = new Hoa\Iterator\Glob(resolve('hoa://Library/Iterator') . '/M*.php');

foreach ($glob as $value) {
    echo $value->getFilename(), "\n";
}

/**
 * Will output:
 *     Map.php
 *     Mock.php
 *     Multiple.php
 */
```

### Look ahead

[](#look-ahead)

`Hoa\Iterator\Lookahead` allows to look ahead for the next element:

```
$counter   = new Hoa\Iterator\Counter(0, 5, 1);
$lookahead = new Hoa\Iterator\Lookahead($counter);

foreach ($lookahead as $value) {
    echo $value;

    if (true === $lookahead->hasNext()) {
        echo ' (next: ', $lookahead->getNext(), ')';
    }

    echo "\n";
}

/**
 * Will output:
 *     0 (next: 1)
 *     1 (next: 2)
 *     2 (next: 3)
 *     3 (next: 4)
 *     4
 */
```

The `Hoa\Iterator\Lookbehind` also exists and allows to look behind for the previous element.

### Buffer

[](#buffer)

`Hoa\Iterator\Buffer` allows to move forward as usual but also backward up to a given buffer size over another iterator:

```
$abcde  = new Hoa\Iterator\Map(['a', 'b', 'c', 'd', 'e']);
$buffer = new Hoa\Iterator\Buffer($abcde, 3);

$buffer->rewind();
echo $buffer->current(); // a

$buffer->next();
echo $buffer->current(); // b

$buffer->next();
echo $buffer->current(); // c

$buffer->previous();
echo $buffer->current(); // b

$buffer->previous();
echo $buffer->current(); // a

$buffer->next();
echo $buffer->current(); // b

/**
 * Will output:
 *     abcbab
 */
```

### Callback generator

[](#callback-generator)

`Hoa\Iterator\CallbackGenerator` allows to transform any callable into an iterator. This is very useful when combined with other iterators, for instance with `Hoa\Iterator\Limit`:

```
$generator = new Hoa\Iterator\CallbackGenerator(function ($key) {
    return mt_rand($key, $key * 2);
});
$limit = new Hoa\Iterator\Limit($generator, 0, 10);

foreach ($limit as $value) {
    echo $value, ' ';
}

/**
 * Could output:
 *     0 2 3 4 4 7 8 10 12 18
 */
```

### Recursive iterators

[](#recursive-iterators)

A recursive iterator is an iterator where its values is other iterators. The most important interface is `Hoa\Iterator\Recursive\Recursive` (it extends [`RecursiveIterator`](http://php.net/recursiveiterator)). Then we find (in alphabetic order):

- `Hoa\Iterator\Recursive\CallbackFilter` (it extends [`RecursiveCallbackFilterIterator`](http://php.net/recursivecallbackfilteriterator)),
- `Hoa\Iterator\Recursive\Directory` (it extends [`RecursiveDirectoryIterator`](http://php.net/recursivedirectoryiterator)),
- `Hoa\Iterator\Recursive\Filter` (it extends [`RecursiveFilterIterator`](http://php.net/recursivefilteriterator)),
- `Hoa\Iterator\Recursive\Iterator` (it extends [`RecursiveIteratorIterator`](http://php.net/recursiveiteratoriterator)),
- `Hoa\Iterator\Recursive\Map` (it extends [`RecursiveArrayIterator`](http://php.net/recursivearrayiterator)),
- `Hoa\Iterator\Recursive\Mock`,
- `Hoa\Iterator\Recursive\RegularExpression`(it extends [`RecursiveRegularExpression`](http://php.net/recursiveregexiterator)).

Documentation
-------------

[](#documentation)

The [hack book of `Hoa\Iterator`](https://central.hoa-project.net/Documentation/Library/Iterator) contains detailed information about how to use this library and how it works.

To generate the documentation locally, execute the following commands:

```
$ composer require --dev hoa/devtools
$ vendor/bin/hoa devtools:documentation --open
```

More documentation can be found on the project's website: [hoa-project.net](https://hoa-project.net/).

Getting help
------------

[](#getting-help)

There are mainly two ways to get help:

- On the [`#hoaproject`](https://webchat.freenode.net/?channels=#hoaproject)IRC channel,
- On the forum at [users.hoa-project.net](https://users.hoa-project.net).

Contribution
------------

[](#contribution)

Do you want to contribute? Thanks! A detailed [contributor guide](https://hoa-project.net/Literature/Contributor/Guide.html) explains everything you need to know.

License
-------

[](#license)

Hoa is under the New BSD License (BSD-3-Clause). Please, see [`LICENSE`](https://hoa-project.net/LICENSE) for details.

###  Health Score

13

—

LowBetter than 1% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity2

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity18

Early-stage or recently created project

 Bus Factor1

Top contributor holds 86.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/15e9d0c7c6322100e7f10b8efb4ed2a28193b52d7ba32d96033635838c7c09f0?d=identicon)[trunglv](/maintainers/trunglv)

---

Top Contributors

[![Hywan](https://avatars.githubusercontent.com/u/946104?v=4)](https://github.com/Hywan "Hywan (106 commits)")[![trunglv](https://avatars.githubusercontent.com/u/820411?v=4)](https://github.com/trunglv "trunglv (7 commits)")[![vonglasow](https://avatars.githubusercontent.com/u/1275202?v=4)](https://github.com/vonglasow "vonglasow (3 commits)")[![jubianchi](https://avatars.githubusercontent.com/u/327237?v=4)](https://github.com/jubianchi "jubianchi (2 commits)")[![shulard](https://avatars.githubusercontent.com/u/482993?v=4)](https://github.com/shulard "shulard (1 commits)")[![Metalaka](https://avatars.githubusercontent.com/u/5406767?v=4)](https://github.com/Metalaka "Metalaka (1 commits)")[![Grummfy](https://avatars.githubusercontent.com/u/668804?v=4)](https://github.com/Grummfy "Grummfy (1 commits)")[![stephpy](https://avatars.githubusercontent.com/u/232744?v=4)](https://github.com/stephpy "stephpy (1 commits)")

### Embed Badge

![Health badge](/badges/trunglv-iterator/health.svg)

```
[![Health](https://phpackages.com/badges/trunglv-iterator/health.svg)](https://phpackages.com/packages/trunglv-iterator)
```

###  Alternatives

[gonetone/laravel-boost-windsurf-extension

Laravel Boost extension package that provides Windsurf editor and Windsurf JetBrains plugin integration.

239.1k](/packages/gonetone-laravel-boost-windsurf-extension)[adereldo/correios

Consulta de cep,rastreio em site do correios

121.4k](/packages/adereldo-correios)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
