2017 © Pedro Peláez
 

library data-tree

image

timostamm/data-tree

  • Wednesday, October 4, 2017
  • by timostamm
  • Repository
  • 1 Watchers
  • 0 Stars
  • 42 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 20 % Grown

The README.md

PHP Tree data structure

This library provides a tree structure via discrete Traits., (*1)

It is intended to be used by other libraries., (*2)

ChildrenTrait

Add, acces and remove child nodes using this trait., (*3)

LookupTrait

Optional methods to inspect the tree., (*4)

AttributesTrait

Optional attributes per node., (*5)

ToStringTrait

Optional __toString() implementation that outputs the nodes class name and its attributes., (*6)

NodeTrait

Combines all of the above traits, (*7)

Protected Access

The sub-namespace ProtectedAccess contains versions of ChildrenTrait, LookupTrait and AttributesTrait where all methods are protected. This allows fine control over the public API when using the Traits in a library., (*8)

Example

// Create a simple tree.
$root = new Node();
$root->setAttribute('name', 'root');

$a = new Node();
$a->setAttribute('name', 'a');
$root->addChild($a);

$b = new Node();
$b->setAttribute('name', 'b');
$root->addChild($b);

$c = new Node();
$c->setAttribute('name', 'c');
$root->addChild($c);


// Find a descendant with the name=b
$bAgain = $root->descendant(function ($node) {
    return $node->getAttribute('name') === 'b';
});

// Get the root node of any node
$rootAgain = $bAgain->findRootNode();

// Remove a node
$bAgain->remove();
$root->removeChild($a);
$root->removeChildAt(0);

The Versions

04/10 2017

dev-master

9999999-dev

  Sources   Download

AGPL-3.0

The Requires

  • php >=5.5.9

 

The Development Requires