/** * 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; }
/** * Retrieves a node path from a node object or string * @param \Navinator\Node|string $var Node object or path string * @return \Navinator\Node */ public function getPathFromVar($var) { if ($var instanceof \Navinator\Node) { return $var->getPath(); } return $var; }