Pipeline
![Software License][ico-license]
, (*1)
Introduction
yuloh/pipeline is a simple functional pipeline. You can easily chain functions together to process a payload. It's a lot easier to read, and there are less temporary variables than doing it the old fashioned way., (*2)
Goals
This package aims to expose the simplest API possible., (*3)
The programming language elixir has a pipe operator (|>) which lets you easily chain functions:, (*4)
1 |> inc |> double
I really liked that syntax, and I wanted to write a pipeline package where I didn't have to write pipe or add over and over, hence this package., (*5)
Why PHP 7 Only?
PHP 7 introduced the Uniform Variable Syntax, which means we can do this:, (*6)
pipe('hello world')('strrev')('strtoupper')();
Instead of something like this:, (*7)
pipe('hello world')->pipe('strrev')->pipe('strtoupper')->process();
Install
Via Composer, (*8)
``` bash
$ composer require yuloh/pipeline, (*9)
## Usage
To create a new pipeline, invoke the `Yuloh\Pipeline\pipe` function with your payload.
```php
use function Yuloh\Pipeline\Pipe;
$pipe = pipe([1, 2]);
Once it's created you can keep chaining stages by invoking the pipeline. The stage must be a valid PHP callable., (*10)
$pipe('array_sum')('sqrt');
If you invoke the pipeline without a stage, the pipeline will be processed and the processed payload is returned., (*11)
$result = $pipe();
All together, it looks like this:, (*12)
pipe([1, 2])('array_sum')('sqrt')();
Passing Arguments
When adding a stage, any additional arguments will be passed to the stage after the payload. For instance, if you were processing a JSON payload the pipeline might look like this:, (*13)
``` php
use function Yuloh\Pipeline\Pipe;, (*14)
$pastTimes = pipe('{"name": "Matt", "pastTimes": ["playing Legend of Zelda", "programming"]}')
('json_decode', true)
(function ($data) {
return $data['pastTimes'];
})
('implode', ', ')
();, (*15)
echo $pastTimes; // playing Legend of Zelda, programming, (*16)
In the previous example, `json_decode` would be invoked as `json_decode($payload, true)` to return an array.
### Alternative Method Call Usage
You can also add stages as method calls instead of function arguments. It's a little more readable for pipelines that are only using standard functions.
```php
pipe('hello world')
->strrev()
->strtoupper()
->get();
Testing
bash
$ composer test, (*17)
License
The MIT License (MIT). Please see License File for more information., (*18)