定制 transprime-research/piper 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

transprime-research/piper

最新稳定版本:3.0.0

Composer 安装命令:

composer require transprime-research/piper

包简介

PHP Pipe method execution with values from chained method executions

README 文档

README

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)

统计信息

  • 总下载量: 4.64k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 17
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固