In Tree you can find a basic but flexible tree data structure for php together with and an handful Builder class, that enables you to build tree in a fluent way.
The Tree\NodeInterface interface abstracts the concept of a tree node. In Tree a Node has essentially two things:
a set of children (that implements the same NodeInterface interface) and a value.
On the other hand, the Tree\Node gives a straight implementation for that interface.
use Tree\Node;
$node = new Node('foo');Each node has a value property, that can be any php value.
$node->setValue('my value');
echo $node->getValue(); //Prints 'my value'$child1 = new Node('child1');
$child2 = new Node('child2');
$node
->addChild($child1)
->addChild($child2);$node->removeChild($child1);$children = $node->getChildren();$node->setChildren([new Node('foo'), new Node('bar')]);$node->removeAllChildren();A leaf is a node with no children.
$node->isLeaf();The builder provides a convenient way to build trees. It is provided by the Builder class,
but you can implement your own builder making an implementation of the BuilderInterfaceclass.
Let's see how to build the following tree, where the nodes label are represents nodes values:
A
/ \
B C
/|\
D E F
/|
G H
And here is the code:
$builder = new Tree\Builder\NodeBuilder;
$builder
->value('A')
->leaf('B')
->tree('C')
->tree('D')
->leaf('G')
->leaf('H')
->end()
->leaf('E')
->leaf('F')
->end()
;
$nodeA = $builder->getNode();The example should be self-explanatory, but here you are a brief description of the methods used above.
Set the value of the current node to $value
Add to the current node a new child whose value is $value.
Add to the current node a new child whose value is $value, and set the new node as the builder current node.
Returns to the context the builder was before the call to treemethod,
i.e. make the builder go one level up.
Returns the current node.
The best way to install Tree is through composer.
Just create a composer.json file for your project:
{
"require": {
"nicmart/tree": "dev-master"
}
}Then you can run these two commands to install it:
$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install
or simply run composer install if you have have already installed the composer globally.
Then you can include the autoloader, and you will have access to the library classes:
<?php
require 'vendor/autoload.php';phpunit
