hoa/iterator
最新稳定版本:2.17.01.10
Composer 安装命令:
composer require hoa/iterator
包简介
The Hoa\Iterator library.
README 文档
README
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
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:
$ composer require hoa/iterator '~2.0'
For more installation procedures, please read the Source page.
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.
Quick usage
We propose a quick overview of 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.
$foobar = new Hoa\Iterator\Map(['f', 'o', 'o', 'b', 'a', 'r']); foreach ($foobar as $value) { echo $value; } /** * Will output: * foobar */
Filters
Hoa\Iterator\Filter and Hoa\Iterator\CallbackFilter allows to filter the
content of an iterator. It extends
FilterIterator and
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
Hoa\Iterator\Limit allows to iterate n elements of an iterator starting from
a specific offset. It extends LimitIterator.
$limit = new Hoa\Iterator\Limit($foobar, 2, 3); foreach ($limit as $value) { echo $value; } /** * Will output: * oba */
Infinity
Hoa\Iterator\Infinite allows to iterate over and over again the same iterator.
It extends 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.
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
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
Hoa\Iterator\Append allows to iterate over iterators one after another. It
extends 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
Hoa\Iterator\Multiple allows to iterate over several iterator at the same
times. It extends 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
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
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.
$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. 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
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
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
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
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):
Hoa\Iterator\Recursive\CallbackFilter(it extendsRecursiveCallbackFilterIterator),Hoa\Iterator\Recursive\Directory(it extendsRecursiveDirectoryIterator),Hoa\Iterator\Recursive\Filter(it extendsRecursiveFilterIterator),Hoa\Iterator\Recursive\Iterator(it extendsRecursiveIteratorIterator),Hoa\Iterator\Recursive\Map(it extendsRecursiveArrayIterator),Hoa\Iterator\Recursive\Mock,Hoa\Iterator\Recursive\RegularExpression(it extendsRecursiveRegularExpression).
Documentation
The
hack book of Hoa\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.
Getting help
There are mainly two ways to get help:
- On the
#hoaprojectIRC channel, - On the forum at users.hoa-project.net.
Contribution
Do you want to contribute? Thanks! A detailed contributor guide explains everything you need to know.
License
Hoa is under the New BSD License (BSD-3-Clause). Please, see
LICENSE for details.
hoa/iterator 适用场景与选型建议
hoa/iterator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22.49M 次下载、GitHub Stars 达 337, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「iterator」 「library」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hoa/iterator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hoa/iterator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hoa/iterator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
collection php library
PHP PDO Mysql select statement iterator implemented as multiple queries using LIMIT clauses
Iteration tools for PHP
Sequential way to run Symfony Processes in parallel
Extends XMLReader PHP class, for simple SAX-reading and XPath queries of huge XML files.
Trait providing methods implementing the Iterator interface.
统计信息
- 总下载量: 22.49M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 337
- 点击次数: 24
- 依赖项目数: 15
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2026-01-04