Properties

$gen

$gen : \Generator

Type

\Generator — The generator that provides the stream's values

$associative

$associative : boolean

Type

boolean — Whether or not the stream is associative

$methods

$methods : array<mixed,\Closure>

Type

array<mixed,\Closure> — The registered methods

$factories

$factories : array<mixed,\Closure>

Type

array<mixed,\Closure> — The registered stream factories

Methods

__call()

__call(string  $name, array  $args) : mixed

Call a registered method

Parameters

string $name

The name of the method

array $args

The arguments for the method

Throws

\LazyCollection\Exceptions\InvalidMethodException

Returns

mixed

__callStatic()

__callStatic(string  $name, array  $args) : mixed

Call a registered factory

Parameters

string $name

The name of the factory method

array $args

The arguments for the factory

Throws

\LazyCollection\Exceptions\InvalidFactoryException

Returns

mixed

registerMethod()

registerMethod(string  $name, callable  $method) 

Register an extension method

Parameters

string $name

The name of the method

callable $method

The function to execute

Throws

\LazyCollection\Exceptions\InvalidMethodException

registerFactory()

registerFactory(string  $name, callable  $factory) 

Register a stream factory

Parameters

string $name

The name of the static method

callable $factory

The function to execute

Throws

\LazyCollection\Exceptions\InvalidFactoryException

getIterator()

getIterator() 

fromIterable()

fromIterable(\iterable  $it, boolean  $isAssoc = null) : \LazyCollection\Stream

Make a stream from an iterable

Parameters

\iterable $it
boolean $isAssoc

Returns

\LazyCollection\Stream

range()

range(integer  $start, integer  $end = null, integer  $step = 1) : \LazyCollection\Stream

Make a stream from a range of integers

Parameters

integer $start
integer $end
integer $step

Returns

\LazyCollection\Stream

splitBy()

splitBy(string  $str, string  $separator = "", boolean  $removeEmptyStrings = true) : \LazyCollection\Stream

Make a stream by splitting a string in parts

Parameters

string $str
string $separator
boolean $removeEmptyStrings

Returns

\LazyCollection\Stream

splitByRegex()

splitByRegex(string  $str, string  $re, boolean  $removeEmptyStrings = true) : \LazyCollection\Stream

Make a stream by splitting a string in parts using a regular expression

Parameters

string $str
string $re
boolean $removeEmptyStrings

Returns

\LazyCollection\Stream

map()

map(callable  $mapper) : \LazyCollection\Stream

Transform each value using the mapper function

Parameters

callable $mapper

Returns

\LazyCollection\Stream

peek()

peek(callable  $cb) : \LazyCollection\Stream

Execute a callback on each value

Parameters

callable $cb

Returns

\LazyCollection\Stream

flatten()

flatten() : \LazyCollection\Stream

Flattens a stream of iterable to a stream of data (noop for associative)

Returns

\LazyCollection\Stream

flatMap()

flatMap(callable  $mapper) : \LazyCollection\Stream

Maps a stream to a stream of iterable then flattens its (associative will only map)

Parameters

callable $mapper

Returns

\LazyCollection\Stream

mapFlattened()

mapFlattened(callable  $mapper) : \LazyCollection\Stream

Flattens the stream then maps it (associative will only map)

Parameters

callable $mapper

Returns

\LazyCollection\Stream

reverse()

reverse() : \LazyCollection\Stream

Reverses the order of the stream (eager, works only for finite streams)

Returns

\LazyCollection\Stream

filter()

filter(callable  $predicate) : \LazyCollection\Stream

Filters only elements satisfying the given predicate

Parameters

callable $predicate

Returns

\LazyCollection\Stream

filterNot()

filterNot(callable  $predicate) : \LazyCollection\Stream

Filters only elements that do not satisfy the given predicate

Parameters

callable $predicate

Returns

\LazyCollection\Stream

filterOut()

filterOut(callable  $predicate) : \LazyCollection\Stream

Alias for filterNot

Parameters

callable $predicate

Returns

