A PHP tree data structure. Allows for referencing nodes by unique key.
This library aims to provide an object oriented way to create a tree data structure. Each node has a user defined unique id by which it is referenced.
composer require tebru/tree:1.0.*
Creating a tree is really easy. All you have to do is instantiate it:
$tree = new Tree();
This will create the tree object and create a root node to act upon.
There are many options for creating a node.
- Creating a node appended to the root node can be done like
- If you want to add this node to a different node you can do so like
- You have the option to add arbitrary data to a node by doing
$tree->createNode('a_category', null, 'Foo Category');
- Finally, you can specify the position your node should appear in the parent's children like
$tree->createNode('node', null, null, 10);
- You may not specify an position for an index that does not already exist
- Setting the position will bump all existing nodes back 1 space starting with the node at the specified index
Just pass in the node id:
This will remove that node and all of its children from the tree.
This is also easy. Just specify the node id of the node you wish to move and the node id of its new parent:
Run tests by running
phpunit from the root directory.
This project uses semantic versioning [http://semver.org] for versioning.