/** * Add child to this node * * If the child already has a parent, the link is unset * * @param Tree_Node $child The child to be added * * @return void */ public function add_child(Tree_Node $child) { if ($child instanceof Tree_Node) { $child->set_parent($this); } return $this; }
/** * Removes a node from the child nodes array by using * reference comparison. * * @param Tree_Node $node The node to remove * @param bool $search Whether to search child nodes * @return bool True/False */ public function remove(Tree_Node $node, $search = false) { foreach ($this->nodes as $index => $_node) { if ($_node === $node) { // Unset parent, tree $node->setParent(null); $node->setTree(null); unset($this->nodes[$index]); $this->nodes = array_values($this->nodes); return true; } else { if ($search and $_node->hasChildren()) { $searchNodes[] = $_node; } } } // Go thru searching those nodes that have children if (!empty($searchNodes)) { foreach ($searchNodes as $_node) { if ($_node->nodes->remove($node, true)) { return true; } } } return false; }