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

v1.0.0 2025-11-20 22:45 UTC

This package is auto-updated.

Last update: 2025-11-20 23:28:14 UTC


README

PHP Version License Build Status

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.

  1. Fork the repository.
  2. Create a feature branch.
  3. Commit your changes.
  4. Push to the branch.
  5. Open a Pull Request.

๐Ÿ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.

Made with โค๏ธ by Ali Harb