PHP Class phpstreams\Stream

Author: Bert Peters ([email protected])
Inheritance: implements IteratorAggregat\IteratorAggregate, implements Countabl\Countable
Show file Open project: bertptrs/phpstreams Class Usage Examples

Protected Properties

Property Type Description
$source Backing source for this Stream.

Public Methods

Method Description
__construct ( Traversabl\Traversable | array $source ) Construct a new stream from a traversable source.
all ( callable $predicate = null ) : boolean Return true if all element matches the predicate.
any ( callable $predicate = null ) : boolean Return true if any element matches the predicate.
collect ( phpstreams\Collector $collector )
count ( ) : integer Count the number of elements in this stream.
distinct ( type $strict = false ) : Stream Enforce distinct values.
filter ( callable $filter ) : Stream Apply a filter to the current stream.
flatMap ( callable $unpacker = null ) : Stream Flatten the underlying stream.
getIterator ( )
isSorted ( ) : boolean Check whether this stream is definitely sorted.
isValidSource ( mixed $source ) : boolean Check whether a source is valid.
limit ( integer $limit ) : Stream Limit the stream to some number of elements.
map ( callable $mapping ) : Stream Map the values in the stream to another stream.
reduce ( mixed $identity, callable $binaryOp ) : mixed Reduce the stream using the given binary operation.
skip ( integer $toSkip ) : Stream Skip the first few elements.
sorted ( callable $sort = null ) : Stream Get a sorted view of the stream.
toArray ( boolean $withKeys = true ) : array Convert this stream to an array.

Method Details

__construct() public method

Construct a new stream from a traversable source.
public __construct ( Traversabl\Traversable | array $source )
$source Traversabl\Traversable | array The source to create the stream from.

all() public method

This method short-circuits, so if any item does not match the predicate, no further elements are considered.
public all ( callable $predicate = null ) : boolean
$predicate callable
return boolean

any() public method

This method short-circuits, so if any item matches the predicate, no further items are evaluated.
public any ( callable $predicate = null ) : boolean
$predicate callable
return boolean

collect() public method

public collect ( phpstreams\Collector $collector )
$collector phpstreams\Collector

count() public method

This method may consume the stream, if it is not repeatable. Use it only when appropriate. As this is simply an implementation of the Countable interface, the count function may be used instead. However, this still may consume the stream.
public count ( ) : integer
return integer The number of elements in this Stream.

distinct() public method

This stream will yield every distinct value only once. Note that internally, it uses in_array for lookups. This is problematic for large numbers of distinct elements, as the complexity of this stream filter becomes O(n * m) with n the number of elements and m the number of distinct values. This mapping preserves key => value relationships, and will yield the values with the first keys encountered.
public distinct ( type $strict = false ) : Stream
$strict type whether to use strict comparisons for uniqueness.
return Stream

filter() public method

Note that filter, like most stream operations, preserves the key => value relationship throughout its execution. That means that there will be missing keys, if you started with a regular array.
public filter ( callable $filter ) : Stream
$filter callable
return Stream a new stream yielding only the elements for which the callback returns true.

flatMap() public method

This method takes each element and unpacks it into a sequence of elements. All individual sequences are concatenated in the resulting stream.
public flatMap ( callable $unpacker = null ) : Stream
$unpacker callable [optional] An unpacker function that can unpack elements into something iterable. Default is to use the identity function.
return Stream

getIterator() public method

public getIterator ( )

isSorted() public method

This is used to optimize some stream operations, such as distinct(), which only needs constant memory when operating on a sorted list. Any stream operations that potentially change the sorting order should override this method to properly reflect the actual sorting order.
public isSorted ( ) : boolean
return boolean

isValidSource() public static method

Valid sources are either an array or a Traversable.
public static isValidSource ( mixed $source ) : boolean
$source mixed
return boolean True if it is.

limit() public method

All elements after the limit are not considered or generated, so this can be used with infinite streams.
public limit ( integer $limit ) : Stream
$limit integer The maximum number of elements to return.
return Stream

map() public method

Map the values in the stream to another stream.
public map ( callable $mapping ) : Stream
$mapping callable
return Stream a new stream with the variables mapped.

reduce() public method

The given is applied to every item in the stream in no particular order. The result is then returned. In order for the callable to be a proper reductor, it should be:
  • Commutative, so op($a, $b) is equal to op($b, $a), and
  • Should preserve respect the given identity, i.e. op($a, $identity) = $identity.
If any of these properties do not hold, the output of this function is not defined.
public reduce ( mixed $identity, callable $binaryOp ) : mixed
$identity mixed The identity element.
$binaryOp callable A reduction function, respecting the properties above.
return mixed

skip() public method

This method discards the first few elements from the stream, maintaining key => value relations.
public skip ( integer $toSkip ) : Stream
$toSkip integer
return Stream

sorted() public method

This method yields its values sorted. Sort is done using the default asort() function, or uasort if a sorting function was given. Note that by the nature of sorting things, the entire previous source will be read into memory in order to sort it. This may be a performance issue.
public sorted ( callable $sort = null ) : Stream
$sort callable [optional] a callback to use for sorting.
return Stream

toArray() public method

Convert this stream to an array.
public toArray ( boolean $withKeys = true ) : array
$withKeys boolean [optional] if false, return a simple array containing all values in order. If true (the default) preserve keys.
return array An (associative) array of the contents of the stream.

Property Details

$source protected property

Backing source for this Stream.
protected $source