Tree builder helper
this is a small library for symfony console to print a tree in a fluent and flexible way., (*1)
The output is similar as the linux tree command, an can be used to debug/output relational data., (*2)
Usage:
$tree = new TreeHelper($output);
$tree
->newNode('first')
->addValue('fist value')
->addValue('second value')
->newNode('fist sub')
->addValue('fist sub value')
->addValue('second sub value')
->newNode('fist sub sub')
->addValue('fist sub sub value')
->addValue('second sub sub value')
->end()
->end()
->newNode('second')
->addValue('second value')
->addValue('second second value')
->end();
$tree->printTree();
or, (*3)
$tree = new TreeHelper($output);
$tree->addArray([
'first' => [
0 => 'fist value',
1 => 'second value',
'fist sub' => [
0 => 'fist sub value',
1 => 'second sub value',
'fist sub sub' => [
'fist sub sub value',
'second sub sub value',
],
],
],
'second' => [
'second value',
'second second value',
]
]);
that will output:, (*4)
.
โ
โโโ first
โโโ fist value
โโโ second value
โโโ fist sub
โ โโโ fist sub value
โ โโโ second sub value
โ โโโ fist sub sub
โ โโโ fist sub sub value
โ โโโ second sub sub value
โโโ second
โโโ second value
โโโ second second value
for other example see the the tests., (*5)
Methods
newNode($name)
Create an new node and bind it to the parent, (*6)
addNode($node)
Append node to tree, (*7)
end()
Will return the parent object (if set else null), (*8)
getRoot()
Will return the root node, (*9)
printTree($output)
Print the tree to output, (*10)
findNode($name)
Search a node by title, (*11)
toArray()
Returns a array representation of the data, (*12)
getTitle()
Returns the title of the node, (*13)
setTitle($title)
Sets the title of the node, (*14)
addValue($value)
Add a value to the node, this can be a valid scalar or a object with method __toString, (*15)
addArray($array)
Add a nested array to node, (*16)
setValues($array)
Add a flat array to node, this be set as the values of the node, (*17)
setParent($parent)
Set the parent of the node, will trow a RuntimeException is the the node all ready
is linked to the given node, so we don`t get infinite loops., (*18)
getNodes()
Get all the (child) nodes defined in the node, all the nodes are only saved in the root
so to get all nodes you probably have to do this $t->getRoot()->getNodes(); else it will
return the nodes where the parent is set as node where from you calling the method., (*19)
setMaxDepth($depth)
Set the max depth of the node values, this can be done globally (on root) or on a specific node., (*20)
getMaxDepth()
Return the max depth that is set, (*21)
Set the form that is used for example:, (*22)
$tree->setFormats([
TreeHelper::LINE_PREFIX_EMPTY => ' ',
TreeHelper::LINE_PREFIX => 'โ ',
TreeHelper::TEXT_PREFIX => 'โ ',
TreeHelper::TEXT_PREFIX_END => 'โ ',
]);
no it will use a 2 space indent instead of 4, see tests, (*23)
Overwrite a specific format, (*24)