Class TreeIterator

A Recursive iterator used to flatten nested structures and also exposes all Collection methods

Properties summary

  • $_mode protected
    int

    The iteration mode

Method Summary

Method Detail

__construct() public

__construct(\RecursiveIterator $items, mixed $mode, mixed $flags)

Constructor

Parameters

\RecursiveIterator $items

The iterator to flatten.

int $mode optional

Iterator mode.

int $flags optional

Iterator flags.

_createMatcherFilter() protected

_createMatcherFilter(array $conditions)

Returns a callable that receives a value and will return whether or not it matches certain condition.

Parameters

array $conditions

A key-value list of conditions to match where the key is the property path to get from the current item and the value is the value to be compared the item with.

Returns

\Closure

_extract() protected

_extract(mixed $data, mixed $path)

Returns a column from $data that can be extracted by iterating over the column names contained in $path.

It will return arrays for elements in represented with {*}

Parameters

array|\ArrayAccess $data

Data.

string[] $path

Path to extract from.

Returns

mixed

_propertyExtractor() protected

_propertyExtractor(mixed $callback)

Returns a callable that can be used to extract a property or column from an array or object based on a dot separated path.

Parameters

string|callable $callback

A dot separated path of column to follow so that the final one can be returned or a callable that will take care of doing that.

Returns

callable

_simpleExtract() protected

_simpleExtract(mixed $data, mixed $path)

Returns a column from $data that can be extracted by iterating over the column names contained in $path

Parameters

array|\ArrayAccess $data

Data.

string[] $path

Path to extract from.

Returns

mixed

_unwrap() public

_unwrap()

Backwards compatible wrapper for unwrap()

Returns

\Traversable

append() public

append(mixed $items)

{@inheritDoc}

Parameters

mixed $items

appendItem() public

appendItem(mixed $item, mixed $key)

{@inheritDoc}

Parameters

mixed $item
mixed $key optional

avg() public

avg(mixed $matcher)

{@inheritDoc}

Parameters

mixed $matcher optional

buffered() public

buffered()

{@inheritDoc}

Returns

\Cake\Collection\Iterator\BufferedIterator

cartesianProduct() public

cartesianProduct(callable $operation, callable $filter)

Parameters

callable|null $operation optional

Operation

callable|null $filter optional

Filter

Returns

\Cake\Collection\CollectionInterface

Throws

LogicException

chunk() public

chunk(mixed $chunkSize)

{@inheritDoc}

Parameters

mixed $chunkSize

chunkWithKeys() public

chunkWithKeys(mixed $chunkSize, mixed $preserveKeys)

{@inheritDoc}

Parameters

mixed $chunkSize
mixed $preserveKeys optional

combine() public

combine(mixed $keyPath, mixed $valuePath, mixed $groupPath)

{@inheritDoc}

Parameters

mixed $keyPath
mixed $valuePath
mixed $groupPath optional

compile() public

compile(mixed $preserveKeys)

{@inheritDoc}

Parameters

mixed $preserveKeys optional

contains() public

contains(mixed $value)

{@inheritDoc}

Parameters

mixed $value

count() public

count()

{@inheritDoc}

Returns

int

countBy() public

countBy(mixed $callback)

{@inheritDoc}

Parameters

mixed $callback

countKeys() public

countKeys()

{@inheritDoc}

Returns

int

each() public

each(callable $c)

{@inheritDoc}

Parameters

callable $c

every() public

every(callable $c)

{@inheritDoc}

Parameters

callable $c

extract() public

extract(mixed $matcher)

{@inheritDoc}

Parameters

mixed $matcher

filter() public

filter(callable $c)

{@inheritDoc}

Parameters

callable $c optional

Returns

\Cake\Collection\Iterator\FilterIterator

first() public

first()

{@inheritDoc}

firstMatch() public

firstMatch(array $conditions)

{@inheritDoc}

Parameters

array $conditions

groupBy() public

groupBy(mixed $callback)

{@inheritDoc}

Parameters

mixed $callback

indexBy() public

indexBy(mixed $callback)

{@inheritDoc}

Parameters

mixed $callback

insert() public

insert(mixed $path, mixed $values)

{@inheritDoc}

Parameters

mixed $path
mixed $values

Returns

\Cake\Collection\Iterator\InsertIterator

isEmpty() public

isEmpty()

{@inheritDoc}

jsonSerialize() public

jsonSerialize()

{@inheritDoc}

last() public

last()

{@inheritDoc}

lazy() public

lazy()

{@inheritDoc}

listNested() public

listNested(mixed $dir, mixed $nestingKey)

{@inheritDoc}

Parameters

mixed $dir optional
mixed $nestingKey optional

Returns

\Cake\Collection\Iterator\TreeIterator

