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

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

sci/stream
==========

Stream library

0.0.1-alpha(10y ago)3306[1 PRs](https://github.com/DrSchimke/stream/pulls)PHPPHP &gt;=5.5.0

Since Jun 27Pushed 8y ago1 watchersCompare

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

READMEChangelogDependencies (2)Versions (3)Used By (0)

Stream
======

[](#stream)

[![Build Status](https://camo.githubusercontent.com/5d4a96cc12d3d8e15cba79da1df2959e187a9a7f1ee94be3d05a81e84f60858c/68747470733a2f2f7472617669732d63692e6f72672f4472536368696d6b652f73747265616d2e737667)](https://travis-ci.org/DrSchimke/stream)

Chainable stream wrapper for arrays and and other traversables.

Here I use the term *stream* **not** in the sense of PHP streamWrappers, but more like in *Pipes and Filters Architecture* – a streaming collection of *things*; actually an *infinite list of things*. (See [also](http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-001-structure-and-interpretation-of-computer-programs-spring-2005/video-lectures/6a-streams-part-1/).)

1. Installation
---------------

[](#1-installation)

Using [composer](https://getcomposer.org/download/):

```
composer require sci/stream dev-master
```

2. Usage
--------

[](#2-usage)

### 2.1 Creating the stream

[](#21-creating-the-stream)

```
use Sci\Stream\ArrayStream;
use Sci\Stream\IteratorStream;

// stream from array
$stream = new ArrayStream([3, 1, 4, 1, 5, 9, 2, 6, 5]);

// stream from ArrayIterator
$stream = new IteratorStream(new \ArrayIterator([2, 7, 1, 8, 2, 8]));

// stream from Generator
function generate()
{
    for ($i = 0; $i < 10; ++$i) {
        yield $i;
    }
}
$stream = new IteratorStream(generate());
```

### 2.2 Map

[](#22-map)

```
$incrementedValues = $stream->map(function ($value) {
    return $value + 1;
});
```

### 2.3 Filter

[](#23-filter)

```
$smallerValues = $stream->filter(function ($value) {
    return $value < 5;
});
```

### 2.4 Reduce

[](#24-reduce)

```
$sum = $stream->reduce(function ($sum, $value) {
    return $sum + $value;
});
```

### 2.5 Chaining

[](#25-chaining)

Stream operations can be chained easily:

```
$stream
    ->filter(function ($value) {
        return $value < 5;
    })
    ->map(function ($value) {
        return 2 * $value;
    })
    ->reduce(function ($sum, $value) {
        return $sum + $value;
    });
```

### 2.6 Getting the Result

[](#26-getting-the-result)

To get a stream's content, use iteration with foreach or the `Stream::toArray()` method:

```
foreach ($stream as $value) {
    // $value ...
}

$array = $stream->toArray();
```

3. Extending
------------

[](#3-extending)

The library is easily extensible by subclassing. For example, we could add conveniently a CSV parser as stream source or some wrapper methods around `Stream::map()` and `Stream::filter()`, to achieve a SQL-like *domain specific language*. (Find the complete example in [StreamExtendingTest](tests/StreamExtendingTest.php).)

```
class CsvStream extends IteratorStream {
    public static function from($filename) {
        return new parent(self::readCsv($filename));
    }

    public function where(array $condition) {
        return $this->filter(function (array $row) use ($condition) {
            return ...;
        });
    }

    public function select(array $columns) {
        return $this->map(function (array $row) use ($columns) {
            $result = [];
            foreach ($columns as $column) {
                if (array_key_exists($column, $row)) {
                    $result[$column] = $row[$column];
                }
            }

            return $result;
        });
    }

    public function limit($offset, $limit) {
        // ...
    }

    private static function readCsv($filename) {
        $fd = fopen($filename, 'r');
        $header = fgetcsv($fd);
        while ($row = fgetcsv($fd)) {
            yield array_combine($header, $row);
        }
        fclose($fd);
    }
}

$csvStream = CsvStream::from('example.csv')
    ->where(['first_name' => 'Peter'])
    ->select(['first_name', 'last_name', 'street', 'zip_code', 'city'])
    ->limit(0, 10);
```

4. License
----------

[](#4-license)

All contents of this package are licensed under the [MIT license](LICENSE).

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3978d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5d3765b40a043c8c7c8ead0ae3cb9691ab6c053d272ec9db1d9bcec0218fb69a?d=identicon)[DrSchimke](/maintainers/DrSchimke)

---

Top Contributors

[![DrSchimke](https://avatars.githubusercontent.com/u/3299009?v=4)](https://github.com/DrSchimke "DrSchimke (28 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[wnx/php-swiss-cantons

Search for Swiss Cantons by name or abbreviation.

1855.4k2](/packages/wnx-php-swiss-cantons)[nami-doc/sprockets-php

Sprockets-PHP is Sprockets (Rails Asset Pipeline) for PHP

496.1k](/packages/nami-doc-sprockets-php)

PHPackages © 2026

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