A PHP implementation of the Shell Pipe (|) concept
, (*1)
A helper in emeraldinspiration's library., (*2)
A PHP implementation of the Shell Pipe |
concept, (*3)
Inspired by:
- PHP RFC: Pipe Operator
- Shell's Pipe |
Operator
- Javascript's Promise API
, (*4)
PHP does not yet have a syntax for piping the output of one function into the input of another function without nesting the call:, (*5)
<?php return new \ArrayObject( [ implode( '', array_reverse( str_split( strtoupper( 'test string' ) ) ) ) ] );
This is messy, and hard to read. Plus it puts the functions in reverse order., (*6)
This class provides an alternate option. It allows using the this
function to crate a cleaner looking pipe from one function to another:, (*7)
<?php use emeraldinspirations\library\helper\pipe\Pipe; return (new Pipe('test string')) ->to('strtoupper') ->thenTo('str_split') ->thenTo('array_reverse') ->thenTo( Pipe::delegateWithParamMask('implode', ['', Pipe::here()]) ) ->thenTo( function ($Param) { return [$Param]; } ) ->thenTo( Pipe::delegateConstructor(\ArrayObject::class) ) ->return();
This project has no dependencies, so can simply be required with composer, (*8)
composer require emeraldinspirations/lib-helper-pipe
In the example above there is the need to prepend a parameter to the implode
function. A future feature may include some way to add additional parameters
to thenTo
calls., (*9)
<?php // Example with (callable $Function, array $Prepend = [], array $Append = []) // ... ->thenTo('implode', [''], []) // ... // Example with (callable $Function, array $ParameterMask = [self::Here]) // ... ->thenTo('implode', ['', Pipe::Here]) // ...
The code in this project is licensed under MIT license., (*10)