emeraldinspirations/lib-helper-pipe

Shell 管道(|)概念的 PHP 实现

1.1.1 2017-09-05 15:39 UTC

This package is not auto-updated.

Last update: 2024-09-26 08:51:52 UTC


README

emeraldinspirations logo

lib-helper-pipe

这是在 emeraldinspirationlibrary 中的一个辅助工具。

Shell 管道 | 概念的 PHP 实现

灵感来自

PHP 还没有一种语法,可以将一个函数的输出直接传递给另一个函数作为输入,而无需嵌套调用

<?php

return new \ArrayObject(
    [
        implode(
            '',
            array_reverse(
                str_split(
                    strtoupper(
                        'test string'
                    )
                )
            )
        )
    ]
);

这很混乱,难以阅读。而且它将函数的顺序颠倒。

这个类提供了一个替代选项。它允许使用 this 函数从一个函数创建到另一个函数的更清晰的管道

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

安装/入门

此项目没有依赖项,因此可以直接使用 composer 需求。

composer require emeraldinspirations/lib-helper-pipe

未来功能

在上面的示例中,需要对 implode 函数进行参数预处理。未来的一个功能可能包括向 thenTo 调用添加额外参数的方法。

<?php

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


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

许可

此项目的代码根据 MIT 许可证 许可。