/** * Traverse the tree, using the tree that has been created by nodes injections. * Nodes have been instantiated prior to the tree's creation. */ public function testTraverseObjects() { $result = []; $userProvidedFunction = function (Node $inNode, array &$inOutResult) { $inOutResult[] = spl_object_hash($inNode); // We store the objects' identifiers. }; $this->__treeByObjects->traverse($userProvidedFunction, $result); $occurrences = array_count_values($result); $this->assertCount(15, $result); // "A" should be found 2 times. $entry1 = spl_object_hash($this->__nodesObjects[0]); // This is the first "A". $entry2 = spl_object_hash($this->__nodesObjects[14]); // This is the second "A". $this->assertContains($entry1, $result); $this->assertContains($entry2, $result); // All other values should be found only once. foreach (['B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N'] as $_index => $_data) { $entry = spl_object_hash($this->__nodesObjects[$_index + 1]); $this->assertContains($entry, $result); $this->assertEquals(1, $occurrences[$entry]); } }
$this->__creationDate = new DateTime(); $this->__creationDate->add(new DateInterval('P' . self::$__inter . 'D')); 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 array This array will be manipulated by the traversal function. * The traversal function if free to use this array as it sees fit. */ $result = []; /** * @var callable The traversal function. * @param Node $inNode The current node. * @param array $inOutResult Reference to the array used by the traversal function. * The traversal function if free to use this array as it sees fit. */ $traversalFunction = function (Node $inNode, array &$inOutResult) { /** @var MyDate $data */ $data = $inNode->getData(); $inOutResult[] = $data->serialise(); // Call the data' serializer. }; $tree->traverse($traversalFunction, $result); print_r($result);