Download the PHP package sanmai/pipeline without Composer

On this page you can find all versions of the php package sanmai/pipeline. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package pipeline

Total Downloads Latest Stable Version CI Coverage Status Type Coverage Documentation

sanmai/pipeline provides a fluent, memory-efficient way to process iterable data in PHP. It uses lazy evaluation with generators, allowing you to build complex data processing pipelines that are easy to read and write.

Inspired by the pipe operator (|) in shells, it lets you chain operations like map, filter, reduce, zip, and more. Because it processes items one by one and only when needed, it's highly effective for large or even infinite collections without exhausting memory.

The library is rigorously tested and robust. Pipeline neither defines nor throws any exceptions.

Install

The latest version requires PHP 8.2 or above.

Some earlier versions work under PHP 5.6 and above, but they are not as feature-complete.

Documentation

Read the full documentation

The documentation includes:

Use

API entry points

All entry points always return an instance of the pipeline.

Method Details Use with
map() Takes an optional initial callback, where it must not require any arguments. Other than that, works just like an instance method below. use function Pipeline\map;
take() Takes any iterables, including arrays, joining them together in succession. use function Pipeline\take;
fromArray() Takes an array, initializes a pipeline with it. use function Pipeline\fromArray;
fromValues() Takes variadic arguments, initializes a pipeline with them. use function Pipeline\fromValues;
zip() Takes an iterable, and several more, transposing them together. use function Pipeline\zip;

Instance methods in a nutshell

Method Details A.K.A.
map() Takes an optional callback that for each input value may return one or yield many. Also takes an initial generator, where it must not require any arguments. Provided no callback does nothing. Also available as a plain function. SelectMany
cast() Takes a callback that for each input value expected to return another single value. Unlike map(), it assumes no special treatment for generators. Provided no callback does nothing. array_map, Select
append() Appends the contents of an iterable to the end of the pipeline. array_merge
push() Appends the arguments to the end of the pipeline. array_push
prepend() Appends the contents of an iterable to the end of the pipeline. array_merge
unshift() Prepends the pipeline with a list of values. array_unshift
zip() Takes a number of iterables, transposing them together with the current sequence, if any. array_map(null, ...$array), Python's zip(), transposition
reservoir() Reservoir sampling with an optional weighting function.
flatten() Flattens inputs: [[1, 2], [3, 4]] becomes [1, 2, 3, 4]. flat_map, flatten, collect_concat
unpack() Unpacks arrays into arguments for a callback. Flattens inputs if no callback provided.
chunk() Chunks the pipeline into arrays of specified length. array_chunk
chunkBy() Chunks the pipeline into arrays with variable sizes. Size 0 produces empty arrays.
select() Selects elements for which the callback returns true. By default only removes null and false. Optional onReject for side effects. array_filter, filter, Where
filter() Alias for select() with strict: false default. Removes all falsy values like array_filter. array_filter
tap() Performs side effects on each element without changing the values in the pipeline.
skipWhile() Skips elements while the predicate returns true, and keeps everything after the predicate return false just once.
slice() Extracts a slice from the inputs. Keys are not discarded intentionally. Supports negative values for both arguments. array_slice
peek() Returns the first N items as a pipeline/iterable. Use prepend() to restore items if needed. array_splice
fold() Reduces input values to a single value. Defaults to summation. Requires an initial value. array_reduce, Aggregate, Sum
reduce() Alias to fold() with a reversed order of arguments. array_reduce
values() Keep values only. array_values
keys() Keep keys only. array_keys
flip() Swaps keys and values. array_flip
tuples() Converts stream to [key, value] tuples.
max() Finds the highest value. max
min() Finds the lowest value. min
count() Counts values. Eagerly executed. array_count
first() Returns the first element. array_first
last() Returns the last element. array_last
each() Eagerly iterates over the sequence. foreach, array_walk
stream() Ensures subsequent operations use lazy, non-array paths
runningCount() Counts seen values using a reference argument.
toList() Returns an array with all values. Eagerly executed.
toAssoc() Returns a final array with values and keys. Eagerly executed. dict, ToDictionary
cursor() Returns a forward-only iterator that maintains position across iterations.
runningVariance() Computes online statistics: sample mean, sample variance, standard deviation. Welford's method
finalVariance() Computes final statistics for the sequence.
__construct() Can be provided with an optional initial iterator. Used in the take() function from above.

Pipeline implements IteratorAggregate and can be used as any other iterable.

Pipeline can be used as an argument to count(). Implements Countable. Be warned that operation of counting values is a terminal operation.

