/** * Converts a $mixed value to an array of pages * * @param mixed $mixed mixed value to get page(s) from * @param bool $recursive whether $value should be looped * if it is an array or a config * @return IfwPsn_Vendor_Zend_Navigation_Page|array|null empty if unable to convert */ protected function _convertToPages($mixed, $recursive = true) { if (is_object($mixed)) { if ($mixed instanceof IfwPsn_Vendor_Zend_Navigation_Page) { // value is a page instance; return directly return $mixed; } elseif ($mixed instanceof IfwPsn_Vendor_Zend_Navigation_Container) { // value is a container; return pages in it $pages = array(); foreach ($mixed as $page) { $pages[] = $page; } return $pages; } elseif ($mixed instanceof IfwPsn_Vendor_Zend_Config) { // convert config object to array and extract return $this->_convertToPages($mixed->toArray(), $recursive); } } elseif (is_string($mixed)) { // value is a string; make an URI page return IfwPsn_Vendor_Zend_Navigation_Page::factory(array('type' => 'uri', 'uri' => $mixed)); } elseif (is_array($mixed) && !empty($mixed)) { if ($recursive && is_numeric(key($mixed))) { // first key is numeric; assume several pages $pages = array(); foreach ($mixed as $value) { if ($value = $this->_convertToPages($value, false)) { $pages[] = $value; } } return $pages; } else { // pass array to factory directly try { $page = IfwPsn_Vendor_Zend_Navigation_Page::factory($mixed); return $page; } catch (Exception $e) { } } } // nothing found return null; }
/** * Adds a page to the container * * This method will inject the container as the given page's parent by * calling {@link IfwPsn_Vendor_Zend_Navigation_Page::setParent()}. * * @param IfwPsn_Vendor_Zend_Navigation_Page|array|IfwPsn_Vendor_Zend_Config $page page to add * @return IfwPsn_Vendor_Zend_Navigation_Container fluent interface, * returns self * @throws IfwPsn_Vendor_Zend_Navigation_Exception if page is invalid */ public function addPage($page) { if ($page === $this) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Navigation/Exception.php'; throw new IfwPsn_Vendor_Zend_Navigation_Exception('A page cannot have itself as a parent'); } if (is_array($page) || $page instanceof IfwPsn_Vendor_Zend_Config) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Navigation/Page.php'; $page = IfwPsn_Vendor_Zend_Navigation_Page::factory($page); } elseif (!$page instanceof IfwPsn_Vendor_Zend_Navigation_Page) { require_once IFW_PSN_LIB_ROOT . 'IfwPsn/Vendor/Zend/Navigation/Exception.php'; throw new IfwPsn_Vendor_Zend_Navigation_Exception('Invalid argument: $page must be an instance of ' . 'IfwPsn_Vendor_Zend_Navigation_Page or IfwPsn_Vendor_Zend_Config, or an array'); } $hash = $page->hashCode(); if (array_key_exists($hash, $this->_index)) { // page is already in container return $this; } // adds page to container and sets dirty flag $this->_pages[$hash] = $page; $this->_index[$hash] = $page->getOrder(); $this->_dirtyIndex = true; // inject self as page parent $page->setParent($this); return $this; }