burnett01/php-piper
最新稳定版本:v1.4.2
Composer 安装命令:
composer require burnett01/php-piper
包简介
Piper enhances the PHP 8.5 pipe operator (|>) with support for multi-argument functions/callables.
README 文档
README
Piper enhances the PHP 8.5 pipe operator (|>) with support for multi-argument functions/callables.
Background
In PHP the pipe operator (|>) works with single-argument callables, such as strlen, trim, etc..,
$nonce = random_bytes(16) |> base64_encode(...);
but what if you want to use it with multi-argument callables, such as rtrim, strtr, etc..
// does not work $nonce = random_bytes(16) |> base64_encode(...) |> strtr(..., '+/', '-_') |> rtrim(..., '=');
// works but too verbose $nonce = random_bytes(16) |> base64_encode(...) |> (fn(string $s): string => strtr($s, '+/', '-_')) |> (fn($s) => rtrim($s, '='));
This is where Piper comes into play!
How it works
Piper is sort of a decorator/wrapper around a callable for the pipe operator |>.
Usage
composer require burnett01/php-piper
PSR-4 function version:
use function Burnett01\Piper\with; $nonce = random_bytes(16) |> base64_encode(...) |> with('strtr', '+/', '-_') // first-class syntax |> with(rtrim(...), '='); // or pipe() function use function Burnett01\Piper\pipe; $nonce = random_bytes(16) |> base64_encode(...) |> pipe('strtr', '+/', '-_') // with first-class syntax |> pipe(rtrim(...), '=');
PSR-4 class version:
use Burnett01\Piper\Piper as pipe; $nonce = random_bytes(16) |> base64_encode(...) |> pipe::to('strtr', '+/', '-_') // or 'with' + first-class syntax |> pipe::with(rtrim(...), '=');
The ellipsis ... represents the first-class callable syntax.
You can use a callable as string or first-class syntax for passing the method.
Other examples
use function Burnett01\Piper\with; $actual = -1234.5 |> abs(...) |> with(number_format(...), 2, '.', ',') |> urlencode(...);
Api
-
Piper::to(callable $fn, mixed ...$args)Creates an instance of Piper for the specificed
$fn.Parameters:
-
callable
$fn- The name of a callable as string (eg.'strlen') or as first-class syntax (eg.strlen(...)) -
variadic (mixed)
$args- The arguments for$fn
Context: static
Returns: instance
-
-
Piper::with(callable $fn, mixed ...$args)alias for
Piper::to(callable $fn, mixed ...$args)(see above) -
pipe(callable $fn, mixed ...$args)alias for
Piper::to(callable $fn, mixed ...$args)(see above) -
with(callable $fn, mixed ...$args)alias for
Piper::to(callable $fn, mixed ...$args)(see above)
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-25
