PHP code example of inmanturbo / pipes
1. Go to this page and download the library: Download inmanturbo/pipes 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/ */
inmanturbo / pipes example snippets
use function Inmanturbo\Pipes\pipe;
$addOne = fn ($number = 0) => $number + 1;
$five = pipe($addOne) // 1
->pipe(fn ($number) => $number + 1) // 2
->pipe($addOne) // 3
->pipe($addOne) // 4
->thenReturn(); // 4
$fifty = pipe(1);
while ($fifty->result() < 50) {
$fifty->pipe(fn ($number) => ++$number);
}
echo $fifty->result();
// 50
use function Inmanturbo\Pipes\pipe;
class Subtract
{
public function __invoke($number)
{
return $number - 1;
}
}
$addOne = fn ($number = 0) => $number + 1;
$six = pipe($addOne, 1)
->pipe($addOne)
->pipe($addOne)
->pipe($addOne)
->then(fn ($number) => ++$number);
$five = pipe($six)
->pipe(Subtract::class)
->thenReturn();
$three = pipe(new Subtract, $five)
->pipe(new Subtract)
->thenReturn();
$addOne = fn ($number = 0) => $number + 1;
$six = pipe($addOne, 1)
->pipe($addOne)
->pipe($addOne)
->pipe($addOne)
->then(fn ($number) => ++$number);
$sixAgain = pipe($addOne, 1)
->pipe($addOne)
->pipe($addOne)
->pipe($addOne)
->thenReturn(fn ($number) => ++$number);
$five = pipe($addOne, 1)
->pipe($addOne)
->pipe($addOne)
->pipe($addOne)
->result();
use function Inmanturbo\Pipes\{pipe, halt};
$fortyFive = pipe(1);
$count = 1;
while ($count < 50) {
$fortyFive->pipe(fn ($number) => $number < 45 ? ++$number : halt($number));
$count ++;
}
echo $fortyFive->result();
// 45
echo $fortyFive->pipe(fn ($number) => ++$number)->result();
// 45
use function Inmanturbo\Pipes\{pipe, halt};
$fortyFive = pipe(1);
$count = 1;
while ($count < 50) {
if (($number = $fortyFive->result()) >= 45) {
$fortyFive->halt($number);
}
$fortyFive->pipe(fn ($number) => ++$number);
$count ++;
}
echo $fortyFive->result();
// 45
echo $fortyFive->pipe(fn ($number) => ++$number)->result();
// 45
use function Inmanturbo\Pipes\{pipe, halt};
$fortySix = pipe(1);
$count = 1;
while ($count < 50) {
if (($number = $fortySix->result()) >= 45) {
$fortySix->halt($number);
}
$fortySix->pipe(fn ($number) => ++$number);
$count ++;
}
echo $fortySix->result();
// 45
echo $fortySix->resume(fn ($number) => ++$number)->result();
// 46
use Illuminate\Pipeline\Pipeline;
use function Inmanturbo\Pipes\hop;
class Add {
public function add($number)
{
return $number +1;
}
}
class InvokeAdd {
public function __invoke($number)
{
return $number +1;
}
}
$five = (new Pipeline)->send(1)
->pipe(hop(fn($number) => $number +1))
->pipe(hop(new InvokeAdd))
->pipe(hop(InvokeAdd::class))
->pipe(hop(fn($number) => (new Add)->add($number)))
->thenReturn();
// 5
$limitThreeMiddleware = function ($number, $next) {
if($number >= 3) {
Log::info('Limit hit');
return $number;
}
return $next($number);
};
$five = (new Pipeline)->send(1)
->pipe(hop(fn($number) => $number +1, $limitThreeMiddleware))
->pipe(hop(new InvokeAdd, $limitThreeMiddleware))
// Limit hit
->pipe(hop(InvokeAdd::class, $limitThreeMiddleware))
->pipe(hop(fn($number) => (new Add)->add($number), $limitThreeMiddleware))
->thenReturn();
// 3