Chain callables using a simple wrapper class.

0.0.1 2017-11-06 16:43 UTC

This package is auto-updated.

Last update: 2021-10-21 13:30:52 UTC


PHP code can be frustratingly opaque, especially when dealing with nested functions. Why not chain those functions instead?

$filter = function ($item) { return $item === 'SOME'; };

// Offputting one-liner
echo implode('.', array_filter(explode('-', strtoupper(trim('    some-value'))), $filter))

// Multiple assignments
$value = '    some-value';
$value = trim($value);
$value = strtoupper($value);
$value = explode('-', $value);
$value = array_filter($value, $filter);
echo implode('.', $value);

// Easy to read pipe
echo take('    some-value')
    ->explode('-', '$$')
    ->implode('.', '$$')

// prints 'SOME'


Via Composer:

composer require aviator/pipe


Via Composer:

composer test


Get a Pipe object:

$value = new Pipe('value');
$value = Pipe::take('value');
$value = take('value');

Then you can chain callables:


And get the mutated value:

echo $value->get();

// prints 'VALUE'

The pipe method is chainable:

echo Pipe::take('    value')
// prints 'VALUE'

Pipe uses a magic __call to redirect other methods to the pipe method, so you don't have to use pipe(...) at all:

echo Pipe::take('    value')
// prints 'VALUE'


You can use callables with arguments:

echo Pipe::take('value')

Pipe will always pass the value you're mutating ('value' in the example above) as the first parameter.

This works most of the time, but since PHP has some unique parameter ordering, there are cases where it doesn't. In these cases you can use the placeholder, by default $$, to represent the mutating value.

For example, implode():

echo Pipe::take(['some', 'array'])
    ->implode('.', '$$')
// prints 'some.array'

Because implode() takes the input value as its second parameter, we tell Pipe where to put it using '$$'. Then when called the value is swapped in.


You may pipe any callable, including a closure:

$closure = function ($item) { return $item . '-postfixed'; };

echo Pipe::take('value')

// prints 'value-postfixed'

Other Stuff


This package operates under the MIT License (MIT). Please see LICENSE for more information.


This is largely based on Sebastiaan Luca's idea and his Pipe\Item class.