Getting Started¶
Requirements¶
- PHP 5.5 or greater.
Installation¶
You can install Pipeline with Composer by adding "webbhuset/pipeline": "*" to your
composer.json or by running composer require webbhuset/pipeline in your terminal.
Building Pipeline Functions¶
The easiest way to construct Pipeline functions is to use the Constructor class. It has a static functions for constructing every Pipeline function, allowing you to construct all functions in a concise manner with a single use statement.
<?php
use Webbhuset\Pipeline\Constructor as F;
$take = F::Take(2);
$input = [1, 3, 5, 7, 9];
echo json_encode(iterator_to_array($take($input)));
// Output: [1,3]
It is of course also possible to construct the functions with new if that is preferred.
Using the Result¶
Since all Pipeline functions return a Generator they are actually not executed until the generator is iterated, and you cannot iterate the result more than once. If you need to iterate more than once, consider converting the result to an array using iterator_to_array().