PHP Class phpstreams\Stream

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

Protected Properties

Свойство Type Description
$source Backing source for this Stream.

Méthodes publiques

Méthode 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 méthode

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 méthode

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
Résultat boolean

any() public méthode

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

collect() public méthode

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

count() public méthode

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
Résultat integer The number of elements in this Stream.

distinct() public méthode

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.
Résultat Stream

filter() public méthode

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
Résultat Stream a new stream yielding only the elements for which the callback returns true.

flatMap() public méthode

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.
Résultat Stream

getIterator() public méthode

public getIterator ( )

isSorted() public méthode

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
Résultat boolean

isValidSource() public static méthode

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

limit() public méthode

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.
Résultat Stream

map() public méthode

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

reduce() public méthode

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.
Résultat mixed

skip() public méthode

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

sorted() public méthode

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.
Résultat Stream

toArray() public méthode

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.
Résultat array An (associative) array of the contents of the stream.

Property Details

$source protected_oe property

Backing source for this Stream.
protected $source