dev-master
9999999-devPHP Implementation of a NestedSet
MIT
The Requires
- php >=7.1
The Development Requires
by Marco Kretz
PHP Implementation of a NestedSet
This is my own implementation of the "Nested Set Model"., (*2)
It has not database connection, yet. Will implement a mysql-synchronization feature soon!, (*3)
A Node represents a single node (or container) within the NestedSet. It's uniquely identified by a name. It can safely be extended (e.g. by a payload attribute) and will still work with the NestedSet., (*4)
There is also an extended Node
called PayloadNode
which can hold arbitrary, serializable data., (*5)
$myNode = new Node('myNode'); $anotherNode = new Node('anotherNode);
The heart of this library. It manages a NestedSet-Model and uses Node
as nodes (containers).
A NestedSet is also an Iterator, so you can simply iterate over it with Nodes as values.
Furthermore it's possible to serialize single Nodes or the whole NestedSet., (*6)
$ns = new NestedSet(); // Define nodes $rootNode = new Node('root'); $childNode1 = new Node('child1'); $childNode2 = new Node('child2'); // Set root node $ns->addRoot($rootNode); // Add nodes $ns->addNode($rootNode, $childNode1); $ns->addNode($rootNode, $childNode2); print($ns); // Remove node $ns->removeNode($childNode2); print($ns); // Retrieve sub-nodes $rootChildren = $ns->getSubNodes($rootNode); for ($rootChildren as $rootChild) { print($rootChild); } // Iterate over NestedSet for ($ns as $index => $node) { print($node); } // Serialization $serialized = serialize($ns); $nsCopy = unserialize($serialized); print($ns === $nsCopy); // true
composer test
or simply phpunit
, (*7)
PHP Implementation of a NestedSet
MIT