1. Go to this page and download the library: Download sanmai/pipeline library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
sanmai / pipeline example snippets
use function Pipeline\take;
// iterable corresponds to arrays, generators, iterators
// we use an array here simplicity sake
$iterable = range(1, 3);
// wrap the initial iterable with a pipeline
$pipeline = take($iterable);
// join side by side with other iterables of any type
$pipeline->zip(
\range(1, 3),
map(function () {
yield 1;
yield 2;
yield 3;
})
);
// lazily process their elements together
$pipeline->unpack(function (int $a, int $b, int $c) {
return $a - $b - $c;
});
// map one value into several more
$pipeline->map(function ($i) {
yield pow($i, 2);
yield pow($i, 3);
});
// simple one-to-one mapper
$pipeline->cast(function ($i) {
return $i - 1;
});
// map into arrays
$pipeline->map(function ($i) {
yield [$i, 2];
yield [$i, 4];
});
// unpack array into arguments
$pipeline->unpack(function ($i, $j) {
yield $i * $j;
});
// one way to filter
$pipeline->map(function ($i) {
if ($i > 50) {
yield $i;
}
});
// this uses a filtering iterator from SPL under the hood
$pipeline->filter(function ($i) {
return $i > 100;
});
// reduce to a single value; can be an array or any value
$value = $pipeline->reduce(function ($carry, $item) {
// for the sake of convenience the default reducer from the simple
// pipeline does summation, just like we do here
return $carry + $item;
}, 0);
var_dump($value);
// int(104)
foreach ($pipeline as $result) {
// Processing happens only if you consume the results.
// Want to stop early after few results? Not a problem here!
}
$pipeline = new \Pipeline\Standard();
$pipeline->map(function () {
// will be executed immediately on the spot, unless yield is used
return $this->veryExpensiveMethod();
})->filter();
$pipeline->map(function () {
// will be executed only as needed, when needed
yield $this->veryExpensiveMethod();
})->filter();
$pipeline = \Pipeline\map(function () {
yield 1;
});
$sum = $pipeline->reduce();
// Won't work the second time though
$pipeline->reduce();
// Exception: Cannot traverse an already closed generator
$iterator = new \IteratorIterator($pipeline);
/** @var $iterator \Iterator */
$pipeline->map(function (Customer $customer) {
foreach ($customer->allPayments() as $item) {
yield $item;
}
});
// Now process all and every payment
$pipeline->map(function (Payment $payment) {
return $payment->amount;
});
$pipeline = new \Pipeline\Standard();
foreach ($pipeline as $value) {
// no errors here
}
$pipeline->runningVariance($varianceForShippedOrders, static function (order $order): ?float {
if (!$order->isShipped()) {
// This order will be excluded from the computation.
return null;
}
return $order->getTotal();
});
$pipeline->runningVariance($varianceForPaidOrders, static function (order $order): ?float {
if ($order->isUnpaid()) {
// This order will be excluded from the computation.
return null;
}
return $order->getProjectedTotal();
});