PHPackages                             jasny/iterator-stream - 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. jasny/iterator-stream

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

jasny/iterator-stream
=====================

Output streams for iterators, formatting and writing one element at a time

021PHP

Since Oct 8Pushed 5y agoCompare

[ Source](https://github.com/jasny/iterator-stream)[ Packagist](https://packagist.org/packages/jasny/iterator-stream)[ RSS](/packages/jasny-iterator-stream/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Jasny Iterator Stream
=====================

[](#jasny-iterator-stream)

[![Build Status](https://camo.githubusercontent.com/16474180a7ed7ab5fbf455a34d6fcbb6f846578374c5e9ea4c9be189fd3a7612/68747470733a2f2f7472617669732d63692e6f72672f6a61736e792f6974657261746f722d73747265616d2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/jasny/iterator-stream)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/793dc5c24764df971a34045345c3646d2e5fcfd0637420f0bdea8152ad60fbbb/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f6974657261746f722d73747265616d2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/iterator-stream/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/0546a8ad30b90aff5960887163a4a0c0fce51a9a51c09bf6886b5edca0a8c7b7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f6974657261746f722d73747265616d2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/iterator-stream/?branch=master)[![Packagist Stable Version](https://camo.githubusercontent.com/9ae0c6f7a440f524facc264a0472fe4b12af4beadaf49ae53c72ca2ac947a436/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a61736e792f6974657261746f722d73747265616d2e737667)](https://packagist.org/packages/jasny/iterator-stream)[![Packagist License](https://camo.githubusercontent.com/9288148bd507d99eebfc68087c39d2a1b44a2e7b14230e8650b0a0b0b1fb6ff0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a61736e792f6974657261746f722d73747265616d2e737667)](https://packagist.org/packages/jasny/iterator-stream)

Output streams for iterators, formatting and writing one element at a time.

*If you're looking for a traversable input stream, take a look at [`SplFileObject`](http://php.net/SplFileObject).*

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

[](#installation)

```
composer require jasny/iterator-stream

```

Usage
-----

[](#usage)

```
$handler = fopen('path/to/some/file.txt');
$stream = new LineOutputStream($handler);

$iterable = ['hello, 'sweet', 'world'];

$stream->write($iterator);
```

The `write()` takes an array or `Traversable` object (not just `Iterators`).

All iterator stream objects take a stream resource or URI (string) as first parameter of the constructor.

```
new LineOutputStream('php://output');
new CsvOutputStream('file://path/to/some/file.csv');
```

If an URI is passed, the iterator stream will open it using `fopen`. Using a scheme is required, also for regular files.

To use it with [PSR-7 streams](https://www.php-fig.org/psr/psr-7/#13-streams), you need to detach the underlying resource and pass it to constructor.

```
$handler = $psr7stream->detach();
$stream = new LineOutputStream($handler);
```

### Prototype pattern

[](#prototype-pattern)

Iterator output streams support the prototype design pattern. They may be created detached of a stream resource. The `withStream` creates a clone of the output stream with the given resource attached.

```
$prototype = new JsonOutputStream();

$stream = $prototype->withStream($resource);
$stream->write($iterable);
```

Prototyping makes the library easier to use with dependency inversion in SOLID applictions.

Output streams
--------------

[](#output-streams)

- [Line](#line)
- [CSV](#csv)
- [JSON](#json)

### Line

[](#line)

Write to a stream line by line.

```
LineOutputStream(resource|string $stream, string $endLine = "\n")
```

### CSV

[](#csv)

Write to a stream as CSV. See [`fputcsv()`](https://php.net/fputcsv).

```
CsvOutputStream(
    resource|string $stream,
    string $delimiter = ',',
    string $enclosure = '"',
    string $escapeChar = '\\'
)
```

The `write()` method optionally takes headers as a seconds argument. These are written as first line.

```
$output = new CsvOutputStream($stream);
$output->write($data, ['name', 'age', 'email']);
```

Each element of the iterable MUST be an array with a fixed number of values. Keys are ignored, if the elements are associative arrays or objects, use [`iterable_project`](https://github.com/jasny/iterable-functions#iterable_project)from `jasny\iterable-functions`.

### JSON

[](#json)

Write to a stream as JSON array. See [`json_encode`](https://php.net/json_encode).

```
JsonOutputStream(resource|string $stream, int $options = 0)
```

Options may be provided as binary set using the `JSON_*` constants.

You can add `JsonOutputStream::OUTPUT_LINES` as option, in which case each element is outputed as line, without turning the complete output into a JSON array.

```
$output = new JsonOutputStream($stream, \JSON_PRETTY_PRINT | JsonOutputStream::OUTPUT_LINES);
```

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity32

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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/3379a93d51305df325df9045e1a8b205d195e4e8c01312dff53a000ee79002eb?d=identicon)[jasny](/maintainers/jasny)

---

Top Contributors

[![jasny](https://avatars.githubusercontent.com/u/100821?v=4)](https://github.com/jasny "jasny (14 commits)")

### Embed Badge

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

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

###  Alternatives

[inovector/mixpostapp

Standalone application with the Laravel Package of Mixpost Lite pre-installed and configured

1232.9k](/packages/inovector-mixpostapp)

PHPackages © 2026

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