Class TreeIterator
A Recursive iterator used to flatten nested structures and also exposes all Collection methods
- RecursiveIteratorIterator implements Iterator, Traversable, OuterIterator
- Cake\Collection\Iterator\TreeIterator uses Cake\Collection\CollectionTrait
Inherited Constants
-
CATCH_GET_CHILD
,CHILD_FIRST
,LEAVES_ONLY
,SELF_FIRST
Properties summary
-
$_mode
protectedinteger
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() 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() 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
Methods used from Cake\Collection\CollectionTrait
cartesianProduct() public ¶
cartesianProduct( callable $operation null , callable $filter null )
Parameters
- callable $operation optional null
- Operation
- callable $filter optional null
- Filter
Returns
Throws
optimizeUnwrap() protected ¶
optimizeUnwrap( )
Unwraps this iterator and returns the simplest traversable that can be used for getting the data out