php-kit/flow

Iterator Nirvana for PHP

1.1.1 2017-06-26 18:26 UTC

This package is auto-updated.

Last update: 2024-11-11 15:37:18 UTC


README

Iterator Nirvana for PHP

Runtime requirements

  • PHP >= 5.4

Introduction

Flow provides a fluent interface to assemble chains of iterators and other data processing operations.

The fluent API makes it very easy and intuitive to use the native SPL iterators (together with some custom ones provided by this library) and the expressive syntax allows you to assemble sophisticated processing pipelines in an elegant, terse and readable fashion.

An iteration chain assembled with this builder can perform multiple transformations over a data flow without storing in memory the resulting data from intermediate steps. When operating over large data sets, this mechanism can be very light on memory consumption.

Inputs to the chain can be any kind of iterables. Iterables are things that can be converted to iterators. They can be native arrays, Traversables (classes implementing the native Iterator or IteratorAggregate intefaces), invoked generator functions (on PHP>=5.5) or even callables (ex. Closures).

Flow allows you to write generator functions using common functions on PHP<5.5, using the FunctionIterator class.

The Flow Builder

TODO: provide examples here.

Iterators

Flow also provides a library of useful iterators that complements and extends the basic set provided by the Standard PHP Library (SPL).

These are the iterators:

CachedIterator

Memoizes another iterator's values so that subsequent iterations will not need to iterate it again.

ConditionalIterator

Iterates another iterator until the iteration finishes or the given callback returns false (whichever occurs first).

FlipIterator

Iterates a given iterator swapping keys for values and/or vice-versa.

FunctionIterator

Iterates over values generated by repeatedly invoking a function or class method.

LoopIterator

Allows the looping of another iterator, with some constraints.

UnfoldIterator

Replaces and expands each iterated value of another iterator.

MapIterator

Transforms data from another iterator using a callback function.

RangeIterator

Iterates over a generated sequence of numbers.

RecursiveIterator

A generic recursive iterator that defines the recursion via a user-defined callback function.

ReduceIterator

Applies a function against an accumulator and each value from a given iterator to reduce the iterated data to a single value.

ReindexIterator

Iterates another iterator replacing keys by a generated sequence of numbers.

SingleValueIterator

Provides one single iteration of a constant value.

Notes

Some operations require the iteration data to be "materialized", i.e. fully iterated and stored internally as an array, before the operation is applied. This only happens for operations that require all data to be present (ex: reverse() or sort()), and the resulting data will be automatically converted back to an iterator whenever it makes sense.

License

This library is open-source software licensed under the MIT license.

Flow - Copyright © 2015 Impactwave, Lda.