sanmai/pipeline
sanmai/pipeline is a lightweight PHP pipeline library to process data through a chain of stages. Compose reusable, testable transformations with clear input/output flow, and plug in custom middleware-like steps for flexible processing in any app.
This document outlines the version history and key changes for the Pipeline library.
tap(), chunkBy(), and cursor() for more expressive pipelines.tap(callable $callback) (7.2): Execute side effects without modifying values. Useful for logging or debugging.chunkBy(callable $callback) (7.3): Create chunks with variable sizes based on a callback.cursor() (7.6): Returns a forward-only iterator that maintains position across loop breaks.toArray() now requires a parameter: Use toList() for a simple array or toAssoc() for an associative array.toArrayPreservingKeys() removed: Use toAssoc() instead.filter() method now includes a strict mode for more precise control over data cleaning.first(), last(), toList(), and toAssoc().cast() method uses array_map() for arrays, improving performance.toArray(): Deprecated, use toList() or toAssoc() instead.toArrayPreservingKeys(): Deprecated, use toAssoc() instead.Array Conversion (Breaking Change)
The toArray() method now requires a boolean parameter. Replace all previous usages as follows:
// Before: toArray() or toArray(false) - get indexed array
$result = take([1, 2, 3])->toArray();
$result = take([1, 2, 3])->toArray(false);
// After: use toList()
$result = take([1, 2, 3])->toList();
// Before: toArray(true) - get associative array with preserved keys
$result = take($data)->toArray(true);
// After: use toAssoc()
$result = take($data)->toAssoc();
toArrayPreservingKeys() Removed
// Before (v6.x)
$result = take($data)->toArrayPreservingKeys();
// After (v7.x)
$result = take($data)->toAssoc();
New Features to Consider
After migrating, consider using these new methods for cleaner code:
// tap() for side effects without modifying values
take($users)
->tap(fn($user) => logger()->info("Processing: {$user->name}"))
->map(fn($user) => $user->email)
->toList();
// chunkBy() for variable-size chunks
take($records)
->chunkBy(fn($record) => $record->category)
->each(fn($chunk) => processCategory($chunk));
// cursor() for pauseable iteration
$cursor = take($largeDataset)->cursor();
foreach ($cursor as $item) {
process($item);
if (needsPause()) {
break; // Can continue later from same position
}
}
// Continue iteration later...
foreach ($cursor as $item) {
process($item);
}
Array Conversion
->toArray() or ->toArray(false) with ->toList().->toArray(true) or ->toArrayPreservingKeys() with ->toAssoc().Filtering
To maintain the old filtering behavior (which removes all falsy values), no changes are needed. To use the new, safer strict filtering, add the strict: true parameter:
// Old behavior (removes all falsy values)
$result = take([0, '', false, null])->filter()->toList(); // []
// New strict mode (removes only null and false)
$result = take([0, '', false, null])->filter(strict: true)->toList(); // [0, '']
The library follows semantic versioning. Future development will focus on:
For the latest updates, please refer to the GitHub repository.
How can I help you explore Laravel packages today?