Flow¶
-
php\util\Flow
implements: Iterator
A special class to work with arrays and iterators under flows. Flows are used for the lazy array/iterator operations, to save the RAM memory.
Class Flow, Immutable
Methods
__construct
($collection)¶Create a new flow, you can also use
of()
method
Parameters:
- $collection – array, Iterator
withKeys
()¶Enables to save keys for the next operation
Returns: php\util\Flow
onlyKeys
($keys, $ignoreCase = false)¶
Parameters:
- $keys – array, Traversable
- $ignoreCase – bool
Returns:
append
($collection)¶Appends a new collection to the current flow, do not remember that you can pass a flow to this method
Parameters:
- $collection – array, Iterator
Returns:
find
($filter = null)¶Finds elements by using the $filter callback, elements - for each iteration that returns
true
Parameters:
- $filter – callable
Returns:
findOne
($filter = null)¶Finds the first element by using the $filter callback, when $filter will return the first
true
Parameters:
- $filter – callable
Returns: mixed
group
($callback)¶
Parameters:
- $callback – callable
Returns:
each
($callback)¶Iterates elements. It will break if $callback returns
false
strongly
Parameters:
- $callback – callable - ($el[, $key]): bool
Returns: int - iteration count
eachSlice
($sliceSize, $callback, $withKeys = false)¶Iterates elements as slices (that are passing as arrays to $callback). It will break if $callback returns
false
strongly
Parameters:
- $sliceSize – int
- $callback – callable - (array $items): bool
- $withKeys – bool
Returns: int - slice iteration count
map
($callback)¶Iterates elements and returns a new flow of the result Example:
$newFlow = Flow::of([1,2,3])->map(function($el){ return $el * 10 });// the new flow will contain 10, 20 and 30
Parameters:
- $callback – callable - ($el[, $key])
Returns:
keys
()¶Create a new flow by using the keys of the current flow
Returns: php\util\Flow
skip
($n)¶Skips $n elements in the current collection
Parameters:
- $n – int - skip count
Returns:
limit
($count)¶Limits collection with $count
Parameters:
- $count – int - count of limit
Returns:
reduce
($callback)¶Iterates elements and gets a result of this operation It can be used for calculate some results, for example:
// calculates a sum of elements$sum = .. ->reduce(function($result, $el){ $result = $result + $el });
Parameters:
- $callback – callable - ($result, $el[, $key])
Returns: int
sort
($comparator = null)¶Sort the last result of the flow, also see:
php\\lib\\items::sort()
Note
use the
withKeys()
method to save keys
Parameters:
- $comparator – callable - ($o1, $o2) -> int, where -1 smaller, 0 equal, 1 greater
Returns: array
sortByKeys
($comparator = null)¶The same method as
sort()
only based on keys insteadof valuesNote
use the
withKeys()
method to save keys
Parameters:
- $comparator – callable - ($key1, $key2) -> int
Returns: array
toArray
()¶Convert elements to an array
Note
use the
withKeys()
method to save keys
Returns: array
toString
($separator)¶Join elements to a string similar to
implode()
in PHP
Parameters:
- $separator – string
Returns: string
count
()¶
Returns: int
current
()¶
Returns: mixed
next
()¶
Returns: void
key
()¶
Returns: mixed
valid
()¶
Returns: bool
rewind
()¶
Returns: void
__clone
()¶private
- static
of
($collection)¶Creates a new flow for an array of Iterator
Parameters:
- $collection – array, Traversable
Returns:
- static
ofRange
($from, $to, $step = 1)¶Creates a new flow for a number range
Parameters:
- $from – int
- $to – int
- $step – int
Returns:
- static
ofString
($string, $chunkSize = 1)¶Creates a new flow for the string
Parameters:
- $string – string
- $chunkSize – int - how many characters to combine for one item ?
Returns:
- static
ofStream
($stream, $chunkSize = 1)¶Creates a new flow for the Stream object
Parameters:
- $stream – php\io\Stream - stream object
- $chunkSize – int - size for
Stream.read($size)
methodReturns: