Hoa central
README
Hoa

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

Hoa

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.

Installation

With Composer, to include this library into your dependencies, you need to require `hoa/iterator`:

```json { "require": { "hoa/iterator": "~1.0" } } ```

Please, read the website to get more informations about how to install.

Quick usage

We propose a quick overview of the list all iterators.

The One

Hoa\Iterator\Iterator defines the basis of an iterator. It extends `Iterator`.

External iterator

Hoa\Iterator\Aggregate allows a class to use an external iterator through the getIterator method. It extends `IteratorAggregate`

Traversable to iterator

Hoa\Iterator\IteratorIterator transforms anything that is traversable into an iterator. It extends `IteratorIterator`.

Iterator of iterators

Hoa\Iterator\Outer represents an iterator that iterates over iterators. It extends `OuterIterator`.

Mock

Hoa\Iterator\Mock represents an empty iterator. It extends `EmptyIterator`.

Seekable

Hoa\Iterator\Seekable represents an iterator that can be seeked. It extends `SeekableIterator`.

Map

Hoa\Iterator\Map allows to iterate an array. It extends `ArrayIterator`.

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

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

/**

Filters

Hoa\Iterator\Filter and Hoa\Iterator\CallbackFilter allows to filter the content of an iterator. It extends `FilterIterator` and `CallbackFilterIterator`.

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

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

/**

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

Limit

Hoa\Iterator\Limit allows to iterate n elements of an iterator starting from a specific offset. It extends `LimitIterator`.

```php $limit = new Hoa($foobar, 2, 3);

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

/**

Infinity

Hoa\Iterator\Infinite allows to iterate over and over again the same iterator. It extends `InfiniteIterator`.

```php $infinite = new Hoa($foobar); $limit = new Hoa($infinite, 0, 21);

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

/**

Also, Hoa\Iterator\NoRewind is an iterator that does not rewind. It extends `NoRewindIterator`.

Repeater

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

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

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

/**

Counter

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

```php $counter = new Hoa(0, 12, 3);

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

/**

Union of iterators

Hoa\Iterator\Append allows to iterate over iterators one after another. It extends `AppendIterator`.

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

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

/**

Multiple

Hoa\Iterator\Multiple allows to iterate over several iterator at the same times. It extends `MultipleIterator`.

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

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

/**

Demultiplexer

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

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

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

/**

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`, `FilesystemIterator` and `SplFileInfo`.

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

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

/**

Also, the Hoa\Iterator\Glob allows to iterator with the glob strategy. It extends `GlobIterator`. Thus:

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

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

/**

Look ahead

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

```php $counter = new Hoa(0, 5, 1); $lookahead = new Hoa($counter);

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

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

echo "\n"; }

/**

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

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:

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

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

/**

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`). Then we find (in alphabetic order):

Documentation

Different documentations can be found on the website: http://hoa-project.net/.

License

Hoa is under the New BSD License (BSD-3-Clause). Please, see `LICENSE`.