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

Параметры:
  • $collectionarray, Iterator
withKeys()

Enables to save keys for the next operation

Результат:php\util\Flow
onlyKeys($keys, $ignoreCase = false)
Параметры:
  • $keysarray, Traversable
  • $ignoreCasebool
Результат:

php\util\Flow

append($collection)

Appends a new collection to the current flow, do not remember that you can pass a flow to this method

Параметры:
  • $collectionarray, Iterator
Результат:

php\util\Flow

find($filter = null)

Finds elements by using the $filter callback, elements - for each iteration that returns true

Параметры:
  • $filtercallable
Результат:

php\util\Flow

findOne($filter = null)

Finds the first element by using the $filter callback, when $filter will return the first true

Параметры:
  • $filtercallable
Результат:

mixed

findValue($value, $strict = false)
Параметры:
  • $value
  • $strictbool
Результат:

int, null, string return null if not found, else - key of value

group($callback)
Параметры:
  • $callbackcallable
Результат:

php\util\Flow

each($callback)

Iterates elements. It will break if $callback returns false strongly

Параметры:
  • $callbackcallable - ($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 false strongly

Параметры:
  • $sliceSizeint
  • $callbackcallable - (array $items): bool
  • $withKeysbool
Результат:

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

Параметры:
  • $callbackcallable - ($el[, $key])
Результат:

php\util\Flow

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

Параметры:
  • $nint - skip count
Результат:

php\util\Flow

limit($count)

Limits collection with $count

Параметры:
  • $countint - count of limit
Результат:

php\util\Flow

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 });

Параметры:
  • $callbackcallable - ($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

Параметры:
  • $comparatorcallable - ($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

Параметры:
  • $comparatorcallable - ($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

Параметры:
  • $separatorstring
Результат:

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

Параметры:
  • $collectionarray, Traversable
Результат:

php\util\Flow

static ofRange($from, $to, $step = 1)

Creates a new flow for a number range

Параметры:
  • $fromint
  • $toint
  • $stepint
Результат:

php\util\Flow

static ofString($string, $chunkSize = 1)

Creates a new flow for the string

Параметры:
  • $stringstring
  • $chunkSizeint - how many characters to combine for one item ?
Результат:

php\util\Flow

static ofStream($stream, $chunkSize = 1)

Creates a new flow for the Stream object

Параметры:
  • $streamphp\io\Stream - stream object
  • $chunkSizeint - size for Stream.read($size) method
Результат:

php\util\Flow