几天内即可获得 Laravel 代码审查方面的专家指导

Piper: Laravel-Style Array and String Helpers for PHP's Pipe Operator

最后更新于 经过

Piper: Laravel-Style Array and String Helpers for PHP's Pipe Operator image

PHP 8.5 introduced the pipe operator|> ), which passes the value on its left as the argument to the callable on its right. It works with any single-argument callable, but PHP's built-in functions and Laravel's chainable classes weren't designed around it. Piper fills that gap: it ports Laravel's collection and string methods into standalone, curried functions made to sit on the right side of a pipe.

Working on plain values

拉拉维尔的 CollectionStringable are method-chainable because you're working with a wrapper object: you collect() going in and call ->all() 或者 ->toString() coming out. Piper takes a different route suited to the pipe operator — its functions operate directly on native arrays and strings:

使用 功能 Spatie\Piper\Arr\ { 筛选 , 地图 , , 加入 };
$结果 = [ 1 , 2 , 3 , 4 , 5 , 6 ]
|> 筛选 fn 整数 $i) => $i 2 === 0
|> 地图 fn 整数 $i) => 战俘 ($i, 2 ))
|> ()
|> 加入 ',' , ', and ' (英文):
// "4, 16, and 36"

There's no wrapper instance in that pipeline. Each function takes an array, returns an array (or in the case of join() , a string), and the pipe operator threads the value through. It's a style that fits the |> syntax, and a natural companion to Collection when you're already working with native values.

Functions are curried for the pipe

Each Piper helper is written so that calling it with its configuration arguments returns a closure waiting for the value being piped. filter(fn (int $i) => $i % 2 === 0) doesn't filter anything on its own — it returns a callable that filters whatever array arrives via |> . That's what lets replace('world', 'Piper') read naturally inside a chain:

使用 功能 Spatie\Piper\Str\ { 降低 , 代替 };
$问候 = '你好世界!'
|> 降低 ()
|> 代替 'world' , 'Piper' (英文):
// "hello, Piper"

Mixing array and string steps

Because the functions pass native values, an array step and a string step belong to the same pipeline. A list can be reduced to a string partway through and keep going:

使用 功能 Spatie\Piper\Arr\ { 地图 , 加入 };
使用 功能 Spatie\Piper\Str\ { , 前缀 };
$headline = [ 'laravel' , 'news' ]
|> 地图 fn 细绳 $word) => 优思 ($word))
|> 加入 ' '
|> ()
|> 前缀 '>> ' (英文):
// ">> LARAVEL NEWS"

Arr namespace covers helpers like filter , map , values , 和 join ; the Str namespace covers lower , upper , replace , prefix , suffix , and others. The method names and behavior mirror Laravel's, so the API is already familiar if you've used Illuminate\Support

安装

Piper requires PHP 8.5 for the pipe operator. Install it with Composer:

作曲家 要求 spatie/piper

Then import the functions you need from the Spatie\Piper\ArrSpatie\Piper\Str namespaces.

Full documentation is available on the Spatie docs site , and the source is on GitHub

保罗·雷德蒙德照片

Laravel News 特约撰稿人。全栈 Web 开发人员兼作家。

归档于:
立方体

Laravel 时事通讯

加入超过 4 万名开发者的行列,不错过任何新的技巧、教程等内容。

图像
SerpApi

适用于您的 LLM 和 AI 应用的 Web 搜索 API

访问 SerpApi
SerpApi logo

SerpApi

Access real-time search engine results through a simple API—no more scraping headaches! Use it for AI applications, SEO tools, product research, travel information, and more

SerpApi
PhpStorm 标志

PhpStorm

首选的 PHP IDE,对 Laravel 及其生态系统提供广泛的开箱即用支持。

PhpStorm
鱼叉:新一代时间跟踪和发票标志

Harpoon:新一代时间跟踪和发票系统

新一代时间跟踪和计费软件,帮助您的机构规划和预测盈利的未来。

Harpoon:新一代时间跟踪和发票系统
Kirschbaum 标志

樱桃树

提供创新和稳定性,确保您的Web应用程序取得成功。

樱桃树
Tinkerwell 徽标

廷克威尔

Laravel 开发者必备的代码运行器。可在本地和生产环境中体验 AI、自动补全和即时反馈功能。

廷克威尔
Lucky Media 标志

幸运传媒

Get Lucky Now——拥有十余年经验的 Laravel 开发理想之选!

幸运传媒
了解 Softtech 的标志

了解软科技

Acquaint Softtech 提供 AI 就绪的 Laravel 开发人员,48 小时内即可上手,每月费用为 3000 美元,没有冗长的销售流程,并提供 100% 退款保证。

了解软科技
Laravel Cloud 标志

Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

Laravel 云
Shift 标志

转移

还在运行旧版本的 Laravel?立即实现 Laravel 自动升级和代码现代化,让您的应用程序保持最新状态。

转移
几天内即可获得 Laravel 代码审查徽标的专家指导

几天内即可获得 Laravel 代码审查方面的专家指导

专家级代码审查!两位拥有 10 年以上 Laravel 开发经验的开发者将为您提供清晰、实用的反馈,帮助团队构建更优质的应用程序。

几天内即可获得 Laravel 代码审查方面的专家指导
SaaSykit:Laravel SaaS 入门套件徽标

SaaSykit:Laravel SaaS 入门套件

SaaSykit 是一个多租户 Laravel SaaS 入门套件,包含运行现代 SaaS 所需的所有功能,例如支付、美观的结账界面、管理面板、用户仪表盘、身份验证、现成组件、统计数据、博客、文档等等。

SaaSykit:Laravel SaaS 入门套件
Simple Feature Flags for Laravel with Laravel Toggle image

Simple Feature Flags for Laravel with Laravel Toggle

阅读文章
Manage Laravel Cloud Deployments Inside PhpStorm image

Manage Laravel Cloud Deployments Inside PhpStorm

阅读文章
Storage Cache Store in Laravel 13.10.0 image

Storage Cache Store in Laravel 13.10.0

阅读文章
Laravel MongoDB Full-Text Search tutorial: The Art of the Relevancy image

Laravel MongoDB Full-Text Search tutorial: The Art of the Relevancy

阅读文章
Drag-and-Drop Sorting for Eloquent Models with Reorderable for Laravel image

Drag-and-Drop Sorting for Eloquent Models with Reorderable for Laravel

阅读文章
Ship AI with Laravel: Real-Time Streaming Chat UI with Livewire image

Ship AI with Laravel: Real-Time Streaming Chat UI with Livewire

阅读文章