\LazyCollection\Stream

notNull()

notNull() : \LazyCollection\Stream

Filters only elements non null

Returns

\LazyCollection\Stream

falsy()

falsy() : \LazyCollection\Stream

Filters only elements that are considered falsy

Returns

\LazyCollection\Stream

truthy()

truthy() : \LazyCollection\Stream

Filters only elements that are considered truthy (i.e. not falsy)

Returns

\LazyCollection\Stream

instanceOf()

instanceOf(string  $className) : \LazyCollection\Stream

Keep only the instances of the given class

Parameters

string $className

Returns

\LazyCollection\Stream

notInstanceOf()

notInstanceOf(string  $className) : \LazyCollection\Stream

Keep only the items that are not instances of the given class

Parameters

string $className

Returns

\LazyCollection\Stream

then()

then(\iterable  $it) : \LazyCollection\Stream

Streams the elements of the stream then the elements of the iterable

Parameters

\iterable $it

Returns

\LazyCollection\Stream

zipWith()

zipWith(\Stream  $rhs) : \LazyCollection\Stream

Transform a pair of stream into a stream of pairs (of shortest length)

Parameters

\Stream $rhs

Returns

\LazyCollection\Stream

takeWhile()

takeWhile(callable  $predicate) : \LazyCollection\Stream

Takes elements while the predicate is satisfied

Parameters

callable $predicate

Returns

\LazyCollection\Stream

takeUntil()

takeUntil(callable  $predicate) : \LazyCollection\Stream

Takes elements until the predicate is satisfied

Parameters

callable $predicate

Returns

\LazyCollection\Stream

take()

take(integer  $maxAmount) : \LazyCollection\Stream

Takes at most the first $maxAmount elements

Parameters

integer $maxAmount

Returns

\LazyCollection\Stream

skipWhile()

skipWhile(callable  $predicate) : \LazyCollection\Stream

Skips elements while the predicate is satisfied

Parameters

callable $predicate

Returns

\LazyCollection\Stream

skipUntil()

skipUntil(callable  $predicate) : \LazyCollection\Stream

Skips elements until the predicate is satisfied

Parameters

callable $predicate

Returns

\LazyCollection\Stream

skip()

skip(integer  $maxAmount) : \LazyCollection\Stream

Skips at most the first $maxAmount elements

Parameters

integer $maxAmount

Returns

\LazyCollection\Stream

subStream()

subStream(integer  $startIndex, integer  $endIndex) : \LazyCollection\Stream

Skips $startIndex elements and takes $endIndex-$startIndex elements

Parameters

integer $startIndex
integer $endIndex

Returns

\LazyCollection\Stream

uniqueBy()

uniqueBy(callable  $idExtractor) : \LazyCollection\Stream

Remove elements/duplicates that have the same id by the $idExtractor

Parameters

callable $idExtractor

Returns

\LazyCollection\Stream

unique()

unique() : \LazyCollection\Stream

Remove duplicates

Returns

\LazyCollection\Stream

sortBy()

sortBy(callable  $idExtractor) : \LazyCollection\Stream

Sorts the elements by comparing the extracted IDs (ascending order)

Parameters

callable $idExtractor

Returns

\LazyCollection\Stream

sort()

sort() : \LazyCollection\Stream

Sorts the elements in ascending order

Returns

\LazyCollection\Stream

sortByDescending()

sortByDescending(callable  $idExtractor) : \LazyCollection\Stream

Sorts the elements by comparing the extracted IDs (descending order)

Parameters

callable $idExtractor

Returns

\LazyCollection\Stream

sortDescending()

sortDescending() : \LazyCollection\Stream

Sorts the elements in descending order

Returns

\LazyCollection\Stream

sortWith()

sortWith(callable  $comparator) : \LazyCollection\Stream

Sorts the elements using the given comparator

Parameters

callable $comparator

Returns

\LazyCollection\Stream

chunks()

chunks(integer  $size) : \LazyCollection\Stream

Split the stream in a stream of chunks (which size is at most $size)

