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:
  • $collectionarray, Iterator
withKeys()

Enables to save keys for the next operation

Returns:php\util\Flow
onlyKeys($keys, $ignoreCase = false)
Parameters:
  • $keysarray, Traversable
  • $ignoreCasebool
Returns:

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

Parameters:
  • $collectionarray, Iterator
Returns:

php\util\Flow

find($filter = null)

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

Parameters:
  • $filtercallable
Returns:

php\util\Flow

findOne($filter = null)

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

Parameters:
  • $filtercallable
Returns:

mixed

findValue($value, $strict = false)
Parameters:
  • $value
  • $strictbool
Returns:

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

group($callback)
Parameters:
  • $callbackcallable
Returns:

php\util\Flow

each($callback)

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

Parameters:
  • $callbackcallable - ($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:
  • $sliceSizeint
  • $callbackcallable - (array $items): bool
  • $withKeysbool
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:
  • $callbackcallable - ($el[, $key])
Returns:

php\util\Flow

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:
  • $nint - skip count
Returns:

php\util\Flow

limit($count)

Limits collection with $count

Parameters:
  • $countint - count of limit
Returns:

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

Parameters:
  • $callbackcallable - ($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:
  • $comparatorcallable - ($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 values

Note

use the withKeys() method to save keys

Parameters:
  • $comparatorcallable - ($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:
  • $separatorstring
Returns:

string

count()
Returns:int
current()
Returns:mixed
next()
Returns:void
key()
Returns:mixed
valid()
Returns:bool
rewind()
Returns:void
__clone()

private

static ofEmpty
Returns:php\util\Flow
static of($collection)

Creates a new flow for an array of Iterator

Parameters:
  • $collectionarray, Traversable
Returns:

php\util\Flow

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

Creates a new flow for a number range

Parameters:
  • $fromint
  • $toint
  • $stepint
Returns:

php\util\Flow

static ofString($string, $chunkSize = 1)

Creates a new flow for the string

Parameters:
  • $stringstring
  • $chunkSizeint - how many characters to combine for one item ?
Returns:

php\util\Flow

static ofStream($stream, $chunkSize = 1)

Creates a new flow for the Stream object

Parameters:
  • $streamphp\io\Stream - stream object
  • $chunkSizeint - size for Stream.read($size) method
Returns:

php\util\Flow