Download the PHP package dbrans/stream-pipeline without Composer
On this page you can find all versions of the php package dbrans/stream-pipeline. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dbrans/stream-pipeline
More information about dbrans/stream-pipeline
Files in dbrans/stream-pipeline
Package stream-pipeline
Short Description A Stream based pipeline pattern implementation
License MIT
Rated 5.00 based on 1 reviews
Informations about the package stream-pipeline
Stream Pipeline
What is it?
A Stream based pipeline pattern implementation.
The Pipeline pattern uses ordered stages to process a sequence of input values. Each implemented task is represented by a stage of the pipeline. You can think of pipelines as similar to assembly lines in a factory, where each item in the assembly line is constructed in stages.
Features
-
Fluent API / Chaining
Chain methods likemap,filter,reducein a readable, functional style. -
Lazy evaluation
The pipeline does not execute any operations until a terminal method likecollect()orforEach()is called.
Each element is processed one at a time through the entire chain, minimizing memory usage and allowing infinite or very large data streams. -
Lightweight and simple
No dependencies and easy to integrate into any project. -
Common functional operations included
map,filter,reduce,flatMap,distinct,limit,skip... -
Generics support via docblocks
Compatible with Psalm/PHPStan for static type checking. -
Immutable style
Each operation returns a new pipeline instance, avoiding side effects. -
Works with any iterable
Supports arrays, generators, or any PHP iterable. - Explicit terminal operations
Methods likecollect()andforEach()allow controlled consumption of data.
How it works
Stream pipeline allows you to go from writing expressions like:
to writing functional expressions like:
Getting started
The library is available as a Composer package on Packagist.org.
To install in your project, just run:
After this runs successfully, you can include the main class in your application logic:
Usage
You can initialize an Stream to use it:
A Stream object exposes several methods to operate with its elements:
The Stream class is immutable, so each chaining method returns a new Stream.
The execution of a Stream is lazy, so the elements are iterated just one time only when a terminal operation (forEach, reduce, toArray, collect...) is called.
Pipe operations
Each method allows a callable argument:
The library exposes some common operations to better readability:
Please see the Javadoc for more information.
Stream Methods
Initialization static operations:
of(...$elements): StreamInterfacefromIterable(iterable $collection): StreamInterfaceiterate($initialValue, callable $stepOperation): StreamInterface
Pipe operations:
map(callable $operation): StreamInterfacefilter(callable $operation): StreamInterfacepeek(callable $operation): StreamInterfacetap(callable $operation): StreamInterface(alias ofpeek).limit(int $limit): StreamInterfaceskip(int $number): StreamInterfacedistinct(?callable $distinctBy = null): StreamInterfaceflatMap(?callable $operation = null): StreamInterfaceconcat(iterable $elements): StreamInterfacetakeWhile(callable $operation): StreamInterfacedropWhile(callable $operation): StreamInterface
Terminal operations:
findFirst()count(): intforEach(callable $callback): voidanyMatch(callable $condition): boolallMatch(callable $condition): boolnoneMatch(callable $condition): boolreduce(callable $operation, $initialValue)toArray(bool $preserveKeys = false): arraycollect(?callable $collector)
All callable functions receive: function ($currentElement, $index, $originalIndex) as arguments. Example:
Pre-defined Collectors
There are pre-defined collector functions with some common operations.
You can use them with the terminal operator collect():
Collectors::join(string $delimiter = '')Collectors::sum(?callable $mapper = null)Collectors::groupBy(?callable $classifier = null, ?callable $mapper = null)Collectors::groupAndReduceBy(?callable $keysMapper = null, ?callable $valuesMapper = null, ?callable $reducer = null)
For example:
Iterator classes
NumberGenerator: a number generator with an optional step.
Example:
Operation classes
Logical: logical operations (such as identity, true, false...).Numbers: numbers operations.Objects: generic objects operations.Strings: string utils and functions.Values: polimorphic values operations.