Class TreeIterator

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

RecursiveIteratorIterator implements Iterator, Traversable, OuterIterator
Extended by Cake\Collection\Iterator\TreeIterator uses Cake\Collection\CollectionTrait

Inherited Constants

  • CATCH_GET_CHILD, CHILD_FIRST, LEAVES_ONLY, SELF_FIRST

Properties summary

  • $_mode protected
    integer
    The iteration mode

Method Summary

  • __construct() public
    Constructor
  • printer() public

    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.

Method Detail

__construct()source public

__construct( RecursiveIterator $items , integer $mode Cake\Collection\Iterator\RecursiveIteratorIterator::SELF_FIRST , integer $flags 0 )

Constructor

Parameters

RecursiveIterator $items
The iterator to flatten.
integer $mode optional Cake\Collection\Iterator\RecursiveIteratorIterator::SELF_FIRST
Iterator mode.
integer $flags optional 0
Iterator flags.

Overrides

RecursiveIteratorIterator::__construct()

printer()source public

printer( string|callable $valuePath , string|callable|null $keyPath null , string $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 null

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

Methods used from Cake\Collection\CollectionTrait

_unwrap()source public

_unwrap( )

append()source public

append( $items )

avg()source public

avg( $matcher null )

buffered()source public

buffered( )

Returns

Cake\Collection\Iterator\BufferedIterator

cartesianProduct()source public

cartesianProduct( callable $operation null , callable $filter null )

Returns

Cake\Collection\CollectionInterface

chunk()source public

chunk( $chunkSize )

chunkWithKeys()source public

chunkWithKeys( $chunkSize , $preserveKeys true )

combine()source public

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

compile()source public

compile( $preserveKeys true )

contains()source public

contains( $value )

countBy()source public

countBy( $callback )

each()source public

each( callable $c )

every()source public

every( callable $c )

extract()source public

extract( $matcher )

filter()source public

filter( callable $c null )

Returns

Cake\Collection\Iterator\FilterIterator

first()source public

first( )

firstMatch()source public

firstMatch( array $conditions )

groupBy()source public

groupBy( $callback )

indexBy()source public

indexBy( $callback )

insert()source public

insert( $path , $values )

Returns

Cake\Collection\Iterator\InsertIterator

isEmpty()source public

isEmpty( )

jsonSerialize()source public

jsonSerialize( )

last()source public

last( )

listNested()source public

listNested( $dir 'desc' , $nestingKey 'children' )

Returns

Cake\Collection\Iterator\TreeIterator

map()source public

map( callable $c )

Returns

Cake\Collection\Iterator\ReplaceIterator

match()source public

match( array $conditions )

max()source public

max( $callback , $type SORT_NUMERIC )

median()source public

median( $matcher null )

min()source public

min( $callback , $type SORT_NUMERIC )

nest()source public

nest( $idPath , $parentPath , $nestingKey 'children' )

optimizeUnwrap()source protected

optimizeUnwrap( )

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

Returns

Traversable|array

reduce()source public

reduce( callable $c , $zero null )

reject()source public

reject( callable $c )

Returns

Cake\Collection\Iterator\FilterIterator

sample()source public

sample( $size 10 )

shuffle()source public

shuffle( )

skip()source public

skip( $howMany )

some()source public

some( callable $c )

sortBy()source public

sortBy( $callback , $dir SORT_DESC , $type SORT_NUMERIC )

stopWhen()source public

stopWhen( $condition )

Returns

Cake\Collection\Iterator\StoppableIterator

sumOf()source public

sumOf( $matcher null )

take()source public

take( $size 1 , $from 0 )

through()source public

through( callable $handler )

toArray()source public

toArray( $preserveKeys true )

toList()source public

toList( )

transpose()source public

transpose( )

Returns

Cake\Collection\CollectionInterface

unfold()source public

unfold( callable $transformer null )

unwrap()source public

unwrap( )

zip()source public

zip( $items )

zipWith()source public

zipWith( $items , $callable )

Properties detail

$_modesource

protected integer

The iteration mode

© 2005–2017 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.4/class-Cake.Collection.Iterator.TreeIterator.html