PHPackages                             jeroen-de-dauw/batching-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. jeroen-de-dauw/batching-iterator

Abandoned → [jeroen/batching-iterator](/?search=jeroen%2Fbatching-iterator)Library[Utility &amp; Helpers](/categories/utility)

jeroen-de-dauw/batching-iterator
================================

Iterator that fetches values in batch

3.0.0(8y ago)38951[1 PRs](https://github.com/JeroenDeDauw/BatchingIterator/pulls)1GPL-2.0+PHPPHP &gt;=7.0CI failing

Since Jul 3Pushed 6y ago1 watchersCompare

[ Source](https://github.com/JeroenDeDauw/BatchingIterator)[ Packagist](https://packagist.org/packages/jeroen-de-dauw/batching-iterator)[ Docs](https://github.com/JeroenDeDauw/BatchingIterator)[ RSS](/packages/jeroen-de-dauw-batching-iterator/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (9)Used By (1)

BatchingIterator
================

[](#batchingiterator)

[![Build Status](https://camo.githubusercontent.com/7488ce302ee096eea5f41f97e45f738e7cf84df3b5c514f9810eb4c0b24902c8/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f4a65726f656e4465446175772f4261746368696e674974657261746f722e706e673f6272616e63683d6d6173746572)](http://travis-ci.org/JeroenDeDauw/BatchingIterator)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/354df8c00c87851c3deb700bc0092acd4575507f326ecfa1bafa842eb71bb467/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4a65726f656e4465446175772f4261746368696e674974657261746f722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/JeroenDeDauw/BatchingIterator/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/6a750c71687f372bafece3a100acfa07fd166efc0747abbb2b01239f9d10086c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f4a65726f656e4465446175772f4261746368696e674974657261746f722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/JeroenDeDauw/BatchingIterator/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/a2f1530dae5314492c817d45ae62e9b463a932f6a92e3433169b14d0b7d25551/68747470733a2f2f706f7365722e707567782e6f72672f6a65726f656e2f6261746368696e672d6974657261746f722f76657273696f6e2e706e67)](https://packagist.org/packages/jeroen/batching-iterator)[![Download count](https://camo.githubusercontent.com/140b4b33fe521c34f5d8729709cd94d0b5578b484e6db4f4ae4cca13be915cb7/68747470733a2f2f706f7365722e707567782e6f72672f6a65726f656e2f6261746368696e672d6974657261746f722f642f746f74616c2e706e67)](https://packagist.org/packages/jeroen/batching-iterator)

Small library providing an `Iterator` that batches requests for additional values. This is useful as a foundation for iterators over data that is in an expensive to access location, such as a database or a web API.

Class overview
--------------

[](#class-overview)

Core interface:

- `BatchingIterator` - iterator that batches requests via a `BatchingFetcher`
- `BatchingFetcher` - interface with `fetchNext` method. You will likely need to create an implementation

Utilities:

- `MultipleBatchingFetcher` - combines a number of `BatchingFetcher` instances into one
- `InMemoryBatchingFetcher` - adapts an `array` to the `BatchingFetcher` interface
- `IteratorBasedBatchingFetcher` - adapts an `Iterator` to the `BatchingFetcher` interface

Usage
-----

[](#usage)

Create a service that uses an `Iterator`.

```
class TweetImporter {

    public function importTweets( Iterator $tweets ) {
        foreach ( $tweets as $tweet ) {
            $this->tweetStore->saveTweet( $tweet );
        }
    }

}
```

Note how this service only depends on Iterator. It is not aware of how the Iterator provides its results. You thus decoupled the service from who retrieves the results, and from when this happens. They could be coming from values already in memory, wrapped in an `ArrayIterator`, or be pulled from a web service as iteration happens. Using an `ArrayIterator` is very helpful for testing.

Implement the `BatchingFetcher` interface. If you already have a service to retrieve the data, this can be a simple wrapper.

```
class BatchingTweetFetcher implements BatchingFetcher {

    public function fetchNext( $maxFetchCount ) {
        // Make a call to some external service to fetch $tweets
        return $tweets;
    }

    public function rewind() {
        // Go back to the first tweet
    }

}
```

Now you can easily instantiate the service, have the batching optimization, and have all responsibilities nicely decoupled.

```
class TweetImportCli {

    public function importTweets() {
        $tweetIterator = new BatchingIterator( new BatchingTweetFetcher() );
        $tweetIterator->setMaxBatchSize( 42 );

        $this->tweetImporter->importTweets( $tweetIterator );
    }

}
```

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

[](#installation)

You can use [Composer](http://getcomposer.org/) to download and install this package as well as its dependencies.

To add this package as a local, per-project dependency to your project, simply add a dependency on `jeroen/batching-iterator` to your project's `composer.json` file. Here is a minimal example of a `composer.json` file that just defines a dependency on BatchingIterator 3.x:

```
{
    "require": {
        "jeroen/batching-iterator": "~3.0"
    }
}
```

Release notes
-------------

[](#release-notes)

### Version 3.0.0 (2017-05-16)

[](#version-300-2017-05-16)

- Dropped support for PHP 5.x
- Added scalar and return type hints

### Version 2.1.2 (2014-11-02)

[](#version-212-2014-11-02)

- The max batch size in `BatchingIterator` now defaults to 10, avoiding usage of the iterator without this value being set.

### Version 2.1.1 (2014-08-19)

[](#version-211-2014-08-19)

- Release with package name `jeroen/batching-iterator` instead of `jeroen-de-dauw/batching-iterator`.

### Version 2.1 (2014-07-19)

[](#version-21-2014-07-19)

- `MultipleBatchingFetcher` now accepts an array of `BatchingFetcher` in its constructor

### Version 2.0 (2014-07-19)

[](#version-20-2014-07-19)

Breaking changes:

- Added `rewind` method to the `BatchingFetcher` interface
- Renamed `BatchingIterator\InMemoryBatchingFetcher` to `BatchingIterator\Fetchers\InMemoryBatchingFetcher`

New features and enhancements:

- `BatchingIterator` can now be iterated over multiple times
- Added `MultipleBatchingFetcher`
- Added `IteratorBasedBatchingFetcher`

### Version 1.0 (2014-07-03)

[](#version-10-2014-07-03)

Initial release with

- `BatchingIterator` class
- `BatchingFetcher` interface
- `InMemoryBatchingFetcher` trivial implementation of `BatchingFetcher`

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 97.6% 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.

###  Release Activity

Cadence

Every ~209 days

Recently: every ~258 days

Total

6

Last Release

3281d ago

Major Versions

1.0 → 2.02014-07-18

2.1.2 → 3.0.02017-05-16

PHP version history (2 changes)1.0PHP &gt;=5.3.0

3.0.0PHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/451bd4039d530fed8f9c3da91bfa519233a397d2182cdfdcad700f6cfea19b7f?d=identicon)[Jeroen De Dauw](/maintainers/Jeroen%20De%20Dauw)

---

Top Contributors

[![JeroenDeDauw](https://avatars.githubusercontent.com/u/146040?v=4)](https://github.com/JeroenDeDauw "JeroenDeDauw (41 commits)")[![jdreesen](https://avatars.githubusercontent.com/u/424602?v=4)](https://github.com/jdreesen "jdreesen (1 commits)")

---

Tags

iteratorBatching

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/jeroen-de-dauw-batching-iterator/health.svg)

```
[![Health](https://phpackages.com/badges/jeroen-de-dauw-batching-iterator/health.svg)](https://phpackages.com/packages/jeroen-de-dauw-batching-iterator)
```

###  Alternatives

[nikic/iter

Iteration primitives using generators

1.1k5.9M38](/packages/nikic-iter)[loophp/collection

A (memory) friendly, easy, lazy and modular collection class.

745663.8k13](/packages/loophp-collection)[athari/yalinqo

YaLinqo, a LINQ-to-objects library for PHP

4561.2M5](/packages/athari-yalinqo)[ihor/nspl

Non-standard PHP library (NSPL) - functional primitives toolbox and more

381368.5k](/packages/ihor-nspl)[ginq/ginq

LINQ to Object inspired DSL for PHP

192257.5k3](/packages/ginq-ginq)[chdemko/sorted-collections

Sorted Collections for PHP &gt;= 8.2

222.5M3](/packages/chdemko-sorted-collections)

PHPackages © 2026

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