2017 © Pedro Peláez
 

library nested-set

PHP Implementation of a NestedSet

image

marco-kretz/nested-set

PHP Implementation of a NestedSet

  • Thursday, July 26, 2018
  • by marco-kretz
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

PHP - NestedSet

Build Status, (*1)

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)

Requirements

  • composer
  • PHP >= 7.1

Node

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)

Usage

$myNode = new Node('myNode');
$anotherNode = new Node('anotherNode);

NestedSet

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)

Usage

$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

Testing

composer test or simply phpunit, (*7)

Todo

  • Add database sync
  • more?

The Versions

26/07 2018

dev-master

9999999-dev

PHP Implementation of a NestedSet

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Marco Kretz