Exemple #1
0
 public function testIsParentOf()
 {
     /**
      * @var Node $root
      * @var Node $A
      * @var Node $AA
      * @var Node $B
      * @var Node $C
      * @var Node $D
      * @var Node $E
      * @var Node $J
      */
     $root = $this->__treeByData->getRoot();
     $found = $this->__treeByData->search('A');
     $this->assertEquals(count($found), 2);
     $A = $found[0];
     $AA = $found[1];
     $found = $this->__treeByData->search('B');
     $this->assertEquals(count($found), 1);
     $B = $found[0];
     $found = $this->__treeByData->search('C');
     $this->assertEquals(count($found), 1);
     $C = $found[0];
     $found = $this->__treeByData->search('D');
     $this->assertEquals(count($found), 1);
     $D = $found[0];
     $found = $this->__treeByData->search('E');
     $this->assertEquals(count($found), 1);
     $E = $found[0];
     $found = $this->__treeByData->search('J');
     $this->assertEquals(count($found), 1);
     $J = $found[0];
     $this->assertTrue($A->isParentOf($B));
     $this->assertTrue($A->isParentOf($C));
     $this->assertTrue($A->isParentOf($D));
     $this->assertTrue($D->isParentOf($E));
     $this->assertTrue($D->isParentOf($J));
     $this->assertFalse($A->isParentOf($root));
     $this->assertFalse($B->isParentOf($A));
     $this->assertFalse($C->isParentOf($A));
     $this->assertFalse($D->isParentOf($A));
     $this->assertFalse($B->isParentOf($B));
     $this->assertFalse($J->isParentOf($D));
     // ... It's OK
 }
Exemple #2
0
/**
 * This example shows how to search for a node's data within the entire tree.
 */
include __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
use dbeurive\Tree\Tree;
use dbeurive\Tree\Node;
$tree = new Tree("getRoot");
$tree->getRoot()->addChild("A")->end()->addChild("B")->end()->addChild("C")->addChild("D")->addChild("E")->addChild("EE")->end()->addChild("EEE")->end()->end()->addChild("F")->end()->end()->addChild("A")->addChild('H')->addChild("I")->addChild("J")->addChild("K")->end()->addChild("L")->end()->end()->end()->end()->end()->end();
/**
 * @var callable Function used to compare two nodes' values.
 * @param mixed $value1 First value.
 * @param mixed $value2 Second value.
 * @return bool If $value1 is equal to $value2, then the function returns the value "true".
 *         Otherwise the function returns the value "false".
 */
$optionalCompareFunction = function ($value1, $value2) {
    return $value1 == $value2;
};
/**
 * @var array List of nodes.
 */
$found = $tree->search("A", 0, $optionalCompareFunction);
/**
 * @var int $_index
 * @var Node $_node
 */
foreach ($found as $_index => $_node) {
    $label = $_node->isLeaf() ? "this is a leaf" : "this is not a leaf";
    echo spl_object_hash($_node) . " " . $_node->getData() . " ({$label})\n";
}
Exemple #3
0
<?php

/**
 * This example shows how search for nodes with given values.
 */
include __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
use dbeurive\Tree\Tree;
$tree = new Tree("getRoot");
$tree->getRoot()->addChild("A")->end()->addChild("B")->end()->addChild("C")->addChild("D")->addChild("E")->addChild("EE")->end()->addChild("EEE")->end()->end()->addChild("F")->end()->end()->addChild("A")->addChild('H')->addChild("I")->addChild("J")->addChild("K")->end()->addChild("L")->end()->end()->end()->end()->end()->end();
/**
 * This function is used to compare two nodes' values.
 * @param string $inData1 First value to compare.
 * @param string $inData2 Second value to compare.
 * @return bool If the values are identical, then the function returns the value true. Otherwise, it returns the value true.
 */
$dataComparator = function ($inData1, $inData2) {
    return $inData1 === $inData2;
};
// We should select 2 nodes.
$result = $tree->search("A", 0, $dataComparator);
/** @var \dbeurive\Tree\Node $_node */
foreach ($result as $_node) {
    print $_node->getData() . " -> Object(" . spl_object_hash($_node) . ")\n";
}