$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); } } $array = array('data' => new MyDate(), 'children' => array(array('data' => new MyDate(), 'children' => array()), array('data' => new MyDate(), 'children' => array()), array('data' => new MyDate(), 'children' => array(array('data' => new MyDate(), 'children' => array(array('data' => new MyDate(), 'children' => array(array('data' => new MyDate(), 'children' => array()), array('data' => new MyDate(), 'children' => array()))), array('data' => new MyDate(), 'children' => array()))), array('data' => new MyDate(), 'children' => array(array('data' => new MyDate(), 'children' => array(array('data' => new MyDate(), 'children' => array(array('data' => new MyDate(), 'children' => array(array('data' => new MyDate(), 'children' => array()), array('data' => new MyDate(), 'children' => array()))))))))))))); /** * @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 array $inCurrent Current node. * @param bool $isLead This boolean tells the function whether the current node is a leaf or not. * @param (array|null $inParent Associative array that represents the parent's node. * This value may be null is the current node is the getRoot node. * @param array $inOutResult Permanent variable used by the function to organize and store values. */ $traversalFunction = function (array $inCurrent, $isLead, $inParent, &$inOutResult) { /** @var MyDate $data */ $data = $inCurrent['data']; $inOutResult[] = $data->serialise(); // Call the data' serializer. }; Tree::arrayTraverse($array, $traversalFunction, $result); echo print_r($result, true);
/** * This method checks that the method used to traverse a tree defined as an array works as expected. */ public function testArrayTraverse() { $fixture = $this->__fixturesDir . DIRECTORY_SEPARATOR . 'traverse.json'; $result = []; $function = function (array $inCurrent, $isLead, $inParent, &$inOutResult) { $inOutResult[] = $inCurrent['data']; }; Tree::arrayTraverse($this->__treeAsArray, $function, $result); $this->assertJsonStringEqualsJsonFile($fixture, json_encode($result)); }