alizharb / php85-functional-pipeline
A functional programming toolkit for PHP 8.5 leveraging the native pipe operator.
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/alizharb/php85-functional-pipeline
Requires
- php: >=8.5
Requires (Dev)
- phpunit/phpunit: ^11.0
README
A modern, type-safe functional programming toolkit designed exclusively for PHP 8.5 and the native Pipe Operator (|>).
๐ Introduction
PHP 8.5 introduces the revolutionary Pipe Operator (|>), allowing for cleaner, more readable code by passing the result of one expression as the first argument to the next.
Functional Pipeline is the missing standard library for this new era. It provides a curated set of curried, pipe-ready primitives (map, filter, reduce, etc.) that make functional programming in PHP a joy.
Why use this package?
- Native Syntax: Built from the ground up for
$data |> func(). - Type Safety: Fully typed with PHPDoc templates (Generics) for excellent IDE support.
- Performance: Leverages Generators for lazy evaluation and memory efficiency.
- Zero Dependencies: Lightweight and focused.
๐ฆ Installation
composer require alizharb/php85-functional-pipeline
โ ๏ธ Requirement: This library requires PHP 8.5 or higher.
๐ง Usage
The "Pipe" Way
Forget nested function calls. Compose your logic linearly.
use function FunctionalPipeline\map; use function FunctionalPipeline\filter; use function FunctionalPipeline\collect; $users = [ ['id' => 1, 'active' => true, 'score' => 10], ['id' => 2, 'active' => false, 'score' => 50], ['id' => 3, 'active' => true, 'score' => 20], ]; $scores = $users |> filter(fn($u) => $u['active']) // Keep active users |> map(fn($u) => $u['score'] * 2) // Double their score |> collect(); // Convert Generator to Array // Result: [20, 40]
Advanced Processing
Use tap() to debug or perform side-effects without breaking the chain.
use function FunctionalPipeline\tap; use function FunctionalPipeline\reduce; $totalRevenue = $orders |> tap(fn($list) => Log::info('Processing ' . count($list) . ' orders')) |> filter(fn($order) => $order->isPaid()) |> map(fn($order) => $order->total) |> reduce(fn($sum, $amount) => $sum + $amount, 0.0);
๐ API Reference
map(callable $callback): Closure
Applies a callback to each element.
- Returns:
Generator(Lazy)
filter(callable $predicate): Closure
Filters elements where the predicate returns true.
- Returns:
Generator(Lazy)
reduce(callable $callback, mixed $initial): Closure
Iteratively reduces the iterable to a single value.
- Returns:
mixed(The result)
tap(callable $callback): Closure
Executes a callback with the current value and returns the value unchanged. Perfect for logging.
- Returns:
mixed(The original value)
compose(callable ...$functions): Closure
Combines multiple functions into a single callable.
- Returns:
Closure
collect(): Closure
Converts a Generator or iterable into a standard array.
- Returns:
array
๐งช Testing
Run the test suite with PHPUnit:
composer test
๐ค Contributing
Contributions are welcome! Please see CONTRIBUTING.md for details.
- Fork the repository.
- Create a feature branch.
- Commit your changes.
- Push to the branch.
- Open a Pull Request.
๐ License
This project is licensed under the MIT License. See the LICENSE file for details.
Made with โค๏ธ by Ali Harb