使用链式方法执行值执行PHP管道方法

3.0.0 2024-04-27 14:17 UTC

This package is auto-updated.

Last update: 2024-09-30 00:09:45 UTC


README

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

piper

使用初始调用值执行PHP管道函数,类似于F#

像专业人士一样使用管道 🆗

快速使用

让我们取一个数组并执行以下操作

  • 翻转数组,使键成为值,反之亦然
  • 获取新键
  • 将(现在是键)值更改为大写
  • 获取精确项 [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']

或者PHP 8.1+的以下示例获取 ['H', 'E', 'L', 'L', 'O', ' ', 'W', 'O', 'R', 'L', 'D']

使用行 - 如在管道中的行

piper("Hello World")
    ->ln(
        htmlentities(...),
        str_split(...),
        [array_map(...), fn(string $part) => strtoupper($part)],
    );

想象一下管子上的胶带。Ducter 允许您以这种方式调用函数

ducter(
    "Hello World",
    htmlentities(...),
    str_split(...),
    [array_map(...), fn(string $part) => strtoupper($part)],
)

像数组一样调用函数

_p("Hello World")
    [htmlentities(...)]
    [str_split(...)]
    [[array_map(...), strtoupper(...)]]()

比如Closure()在Closure上

_p("Hello World")
    (htmlentities(...))
    (strtoupper(...))
    (str_split(...))
    (array_map(...), strtoupper(...))()

使用下划线 _ 更干净

_p("Hello World")
    ->_(htmlentities(...))
    ->_(str_split(...))
    ->_(array_map(...), strtoupper(...)));

pipe() 的快捷键是 p()

_p("Hello World")
    ->p(htmlentities(...))
    ->p(str_split(...))
    ->p(array_map(...), strtoupper(...))()

PHP 7.4 fn() 类似

_p("Hello World")
    ->fn(htmlentities(...))
    ->fn(str_split(...))
    ->fn(array_map(...), strtoupper())
    ->fn()

代理调用全局可用函数

_p("Hello World")
    ->htmlentities()
    ->str_split()
    ->array_map(strtoupper(...))()

而不是

array_intersect(
    array_map(
        fn($val) => strtoupper($val),
        array_keys(
            array_flip(['name' => 'ade', 'hobby' => 'coding'])
        )
    ),
    [0 => 'ADE']
); //returns ['ADE']

或者

$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:您仍然可以使用旧的 function() { return v; }fn() 是 PHP 7.4+ 中的新短箭头函数。请参阅:[https://php.ac.cn/manual/en/functions.arrow.php](https://php.ac.cn/manual/en/functions.arrow.php)

安装

  • composer require transprime-research/piper

需求

最低需求

  • PHP 7.2 +
  • Composer

其他用法

use Transprime\Piper\Piper;

// Normal
$piper = new Piper();
$piper->pipe(['AA'])->to('implode')->up();

// Better
$piper->on(['AA'])->to('implode')();

piper() 函数是一个辅助函数。它通常按以下方式调用

// Nifty
piper('AA')->to('strtolower')();

// Good
Piper::on('AA')->to('strtolower')->up();

//Better
Piper::on('AA')->to('strtolower')();

Piper piper() 函数接受第二个参数的可调用实例,该实例将立即放置在第一个参数上

// test global method
piper('NAME', 'strtolower') // NAME becomes name
    ->to(fn($name) => ucfirst($name))
    ->up();

也接受具有可访问方法名称的类。比如说我们有这个类

class StrManipulator
{
    public function __invoke(string $value)
    {
        return $this->strToLower($value);
    }

    public static function strToLower(string $value)
    {
        return strtolower($value);
    }
}

StrManipulator 类可以这样传递

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

甚至可调用对象也是允许的

// test class object
piper('NAME', new StrManipulator()) // A class that implements __invoke
    ->to(fn($name) => ucfirst($name))
    ->up();

请参阅 \Transprime\Piper\Tests\PiperTest 了解更多示例。

即将推出

  • 使用 fro() 的向后管道,看起来像
piper('array_intersect', [0 => 'ADE'])
    ->fro('array_map', fn($val) => strtoupper($val))
    ->fro('array_keys')
    ->fro('array_flip')
    ->fro(['name' => 'ade', 'age' => 5])();

API 实现将决定

附加信息

在此处查看此系列的其它包

类似包

许可证

MIT (见LICENCE文件)