/** * Index a tree using the tree that has been created by objects injections. * Nodes have been instantiated prior to the tree's creation. * Indexes points to (single) nodes. */ public function testIndexObjectsUnique() { $index = $this->__treeByObjects->index(null, true); // IDs are SHA1(data). $this->assertCount(14, $index); for ($_i = 0; $_i < count($this->__nodesData); $_i++) { $_data = $this->__nodesData[$_i]; $_id = sha1($_data); /** @var Node $_node */ $_node = $index[$_id]; /** @var Node $_expectedNode */ $_expectedNode = $_data == 'A' ? $this->__nodesObjects[14] : $this->__nodesObjects[$_i]; $this->assertSame($_expectedNode, $_node); } }
} 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"; foreach ($_value as $_o) { print " -- Object(" . spl_object_hash($_o) . ")\n"; } }
private $__creationDate = null; private static $__inter = 0; public function __construct() { $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 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 true, by default). // This tells the function that indexes should point to (single) nodes. $index = $tree->index($indexBuilder); foreach ($index as $_key => $_value) { print "{$_key} => " . "Object(" . spl_object_hash($_value) . ")\n"; }