承接 datahihi1/pipefn 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

datahihi1/pipefn

Composer 安装命令:

composer require datahihi1/pipefn

包简介

Polyfill for PHP 8.5 pipe operator supporting closures, functions, and methods.

README 文档

README

A lightweight polyfill that provides a pipe() function for method chaining and functional pipelines in PHP 5.6+ applications. This function allows you to chain method calls, closures, functions, and more in a clean, readable way.

Installation

Via Composer

composer require datahihi1/pipefn:dev-main

Manual Installation

  1. Download the pipefn.php file
  2. Include it in your project:
    require_once 'pipefn.php';

Usage

The pipe() function takes an initial value and chains it through multiple steps (functions, closures, methods, callables, etc).

Basic Syntax

$result = pipe($value, ...$steps);

Parameters

  • $value - The initial value to be piped through the steps
  • ...$steps - Variable number of steps, which can be:
    • String function/method names (global, static, or object methods)
    • Arrays in format [callable, [arguments]] or [methodName, [arguments]]
    • Closures/functions
    • Callable arrays (e.g. [$object, 'method'])

Supported Usages

1. Chain closures/functions

pipe(" hi ", fn($v) => trim($v), fn($v) => strtoupper($v), fn($v) => $v . "!");
// Result: "HI!"

2. Global function names

pipe(" test ", 'trim', 'strtoupper');
// Result: "TEST"

3. Static method (Class::method)

pipe("hey", 'MyClass::upper');
pipe("a", ['MyClass::append', ['b']]);
// Result: "HEY", "ab"

4. Object method (with or without arguments)

$user = new class('john') {
    public $name;
    public function __construct($n){$this->name = $n;}
    public function setName($n){$this->name=$n; return $this;}
    public function upper(){ $this->name = strtoupper($this->name); return $this; }
    public function get(){return $this->name;}
};
pipe($user, ['setName', ['jane']], 'upper', 'get');
// Result: "JANE"

5. Callable array (e.g. [[$obj, 'method'], [args]])

pipe("x", [[$obj, 'method'], ['y']]);
// Calls $obj->method("x", "y")

6. Closure with multiple arguments

pipe("a", [fn($v, $x, $y) => $v . $x . $y, ['b', 'c']]);
// Result: "abc"

7. Function with callback (e.g. array_map)

pipe([1,2,3], ['array_map', [fn($x)=>$x*2]], 'array_sum');
// Result: 12

8. No steps (returns value unchanged)

pipe("hello"); // Result: "hello"

9. Error handling

If a step is not callable or invalid, an InvalidArgumentException is thrown with a clear message.

Requirements

  • PHP >= 5.6
  • No additional dependencies

Features

  • ✅ PHP 5.6+ compatibility
  • ✅ Method chaining with arguments
  • ✅ Closure/function/callable support
  • ✅ Callable array (e.g. [$obj, 'method'])
  • ✅ Lightweight (single file)
  • ✅ No external dependencies
  • ✅ Automatic function/method existence check
  • ✅ Clear error messages for debugging

License

This project is open source and available under the MIT License.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Give it a star if you find it useful! ⭐ :v

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-28