GithubHelp home page GithubHelp logo

transprime-research / piper Goto Github PK

View Code? Open in Web Editor NEW
17.0 2.0 0.0 63 KB

Pipe operation with PHP

Home Page: https://transprime-research.github.io/piper/

License: MIT License

Dockerfile 4.42% PHP 94.00% Shell 1.58%
operator pipe php

piper's Introduction

Build Status Latest Stable Version Total Downloads Latest Unstable Version Latest Monthly Downloads License

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:

Similar packages

Licence

MIT (See LICENCE file)

piper's People

Contributors

omitobi avatar

Stargazers

Aleksandr Denisyuk avatar Dawid Janik avatar  avatar Travis van der F. avatar San B avatar Dan Alvidrez avatar Syed Sirajul Islam Anik avatar Mohsen Sadeghzade avatar Rick avatar Ján Bočínec avatar Flávio H. Ferreira avatar Milan Chheda avatar Brian Faust avatar Samet Sahindogan avatar Clayton Stone avatar noitran avatar  avatar

Watchers

James Cloos avatar  avatar

piper's Issues

Immediate execution and performance management

Test or benchmark the performance of current implementation against executing the functions immediately function is called.

Also consider how to manage execution when there are many items in the execution arrays. Are there benefit in using Generators, or completely newing up new instance of Piper on every function call?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.