Parameters

integer $size

Returns

\LazyCollection\Stream

forEach()

forEach(callable  $cb) : void

Execute a callback on each element

Parameters

callable $cb

reduce()

reduce(callable  $reducer, mixed  $init = null) : mixed|null

Reduces the stream to a single value via the reducer (if no init is provided then it will be the first element)

Parameters

callable $reducer
mixed $init

Returns

mixed|null

count()

count(callable  $predicate = [Helpers::class, mixed  $"yes"]) : integer

Counts the amount of elements that satisfy the predicate (if no predicate is provided, will count the amount of elements)

Parameters

callable $predicate
mixed $"yes"]

Returns

integer

toArray()

toArray() : array

Consume the stream and put the values in an array

Returns

array

toJSON()

toJSON() : string

Consume the stream and transforms it into JSON

Returns

string

associateBy()

associateBy(callable  $valueFactory = [Helpers::class, mixed  $"identity"], callable  $keyFactory = [Helpers::class) : array

Create an associative array by generating key and value separately

Parameters

callable $valueFactory
mixed $"identity"]
callable $keyFactory

Returns

array

associate()

associate(callable  $factory) : array

Create an associative array by generating a [$key, $value] pair

Parameters

callable $factory

Returns

array

groupBy()

groupBy(callable  $keyExtractor) : array

Group an array of associative arrays into an associative array of arrays

Parameters

callable $keyExtractor

Returns

array

join()

join(array  $options) : string

Join to string using prefix, separator, suffix, stringifier, strlen and substr options

Parameters

array $options

Returns

string

partition()

partition(callable  $predicate) : array

Partitions the values based on the predicate (true is left, false is right)

Parameters

callable $predicate

Returns

array

sum()

sum(mixed  $init = null) : \LazyCollection\number|null

Computes the sum of all the elements

Parameters

mixed $init

Returns

\LazyCollection\number|null

sumBy()

sumBy(callable  $mapper, mixed  $init = null) : \LazyCollection\number|null

Computes the sum of all the mapped elements

Parameters

callable $mapper
mixed $init

Returns

\LazyCollection\number|null

average()

average() : \LazyCollection\number

Computes the average of all the elements

Returns

\LazyCollection\number

unzip()

unzip() : array

Unzips a stream of pair into a pair of arrays

Returns

array

all()

all(callable  $predicate) : boolean

Determine whether or not all the elements satisfy the predicate

Parameters

callable $predicate

Returns

boolean

none()

none(callable  $predicate) : boolean

Determine whether or not none of the elements satisfy the predicate

Parameters

callable $predicate

Returns

boolean

any()

any(callable  $predicate) : boolean

Determine whether or any of the elements satisfy the predicate

Parameters

callable $predicate

Returns

boolean

notAll()

notAll(callable  $predicate) : boolean

Determine whether or any of the elements does not satisfy the predicate

Parameters

callable $predicate

Returns

boolean

first()

first(callable  $predicate = [Helpers::class, mixed  $"yes"]) : mixed

Get the first element that match the predicate (or the very first if no predicate is provided) or throw if none

Parameters

callable $predicate
mixed $"yes"]

Returns

mixed

firstOr()

firstOr(mixed  $default, callable  $predicate = [Helpers::class, mixed  $"yes"]) : mixed

Get the first element that match the predicate or the default element if none

Parameters

mixed $default
callable $predicate
mixed $"yes"]

Returns

mixed

firstOrNull()

firstOrNull(callable  $predicate = [Helpers::class, mixed  $"yes"]) : mixed|null

Get the first element that match the predicate or null if none

Parameters

callable $predicate
mixed $"yes"]

Returns

mixed|null

last()

last(callable  $predicate = [Helpers::class, mixed  $"yes"]) : mixed

Get the last element that match the predicate (or the very first if no predicate is provided) or throw if none

Parameters

callable $predicate
mixed $"yes"]

Returns

mixed

lastOr()

lastOr(mixed  $default, callable  $predicate = [Helpers::class, mixed  $"yes"]) : mixed

