/**
  * @param RokMenuNodeTree $menu
  * @return RokMenuNodeTree menu after reprocessing
  */
 protected function preProcessMenu(RokMenuNodeTree &$menu)
 {
     $remove_nodes = array();
     $nodeIterator = new RecursiveIteratorIterator($menu, RecursiveIteratorIterator::SELF_FIRST);
     foreach ($nodeIterator as $node) {
         if (!$this->isAccessable($node)){
             $remove_nodes[] = $node->getId();
         }
     }
     foreach($remove_nodes as $remove_node){
         $menu->removeNode($remove_node);
     }
     return $menu;
 }
 /**
  * Takes the menu item nodes and puts them into a tree structure
  * @param  $nodes
  * @param  $maxdepth
  * @return bool|RokMenuNodeTree
  */
 protected function createMenuTree(&$nodes, $maxdepth)
 {
     // TODO: move maxdepth to higher processing level?
     if (!empty($nodes)) {
         // Build Menu Tree root down (orphan proof - child might have lower id than parent)
         $ids = array();
         $ids[0] = true;
         $unresolved = array();
         // pop the first item until the array is empty if there is any item
         if (is_array($nodes)) {
             while (count($nodes) && !is_null($node = array_shift($nodes))) {
                 if (!$this->menu->addNode($node)) {
                     if (!array_key_exists($node->getId(), $unresolved) || $unresolved[$node->getId()] < $maxdepth) {
                         array_push($nodes, $node);
                         if (!isset($unresolved[$node->getId()])) {
                             $unresolved[$node->getId()] = 1;
                         } else {
                             $unresolved[$node->getId()]++;
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * Gets the current active based on the current_node
  *
  * @param RokMenuNodeTree $menu
  * @param                 $active_id
  *
  * @return array
  */
 protected function findActiveBranch(RokMenuNodeTree $menu, $active_id)
 {
     $active_branch = array();
     /** @var $current JoomlaRokMenuNode */
     $current = $menu->findNode($active_id);
     if ($current) {
         do {
             $active_branch[$current->getId()] = $current;
             if ($current->getParent() == self::ROOT_ID) {
                 break;
             }
         } while ($current = $current->getParentRef());
         $active_branch = array_reverse($active_branch, true);
     }
     return $active_branch;
 }