Download the PHP package dynamic-commerce/tree-nodes without Composer
On this page you can find all versions of the php package dynamic-commerce/tree-nodes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dynamic-commerce/tree-nodes
More information about dynamic-commerce/tree-nodes
Files in dynamic-commerce/tree-nodes
Package tree-nodes
Short Description Library to handle tree structures with entities
License MIT
Homepage https://www.dc.ag
Informations about the package tree-nodes
Small library for a basic but very flexible tree data structure. You have the possibility to sort tree nodes and control the payload type within the tree nodes.
Overview
The interface TreeNodes\TreeNode
) and a payload (can be a simple integer or boolean or a complex object).
We offer different kind of tree nodes. Choose one which fits your use-case the best.
Tree Node types
- TreeNode
- TypedPayloadTreeNode
- SortableTreeNode
- TypedPayloadSortableTreeNode
For each type a generic implementation class exists, e.g. TreeNodes\TreeNode
.
Install
Best way to get started is to use Composer.
composer require dynamic-commerce/tree-nodes
Basic usage
In the following examples we use the following example tree structure:
Id Generator
Each tree node has a id property. To generate this id, you can specify your own IdGenerator using the interface TreeNodes\GenericIdGenerator
to generate a v4 UUID.
Create a new TreeNode
The interface TreeNodes\TypedPayloadTreeNode
offers a set of payload types as public constants you can use to restrict the type of the payload.
If you want to have a object as payload to just be a specific class you can simply provide the FQDN.
Adding children
TreeNodes with sorting
When you add a new sortable child the sorting will be calculated and set to the next highest sorting of the current children within the current parent node.
When you want to change the sorting of a tree node you can simply request it. Use the static method processNewSortingRequest
providing the node you want to move and the new sorting.
Not only can you change the sorting within a tree node children you can even move a node with all it's children to a new position. To do that you can use the static method processMoveRequest
providing the node you want to move, the new parent node e.g. Root and the new sorting it should have.
You can implement your own classes for handling sort and move requests. Simple use the interface TreeNodes\canProcessTreeNodeSortRequests
.
Remove a child
Same as with adding children you can remove the children with different types by using separate functions
Testing
We use PHPUnit to automate testing and to detect issues. All unit tests are located in the phpunit.xml
and run ./vendor/bin/phpunit -c phpunit.xml
in your preferred CMD.