Flow¶
-
php\util\Flow implements:
IteratorA 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
Параметры:
- $collection –
array,Iterator
withKeys()¶Enables to save keys for the next operation
Результат: php\util\Flow
onlyKeys($keys, $ignoreCase = false)¶
Параметры:
- $keys –
array,Traversable- $ignoreCase –
boolРезультат:
append($collection)¶Appends a new collection to the current flow, do not remember that you can pass a flow to this method
Параметры:
- $collection –
array,IteratorРезультат:
find($filter = null)¶Finds elements by using the $filter callback, elements - for each iteration that returns
true
Параметры:
- $filter –
callableРезультат:
findOne($filter = null)¶Finds the first element by using the $filter callback, when $filter will return the first
true
Параметры:
- $filter –
callableРезультат:
mixed
findValue($value, $strict = false)¶
Параметры:
- $value –
- $strict –
boolРезультат:
int,null,stringreturn null if not found, else - key of value
group($callback)¶
Параметры:
- $callback –
callableРезультат:
each($callback)¶Iterates elements. It will break if $callback returns
falsestrongly
Параметры:
- $callback –
callable- ($el[, $key]): boolРезультат:
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
falsestrongly
Параметры:
- $sliceSize –
int- $callback –
callable- (array $items): bool- $withKeys –
boolРезультат:
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
Параметры:
- $callback –
callable- ($el[, $key])Результат:
keys()¶Create a new flow by using the keys of the current flow
Результат: php\util\Flow
skip($n)¶Skips $n elements in the current collection
Параметры:
- $n –
int- skip countРезультат:
limit($count)¶Limits collection with $count
Параметры:
- $count –
int- count of limitРезультат:
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 });
Параметры:
- $callback –
callable- ($result, $el[, $key])Результат:
int
sort($comparator = null)¶Sort the last result of the flow, also see:
php\\lib\\items::sort()Примечание
use the
withKeys()method to save keys
Параметры:
- $comparator –
callable- ($o1, $o2) -> int, where -1 smaller, 0 equal, 1 greaterРезультат:
array
sortByKeys($comparator = null)¶The same method as
sort()only based on keys insteadof valuesПримечание
use the
withKeys()method to save keys
Параметры:
- $comparator –
callable- ($key1, $key2) -> intРезультат:
array
toArray()¶Convert elements to an array
Примечание
use the
withKeys()method to save keys
Результат: array
toString($separator)¶Join elements to a string similar to
implode()in PHP
Параметры:
- $separator –
stringРезультат:
string
count()¶
Результат: int
current()¶
Результат: mixed
next()¶
Результат: void
key()¶
Результат: mixed
valid()¶
Результат: bool
rewind()¶
Результат: void
__clone()¶private
- static
ofEmpty¶
Результат: php\util\Flow
- static
of($collection)¶Creates a new flow for an array of Iterator
Параметры:
- $collection –
array,TraversableРезультат:
- static
ofRange($from, $to, $step = 1)¶Creates a new flow for a number range
Параметры:
- $from –
int- $to –
int- $step –
intРезультат:
- static
ofString($string, $chunkSize = 1)¶Creates a new flow for the string
Параметры:
- $string –
string- $chunkSize –
int- how many characters to combine for one item ?Результат:
- static
ofStream($stream, $chunkSize = 1)¶Creates a new flow for the Stream object
Параметры:
- $stream – php\io\Stream - stream object
- $chunkSize –
int- size forStream.read($size)methodРезультат: