emeraldinspirations/lib-helper-pipe

A PHP implementation of the Shell Pipe (|) concept

1.1.1 2017-09-05 15:39 UTC

This package is not auto-updated.

Last update: 2024-03-28 06:29:49 UTC


README

emeraldinspirations logo

lib-helper-pipe

A helper in emeraldinspiration's library.

A PHP implementation of the Shell Pipe | concept

Inspired by:

PHP does not yet have a syntax for piping the output of one function into the input of another function without nesting the call:

<?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.

This class provides an alternate option. It allows using the this function to crate a cleaner looking pipe from one function to another:

<?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();

Installing / Getting started

This project has no dependencies, so can simply be required with composer

composer require emeraldinspirations/lib-helper-pipe

Future Features

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.

<?php

// Example with (callable $Function, array $Prepend = [], array $Append = [])
    // ...
    ->thenTo('implode', [''], [])
    // ...


// Example with (callable $Function, array $ParameterMask = [self::Here])
    // ...
    ->thenTo('implode', ['', Pipe::Here])
    // ...

Licensing

The code in this project is licensed under MIT license.