1. Go to this page and download the library: Download matthijsbreijer/oaktree library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
matthijsbreijer / oaktree example snippets
use MatthijsBreijer\OakTree\Node;
$node = new Node('data');
// when $child1 is located at array key '1'
$child1 = $node->getChildByKey(1);
// when $child1 is located at array key 'customKey'
$child1 = $node->getChildByKey('customKey');
// requesting a non-existent key throws \OutOfBoundsException
$node->getChildByKey('nonExistentKey');
$child1 = new Node('child1');
$child2 = new Node('child2');
$node->addChild($child1)
->addChild($child2);
// all examples below produce same result
$child1 = $child1->pop();
$child1 = $node->getChildren()[0]->pop();
$child1 = $node->getChildByKey(0)->pop();
// using one of the above examples $node would look as follows
var_dump($node->getChildren()); // array(1) [1 => $child2]
$node->setChildren([new Node('a'), new Node('b')]);
// or with keys
$node->setChildren([0 => new Node('a'), 'customKey' => new Node('b')]);
$childNode->getParent(); // returns $parent Node
$root->getParent(); // $root has no parent Node and returns NULL
// all return $root Node when part of the same tree
$root->getRoot();
$child1->getRoot();
$grandChild1->getRoot();
$node->isLeaf(); // bool(true)
$node->isChild(); // bool(true)
$node->isRoot(); // bool(true)
$tree = new Node('root');
$child1 = new Node('child1');
$child2 = new Node('child2');
$tree->addChild($child1)
->addChild($child2);
$visitor = new LeafVisitor();
$leafs = $tree->accept($visitor); // array(2) [$child1, $child2]