map() public

map(callable $c)

{@inheritDoc}

Parameters

callable $c

Returns

\Cake\Collection\Iterator\ReplaceIterator

match() public

match(array $conditions)

{@inheritDoc}

Parameters

array $conditions

max() public

max(mixed $callback, mixed $type)

{@inheritDoc}

Parameters

mixed $callback
mixed $type optional

median() public

median(mixed $matcher)

{@inheritDoc}

Parameters

mixed $matcher optional

min() public

min(mixed $callback, mixed $type)

{@inheritDoc}

Parameters

mixed $callback
mixed $type optional

nest() public

nest(mixed $idPath, mixed $parentPath, mixed $nestingKey)

{@inheritDoc}

Parameters

mixed $idPath
mixed $parentPath
mixed $nestingKey optional

newCollection() protected

newCollection(mixed ...$args)

Returns a new collection.

Allows classes which use this trait to determine their own type of returned collection interface

Parameters

mixed ...$args

Constructor arguments.

Returns

\Cake\Collection\CollectionInterface

optimizeUnwrap() protected

optimizeUnwrap()

Unwraps this iterator and returns the simplest traversable that can be used for getting the data out

Returns

\Traversable|array

prepend() public

prepend(mixed $items)

{@inheritDoc}

Parameters

mixed $items

prependItem() public

prependItem(mixed $item, mixed $key)

{@inheritDoc}

Parameters

mixed $item
mixed $key optional

printer() public

printer(mixed $valuePath, mixed $keyPath, mixed $spacer)

Returns another iterator which will return the values ready to be displayed to a user. It does so by extracting one property from each of the elements and prefixing it with a spacer so that the relative position in the tree can be visualized.

Both $valuePath and $keyPath can be a string with a property name to extract or a dot separated path of properties that should be followed to get the last one in the path.

Alternatively, $valuePath and $keyPath can be callable functions. They will get the current element as first parameter, the current iteration key as second parameter, and the iterator instance as third argument.

Example

$printer = (new Collection($treeStructure))->listNested()->printer('name');

Using a closure:

$printer = (new Collection($treeStructure))
     ->listNested()
     ->printer(function ($item, $key, $iterator) {
         return $item->name;
     });

Parameters

string|callable $valuePath

The property to extract or a callable to return the display value

string|callable|null $keyPath optional

The property to use as iteration key or a callable returning the key value.

string $spacer optional

The string to use for prefixing the values according to their depth in the tree

Returns

\Cake\Collection\Iterator\TreePrinter

reduce() public

reduce(callable $c, mixed $zero)

{@inheritDoc}

Parameters

callable $c
mixed $zero optional

reject() public

reject(callable $c)

{@inheritDoc}

Parameters

callable $c

Returns

\Cake\Collection\Iterator\FilterIterator

sample() public

sample(mixed $size)

{@inheritDoc}

Parameters

mixed $size optional

shuffle() public

shuffle()

{@inheritDoc}

skip() public

skip(mixed $howMany)

{@inheritDoc}

Parameters

mixed $howMany

some() public

some(callable $c)

{@inheritDoc}

Parameters

callable $c

sortBy() public

sortBy(mixed $callback, mixed $dir, mixed $type)

{@inheritDoc}

Parameters

mixed $callback
mixed $dir optional
mixed $type optional

stopWhen() public

stopWhen(mixed $condition)

{@inheritDoc}

Parameters

mixed $condition

Returns

\Cake\Collection\Iterator\StoppableIterator

sumOf() public

sumOf(mixed $matcher)

{@inheritDoc}

Parameters

mixed $matcher optional

take() public

take(mixed $size, mixed $from)

{@inheritDoc}

Parameters

mixed $size optional
mixed $from optional

takeLast() public

takeLast(mixed $howMany)

{@inheritDoc}

Parameters

mixed $howMany

through() public

through(callable $handler)

{@inheritDoc}

Parameters

callable $handler

toArray() public

toArray(mixed $preserveKeys)

{@inheritDoc}

Parameters

mixed $preserveKeys optional

toList() public

toList()

{@inheritDoc}

transpose() public

transpose()

{@inheritDoc}

Returns

\Cake\Collection\CollectionInterface

Throws

LogicException

unfold() public

unfold(callable $transformer)

{@inheritDoc}

Parameters

callable $transformer optional

unwrap() public

unwrap()

{@inheritDoc}

zip() public

zip(mixed $items)

{@inheritDoc}

Parameters

mixed $items

zipWith() public

zipWith(mixed $items, mixed $callable)

{@inheritDoc}

Parameters

mixed $items
mixed $callable

Property Detail

$_mode protected

The iteration mode

Type

int

© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/3.9/class-Cake.Collection.Iterator.TreeIterator.html