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 }
<?php /** * 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";
$this->__creationDate = new DateTime(); $this->__creationDate->add(new DateInterval('P' . self::$__inter . 'D')); if (self::$__inter > 3) { self::$__inter = 0; } else { self::$__inter += 1; } } public function serialise($inOptFormat = 'Y-m-d H:i:s') { return $this->__creationDate->format($inOptFormat); } } $tree = new Tree(new MyDate()); // Root = date. $tree->getRoot()->addChild(new MyDate())->end()->addChild(new MyDate())->end()->addChild(new MyDate())->addChild(new MyDate())->addChild(new MyDate())->addChild(new MyDate())->end()->addChild(new MyDate())->end()->end()->addChild(new MyDate())->end()->end()->addChild(new MyDate())->addChild(new MyDate())->addChild(new MyDate())->addChild(new MyDate())->addChild(new MyDate())->end()->addChild(new MyDate())->end()->end()->end()->end()->end()->end(); /** * @var callable This function generates an index from a given data. * @param MyDate $inData Data to serialize. * @return string The function returns the generated index. */ $indexBuilder = function (MyDate $inData) { return $inData->serialise(); // Call the data' serializer. }; // Please note the use of the second parameter (which value is false). // This tells the function that indexes should point to array of nodes. $index = $tree->index($indexBuilder, false); /** @var array $_value */ foreach ($index as $_key => $_value) { print "{$_key} => " . count($_value) . " elements.\n";
$A = new Node("A"); $AA = new Node("A"); $B = new Node("B"); $C = new Node("C"); $D = new Node("D"); $E = new Node("E"); $EE = new Node("EE"); $EEE = new Node("EEE"); $F = new Node("F"); $H = new Node("H"); $I = new Node("I"); $J = new Node("J"); $K = new Node("K"); $L = new Node("L"); $tree = new Tree($root); $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($AA)->addChild($H)->addChild($I)->addChild($J)->addChild($K)->end()->addChild($L)->end()->end()->end()->end()->end()->end(); // --------------------------------------------------------------------------------------------------------------------- // Testing ancestry // --------------------------------------------------------------------------------------------------------------------- print "Is <" . $root->getData() . "> an ascendant of <" . $L->getData() . "> ? " . ($root->isAscendantOf($L) ? 'yes' : 'no') . "\n"; print "Is <" . $root->getData() . "> an ascendant of <" . $F->getData() . "> ? " . ($root->isAscendantOf($F) ? 'yes' : 'no') . "\n"; print "Is <" . $L->getData() . "> an ascendant of <" . $K->getData() . "> ? " . ($L->isAscendantOf($K) ? 'yes' : 'no') . "\n"; // --------------------------------------------------------------------------------------------------------------------- // Testing descendance // --------------------------------------------------------------------------------------------------------------------- print "Is <" . $root->getData() . "> a descendant of <" . $L->getData() . "> ? " . ($root->isDescendantOf($L) ? 'yes' : 'no') . "\n"; print "Is <" . $root->getData() . "> a descendant of <" . $F->getData() . "> ? " . ($root->isDescendantOf($F) ? 'yes' : 'no') . "\n"; print "Is <" . $L->getData() . "> a descendant of <" . $K->getData() . "> ? " . ($L->isDescendantOf($K) ? 'yes' : 'no') . "\n"; // --------------------------------------------------------------------------------------------------------------------- // Is a node the parent of another node ? // ---------------------------------------------------------------------------------------------------------------------