Get the last element that match the predicate or the default element if none

Parameters

mixed $default
callable $predicate
mixed $"yes"]

Returns

mixed

lastOrNull()

lastOrNull(callable  $predicate = [Helpers::class, mixed  $"yes"]) : mixed|null

Get the first element last match the predicate or null if none

Parameters

callable $predicate
mixed $"yes"]

Returns

mixed|null

atIndex()

atIndex(integer  $i) : mixed

Get the element at index $i or throw if none

Parameters

integer $i

Returns

mixed

atIndexOr()

atIndexOr(integer  $i, mixed  $default) : mixed

Get the $i element or the default if none

Parameters

integer $i
mixed $default

Returns

mixed

atIndexOrNull()

atIndexOrNull(integer  $i) : mixed|null

Get the $i element or null if none

Parameters

integer $i

Returns

mixed|null

indexOfFirst()

indexOfFirst(callable  $predicate) : integer

Get the index of the first element that satisfies the predicate, or -1

Parameters

callable $predicate

Returns

integer

indexOf()

indexOf(mixed  $needle) : integer

Get the first index of the $needle, or -1

Parameters

mixed $needle

Returns

integer

indexOfLast()

indexOfLast(callable  $predicate) : integer

Get the index of the last element that satisfies the predicate, or -1

Parameters

callable $predicate

Returns

integer

lastIndexOf()

lastIndexOf(mixed  $needle) : integer

Get the last index of the $needle, or -1

Parameters

mixed $needle

Returns

integer

maxBy()

maxBy(callable  $mapper) : mixed|null

Get the max value determined by comparing mapped values

Parameters

callable $mapper

Returns

mixed|null

max()

max() : mixed|null

Get the max value

Returns

mixed|null

maxWith()

maxWith(callable  $comparator) : mixed|null

Get the max value using a custom comparator

Parameters

callable $comparator

Returns

mixed|null

minBy()

minBy(callable  $mapper) : mixed|null

Get the min value determined by comparing mapped values

Parameters

callable $mapper

Returns

mixed|null

min()

min() : mixed|null

Get the min value

Returns

mixed|null

minWith()

minWith(callable  $comparator) : mixed|null

Get the min value using a custom comparator

Parameters

callable $comparator

Returns

mixed|null

single()

single(callable  $predicate = [Helpers::class, mixed  $"yes"]) : mixed

Get the single element that satisfies the predicate (if no predicate is given, the single element), or throws

Parameters

callable $predicate
mixed $"yes"]

Returns

mixed

singleOr()

singleOr(mixed  $default, callable  $predicate = [Helpers::class, mixed  $"yes"]) : mixed

Get the single element that satisfies the predicate (if no predicate is given, the single element), or get the default

Parameters

mixed $default
callable $predicate
mixed $"yes"]

Returns

mixed

singleOrNull()

singleOrNull(callable  $predicate = [Helpers::class, mixed  $"yes"]) : mixed

Get the single element that satisfies the predicate (if no predicate is given, the single element), or null

Parameters

callable $predicate
mixed $"yes"]

Returns

mixed

contains()

contains(mixed  $needle) : boolean

Determine whether or not the stream contains the $needle

Parameters

mixed $needle

Returns

boolean

__construct()

__construct(\Generator  $gen, boolean  $associative = false) 

Construct a stream from its generator

Parameters

\Generator $gen

The generator to get values from

boolean $associative

Whether or not the stream is associative

pipe()

pipe(callable  $generatorFactory, boolean  $associative = false) : self

Construct a consumer stream from this stream

Parameters

callable $generatorFactory

A function that accepts the current generator and returns a new generator

boolean $associative

Returns

self

hasMethod()

hasMethod(string  $name) : boolean

Determine whether or not a method is registered with the given name

Parameters

string $name

The method to lookup

Returns

boolean

hasFactory()

hasFactory(string  $name) : boolean

Determine whether or not a factory is registered with the given name

Parameters

string $name

The factory to lookup

Returns

boolean