In general, Pipeline instances are mutable, meaning every Pipeline-returning method returns the very same Pipeline instance. This gives us great flexibility on trusting someone or something to add processing stages to a Pipeline instance, while also avoiding non-obvious mistakes, raised from a need to strictly follow a fluid interface. E.g. if you add a processing stage, it stays there no matter if you capture the return value or not. This peculiarity could have been a thread-safety hazard in other circumstances, but under PHP this is not an issue.

Caveats

Classes and interfaces: overview

This library is built to last. There's not a single place where an exception is thrown. Never mind any asserts whatsoever.

Methods

__construct()

Takes an instance of Traversable or none. In the latter case the pipeline must be primed by passing an initial generator to the map method.

$pipeline->map()

Takes a processing stage in a form of a generator function or a plain mapping function. Provided no callback does nothing.

Can also take an initial generator, where it must not require any arguments.

$pipeline->flatten()

Flatten inputs:

$pipeline->unpack()

An extra variant of map which unpacks arrays into arguments for a callback.

Where with map() you would use:

With unpack() these things are done behind the scene for you:

You can have all kinds of standard type checks with ease too.

With no callback, the default callback for unpack() will flatten inputs as in flatten().

$pipeline->cast()

Works similarly to map, but does not have a special treatment for generators. Think of array_map.

For this example, where map() would have filled the pipeline with a series of payments, cast() will add a generator for each customer.

$pipeline->zip()

Sequence-joins several iterables together, forming a feed with elements side by side:

With iterators with unequal number of elements, missing elements are left as nulls.

$pipeline->select()

Selects elements for which the callback returns true. filter() is an alias with strict: false default.

By default, select() only removes null and false values.

With the optional strict: false parameter, it removes all falsy values like array_filter:

The optional onReject callback allows side effects (like logging) for rejected items. It receives ($value, $key) like tap():

$pipeline->slice()

Takes offset and length arguments, functioning in a very similar fashion to how array_slice does with $preserve_keys set to true.

This example will remove first and last elements of the sequence.

Implementation uses a rolling window buffer for negative values of offset and length, and falls back on plain old array_slice for input arrays.

$pipeline->reduce()

Takes a reducing callback not unlike that of array_reduce with two arguments for the value of the previous iteration and for the current item. As a second argument it can take an initial value.

The pipeline has a default callback that sums all values.

$pipeline->toList()

Returns an array with all values from a pipeline. All array keys are ignored to make sure every single value is returned.

If in the example about one would use iterator_to_array($result) they would get just [3, 4].

$pipeline->tap()

Performs side effects on each element without changing the values in the pipeline. Useful for debugging, logging, or other side effects.

The tap() method executes the callback for each element as it flows through the pipeline, but the original values continue unchanged to the next stage.

$pipeline->each()

Eagerly iterates over the sequence using the provided callback.

Discards the sequence after iteration unless instructed otherwise by the second argument.

$pipeline->getIterator()

A method to conform to the Traversable interface. In case of unprimed \Pipeline\Standard it'll return an empty array iterator, essentially a no-op pipeline. Therefore this should work without errors:

This allows to skip type checks for return values if one has no results to return: instead of false or null it is safe to return an unprimed pipeline.

$pipeline->cursor()

Returns a forward-only iterator that maintains position across iterations. A cursor allows breaking out of a loop and continuing later:

$pipeline->runningVariance()

Computes online statistics for the sequence: counts, sample mean, sample variance, standard deviation. You can access these numbers on the fly with methods such as getCount(), getMean(), getVariance(), getStandardDeviation().

This method also accepts an optional cast callback that should return float|null: null values are discarded. Therefore you can have several running variances computing numbers for different parts of the data.

As you process orders, you will be able to access $varianceForShippedOrders->getMean() and $varianceForPaidOrders->getMean().

This computation uses Welford's online algorithm, therefore it can handle very large numbers of data points.

$pipeline->finalVariance()

A convenience method to computes the final statistics for the sequence. Accepts an optional cast method, else assumes the sequence contains valid numbers.

Type Safety

The library provides extensive generic type tracking, whether you prefer method chaining or not.

Given the following type:

A static analyzer will correctly infer that these examples are sound:

But if you were to change the signature of the class:

PHPStan will correctly note that:

Contributions

Contributions to documentation and test cases are welcome. Bug reports are welcome too.

API is expected to stay as simple as it is, though.

About collection pipelines in general

About collection pipelines programming pattern by Martin Fowler.

In a more general sense this library implements a subset of CSP paradigm, as opposed to Actor model.

What else is out there:

Submit a PR to add yours.

More badges

License FOSSA Status


All versions of pipeline with dependencies

PHP Build Version
Package Version
Requires php Version >=8.2
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package sanmai/pipeline contains the following files

Loading the files please wait ...