transprime-research / piper
PHP Pipe method execution with values from chained method executions
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: >=6.0
- symfony/var-dumper: ^6.0
This package is auto-updated.
Last update: 2024-10-30 00:21:06 UTC
README
piper
PHP Pipe function execution with values from initial call like F#
Pipe Like a PRO 🆗
Quick Usage
Let us take an array and do the following:
- flip the array to make the keys become the values and vice versa
- get the new keys
- change (the keys now) values to upper case
- take the exact item with
[0 => 'ADE']
piper(['name' => 'ade', 'hobby' => 'coding']) ->to('array_flip') ->to('array_keys') ->to('array_map', fn($val) => strtoupper($val)) ->to('array_intersect', [0 => 'ADE'])(); //returns ['ADE']
Or this in PHP 8.1+
Getting ['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D']
examples:
Use line - as in the line in Pipe-"line"
piper("Hello World") ->ln( htmlentities(...), str_split(...), [array_map(...), fn(string $part) => strtoupper($part)], );
Think about ductape on a pipe. Ducter allows such neat calls to your functions
ducter( "Hello World", htmlentities(...), str_split(...), [array_map(...), fn(string $part) => strtoupper($part)], )
Call functions like an array:
_p("Hello World") [htmlentities(...)] [str_split(...)] [[array_map(...), strtoupper(...)]]()
How about Closure() on Closure
_p("Hello World") (htmlentities(...)) (strtoupper(...)) (str_split(...)) (array_map(...), strtoupper(...))()
Cleaner with underscore _
_p("Hello World") ->_(htmlentities(...)) ->_(str_split(...)) ->_(array_map(...), strtoupper(...)));
Shortcut to pipe()
is p()
_p("Hello World") ->p(htmlentities(...)) ->p(str_split(...)) ->p(array_map(...), strtoupper(...))()
PHP 7.4 fn()
like
_p("Hello World") ->fn(htmlentities(...)) ->fn(str_split(...)) ->fn(array_map(...), strtoupper()) ->fn()
Proxied call to globally available functions
_p("Hello World") ->htmlentities() ->str_split() ->array_map(strtoupper(...))()
Instead of:
array_intersect( array_map( fn($val) => strtoupper($val), array_keys( array_flip(['name' => 'ade', 'hobby' => 'coding']) ) ), [0 => 'ADE'] ); //returns ['ADE']
Or:
$arr = array_flip(['name' => 'ade', 'hobby' => 'coding']); // or array_values $arr = array_keys($arr); $arr = array_map(fn($val) => strtoupper($val), $arr); $arr = array_intersect($arr, [0 => 'ADE']); //$arr is ['ADE']
PS: You can still use the old
function() { return v; }
,fn()
is the new short arrow function in PHP 7.4+ See: https://www.php.net/manual/en/functions.arrow.php
Installation
composer require transprime-research/piper
Requirement
Minimum Requirement
- PHP 7.2 +
- Composer
Other Usages
use Transprime\Piper\Piper; // Normal $piper = new Piper(); $piper->pipe(['AA'])->to('implode')->up(); // Better $piper->on(['AA'])->to('implode')();
piper()
function is a helper function. It is normally called like so:
// Nifty piper('AA')->to('strtolower')(); // Good Piper::on('AA')->to('strtolower')->up(); //Better Piper::on('AA')->to('strtolower')();
Piper piper()
function accepts a callable instance on the second parameter that would be immediately on the first parameter:
// test global method piper('NAME', 'strtolower') // NAME becomes name ->to(fn($name) => ucfirst($name)) ->up();
Also accepts a class with an accessible method name. Say we have this class:
class StrManipulator { public function __invoke(string $value) { return $this->strToLower($value); } public static function strToLower(string $value) { return strtolower($value); } }
StrManipulator
class can be passed in with a method like so:
// test class method piper('NAME', StrManipulator::class . '::strToLower') ->to(fn($name) => ucfirst($name)) ->up(); // test array class and method piper('NAME', [StrManipulator::class, 'strToLower']) ->to(fn($name) => ucfirst($name)) ->up();
Even callable object is allowed like so:
// test class object piper('NAME', new StrManipulator()) // A class that implements __invoke ->to(fn($name) => ucfirst($name)) ->up();
Please see \Transprime\Piper\Tests\PiperTest
for more examples.
Coming Soon
- Backward Pipe with
fro()
looking like:
piper('array_intersect', [0 => 'ADE']) ->fro('array_map', fn($val) => strtoupper($val)) ->fro('array_keys') ->fro('array_flip') ->fro(['name' => 'ade', 'age' => 5])();
Api implementation to be decided
Additional Information
See other packages in this series here:
- https://github.com/omitobi/conditional [A smart PHP if...elseif...else statement]
- https://github.com/transprime-research/arrayed [A smarter Array now like an object]
- https://github.com/transprime-research/attempt [A smart PHP try...catch statement]
- https://github.com/omitobi/carbonate [A smart Carbon + Collection package]
- https://github.com/omitobi/laravel-habitue [Jsonable Http Request(er) package with Collections response]
Similar packages
Licence
MIT (See LICENCE file)