/** * Convert this node to an array ready to be used by the template * * The $filter callback method signature should include the follow parameters: * * - **`$node`**: The node to be filtered * - **`$nodeArrayData`**: Node array data to be returned for template * - **`$collection`**: * - **`$sortedSiblings`**: * - **`$currentNode`**: * - **`$currentNodeAncestorPaths`**: * * @param \Navinator\Collection $collection Collection context used to convert this node to an array * @param array $sortedSiblings sorted siblings of this node includes this node (passed to avoid fetching them constantly) * @param \Navinator\Node $currentNode The node to treat as the currently navigated to node * @param array $currentNodeAncestorPaths Ancestor path of the current node (passed to avoid fetching them constantly) * @param callback $filter Function to filter nodes - see the method description for details about the method signature */ public function prepareForTemplate(\Navinator\Collection $collection, $sortedSiblings = array(), \Navinator\Node $currentNode = null, $currentNodeAncestorPaths = array(), $filter = null) { $isCurrentNodeAncestor = !empty($currentNodeAncestorPaths) && in_array($this->path, $currentNodeAncestorPaths); $isCurrentNode = !empty($currentNode) && $this->path == $currentNode->getPath(); $isCurrentRoot = $this->getDepth() == 1 && $isCurrentNodeAncestor; $isFirstChild = false; $isLastChild = false; if ($sortedSiblings) { reset($sortedSiblings); $firstKey = key($sortedSiblings); if ($this === $sortedSiblings[$firstKey]) { $isFirstChild = true; } end($sortedSiblings); $lastKey = key($sortedSiblings); if ($this === $sortedSiblings[$lastKey]) { $isLastChild = true; } } $output = array('url' => $this->url, 'path' => $this->path, 'display_name' => $this->display_name, 'template_data' => $this->template_data, 'depth' => $this->getDepth(), 'is_first_child' => $isFirstChild, 'is_last_child' => $isLastChild, 'is_current_root' => $isCurrentRoot, 'is_current' => $isCurrentNode, 'is_current_ancestor' => $isCurrentNodeAncestor, 'display_order' => $collection->getNodeDisplayOrder($this)); // if this node filters to false, return and do not handle child nodes if ($filter && $filter instanceof \Closure && !$filter($this, $output, $collection, $sortedSiblings, $currentNode, $currentNodeAncestorPaths)) { return; } $children = $this->getChildren($collection); $childArr = array(); if ($children) { $children = $collection->sortNodeArray($children); foreach ($children as $child) { $childItem = $child->prepareForTemplate($collection, $children, $currentNode, $currentNodeAncestorPaths, $filter); if (!empty($childItem)) { $childArr[] = $childItem; } } } $output['children'] = $childArr; return $output; }
if ($i !== $count) { ?> / <?php } ?> </li> <?php $i++; } ?> <?php } use Navinator\Collection; use Navinator\Node; $collection = new Collection(); // create a node passing the node's path // note: a node with path 'my-favorite-sites' has not been added to the collection yet and does not need to be $node = new Node('my-favorite-sites/google'); $node->url = 'http://google.com'; $node->display_name = 'Google Search'; $collection->addNode($node); $node = new Node('my-favorite-sites'); $node->display_name = 'My Favorite Sites'; // if $node->url is not set, the node path is used: same as $node->url = '/my-favorite-sites/'; $collection->addNode($node); // create a node object from an array $node = new Node(array('path' => 'my-favorite-sites/github', 'url' => 'http://github.com', 'display_name' => 'Github')); $collection->addNode($node); $node = new Node(array('path' => 'my-favorite-sites/github/gist', 'url' => 'http://gist.github.com')); // if $node->display_name (array key or property) is not set, the last segment of the the node path is used: same as $node->display_name = 'gist';