/** * root * / \ * A B * / \ * C D * | * E */ public function testGetYield() { $root = new Node('root'); $root->addChild($a = new Node('A'))->addChild($b = new Node('B')); $a->addChild($c = new Node('C'))->addChild($d = new Node('D', [$e = new Node('E')])); $visitor = new YieldVisitor(); $yield = $root->accept($visitor); $this->assertSame([$c, $e, $b], $yield); }
/** * root * /|\ * a b c * /| | * d e f */ public function testWalkSubTree() { $root = new Node('root'); $a = new Node('a'); $b = new Node('b'); $c = new Node('c'); $d = new Node('d'); $e = new Node('e'); $f = new Node('f'); $root->addChild($a); $root->addChild($b); $root->addChild($c); $a->addChild($d); $a->addChild($e); $b->addChild($f); $visitor = new PostOrderVisitor(); $expected = [$d, $e, $a]; $this->assertSame($expected, $visitor->visit($a)); }
public function convertConfigToTree(array $config, Node $root) { foreach ($config as $key => $value) { // If the key is not a string, we're on the last node if (is_integer($key)) { $node = new Node($value); } else { $node = new Node($key); if (is_array($value)) { $this->convertConfigToTree($value, $node); } else { $node->addChild(new Node($value)); } } $root->addChild($node); } return $root; }
public function testGetSize() { $root = new Node(); $root->addChild($child1 = new Node('child1'))->addChild($child2 = new Node('child2'))->addChild($child3 = new Node('child3')); $child3->addChild(new Node("a"))->addChild($child4 = new Node("b")); $child4->addChild($child5 = new Node("c")); $child5->addChild(new Node("d"))->addChild(new Node("f")); $this->assertEquals(9, $root->getSize()); $this->assertEquals(3, $child5->getSize()); $this->assertEquals(4, $child4->getSize()); $this->assertEquals(6, $child3->getSize()); $this->assertEquals(1, $child2->getSize()); }
public function testGetHeight() { $root = new Node(); $root->addChild($child1 = new Node('child1'))->addChild($child2 = new Node('child2'))->addChild($child3 = new Node('child3')); $child3->addChild(new Node("a"))->addChild(new Node("b")); $this->assertEquals(0, $child1->getHeight()); $this->assertEquals(2, $root->getHeight()); $this->assertEquals(1, $child3->getHeight()); }
/** * Recursively build chart of account tree * * @param Node $tree * @param \DOMNode $node * @param Chart $chart * @param array $accountTypes */ protected function buildTree(Node $tree, \DOMNode $node, Chart $chart, array $accountTypes) { //create current node list($nominal, $type, $name) = FFor::create()->attributes(function () use($node) { return $node->attributes; })->nominal(function ($attributes) { return new Nominal($attributes->getNamedItem('nominal')->nodeValue); })->name(function ($attributes) { return new StringType($attributes->getNamedItem('name')->nodeValue); })->type(function ($attributes) use($accountTypes) { return new AccountType($accountTypes[strtoupper($attributes->getNamedItem('type')->nodeValue)]); })->fyield('nominal', 'type', 'name'); $tree->setValue(new Account($chart, $nominal, $type, $name)); //recurse through sub accounts foreach ($node->childNodes as $childNode) { if ($childNode instanceof \DOMElement) { $childTree = new Node(); $tree->addChild($childTree); $this->buildTree($childTree, $childNode, $chart, $accountTypes); } } }
public function testIsChild() { $root = new Node('root'); $root->addChild($child = new Node('child')); $this->assertTrue($child->isChild()); $this->assertFalse($root->isChild()); }
public function testBasicArray() { $tree = new Tree(); $a = new Node('a'); $c = new Node('c'); $a->addChild($c); $a->addChild($d = new Node('d')); $b = new Node('b'); $b->addChild($e = new Node('e')); $b->addChild($f = new Node('f')); $tree->addNode($a); $tree->addNode($b); $tree->addNode($g = new Node('g')); $h = new Node('h'); $tree->addNode($h, $c); $this->assertEquals([1 => $a, 2 => $c, 3 => $d, 4 => $b, 5 => $e, 6 => $f, 7 => $g, 8 => $h], $tree->getNodes()); }