/** * @param array $data * @return Tree\Tree */ public static function fromArray($data) { $tree = new self(); foreach ($data as $node) { $tree->setNode(Node::fromArray($node)); } return $tree; }
/** * Iterate through the tree to create an adjacency list from this object * * @param array $nodes * @param \Tree\NestedSet\Node $p * * @return \Tree\AdjacencyList\AdjacencyList */ private static function convertToAdjacencyList(&$nodes, &$p = null) { static $new_tree; if ($new_tree === null) { $new_tree = new \Tree\AdjacencyList\AdjacencyList(); } while (($node = current($nodes)) !== false) { if (is_null($p)) { $parent_id = 0; } else { $parent_id = $p->getId(); } $new_node = array('id' => $node->getId(), 'parent_id' => $parent_id); $new_tree->setNode(\Tree\AdjacencyList\Node::fromArray($new_node)); if (empty($nodes)) { break; } if (!($next = next($nodes))) { break; } if ($next->getLevel() == $node->getLevel()) { continue; } if ($next->getLevel() > $node->getLevel()) { // child self::convertToAdjacencyList($nodes, $node); if ($node = current($nodes) && $node->getLevel() <= $p->getLevel()) { break; } } else { break; } } return $new_tree; }