hguenot/phpstream

PHPStream claims to be a partial PHP port of the Java Stream API.

V3.0.0 2021-03-07 06:54 UTC

This package is auto-updated.

Last update: 2024-09-24 07:52:39 UTC


README

PHP Stream is a basic port of new Java Stream API in PHP.

This library could be used to filter, convert or reduce any array.

Continuous integration

Build Status Code coverage GitHub last version Packagist last version Packagist downloads

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist hguenot/phpstream "*"

or add

"hguenot/phpstream": "*"

to the require section of your composer.json file.

Usage

  • Here is a basic usage of this library.
// Create a simple array of data
$array = [ -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];

// Creates a Stream based on the previous array
$stream = new \phpstream\Stream($array);

// Compute the opposite value of each value
$stream = $stream->map(function($value){
    return intval($value) * (-1);
});

// Get only odd values
$stream = $stream->filter(function($value){
    return (intval($value) % 2) == 0;
});

// Collects data into an array
$new_array = $stream->collect(new \phpstream\collectors\ListCollector());

// Computes sum of all elements
$sum = $stream->reduce(function($a, $b){
    return intval($a) + intval($b);
}, 0);
  • All stream operations can be chained :
$sum = \phpstream\Stream::of($array)
    ->map(function($value){
        return intval($value) * (-1);
    })
    ->filter(function($value){
        return (intval($value) % 2) == 0;
    })
    ->reduce(function($a, $b){
        return intval($a) + intval($b);
    }, 0);

You can find more examples on PHPUnit test.