public function addPage($page) { if (is_array($page) || $page instanceof Config) { if (isset($page['route']) && !isset($page['type'])) { $page['type'] = "Bundle\\ZendNavigationBundle\\Page\\RouterPage"; } else { if (isset($page['uri']) && !isset($page['type'])) { $page['type'] = "Bundle\\ZendNavigationBundle\\Page\\UriPage"; } } $page = AbstractPage::factory($page); } parent::addPage($page); if ($page instanceof AbstractPage) { $page->setRouter($this->router); $page->setRequest($this->request); } return $this; }
public function testToArrayMethod() { $options = array('label' => 'foo', 'uri' => 'http://www.example.com/foo.html', 'fragment' => 'bar', 'id' => 'my-id', 'class' => 'my-class', 'title' => 'my-title', 'target' => 'my-target', 'rel' => array(), 'rev' => array(), 'order' => 100, 'active' => true, 'visible' => false, 'resource' => 'joker', 'privilege' => null, 'foo' => 'bar', 'meaning' => 42, 'pages' => array(array('label' => 'foo.bar', 'uri' => 'http://www.example.com/foo.html'), array('label' => 'foo.baz', 'uri' => 'http://www.example.com/foo.html'))); $page = AbstractPage::factory($options); $toArray = $page->toArray(); // tweak options to what we expect toArray() to contain $options['type'] = 'Zend\\Navigation\\Page\\Uri'; // calculate diff between toArray() and $options $diff = array_diff_assoc($toArray, $options); // should be no diff $this->assertEquals(array(), $diff); // $toArray should have 2 sub pages $this->assertEquals(2, count($toArray['pages'])); // tweak options to what we expect sub page 1 to be $options['label'] = 'foo.bar'; $options['fragment'] = null; $options['order'] = null; $options['id'] = null; $options['class'] = null; $options['title'] = null; $options['target'] = null; $options['resource'] = null; $options['active'] = false; $options['visible'] = true; unset($options['foo']); unset($options['meaning']); // assert that there is no diff from what we expect $subPageOneDiff = array_diff_assoc($toArray['pages'][0], $options); $this->assertEquals(array(), $subPageOneDiff); // tweak options to what we expect sub page 2 to be $options['label'] = 'foo.baz'; // assert that there is no diff from what we expect $subPageTwoDiff = array_diff_assoc($toArray['pages'][1], $options); $this->assertEquals(array(), $subPageTwoDiff); }
public function testAclFiltersAwayPagesFromPageProperty() { $acl = new Acl\Acl(); $acl->addRole(new Role\GenericRole('member')); $acl->addRole(new Role\GenericRole('admin')); $acl->addResource(new Resource\GenericResource('protected')); $acl->allow('admin', 'protected'); $this->_helper->setAcl($acl); $this->_helper->setRole($acl->getRole('member')); $samplePage = AbstractPage::factory(array( 'label' => 'An example page', 'uri' => 'http://www.example.com/', 'resource' => 'protected' )); $active = $this->_helper->findOneByLabel('Home'); $expected = array( 'alternate' => false, 'stylesheet' => false, 'start' => false, 'next' => 'Page 1', 'prev' => false, 'contents' => false, 'index' => false, 'glossary' => false, 'copyright' => false, 'chapter' => 'array(4)', 'section' => false, 'subsection' => false, 'appendix' => false, 'help' => false, 'bookmark' => false ); $actual = array(); foreach ($expected as $type => $discard) { $active->addRel($type, $samplePage); $found = $this->_helper->findRelation($active, 'rel', $type); if (null === $found) { $actual[$type] = false; } elseif (is_array($found)) { $actual[$type] = 'array(' . count($found) . ')'; } else { $actual[$type] = $found->getLabel(); } } $this->assertEquals($expected, $actual); }
public function testShouldFailIfUnableToDetermineType() { try { $page = AbstractPage::factory(array('label' => 'My Invalid Page')); } catch (Navigation\Exception $e) { return; } $this->fail('An exception has not been thrown for invalid page type'); }
/** * 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 \Zend\Navigation\AbstractPage|array|null empty if unable to convert */ protected function _convertToPages($mixed, $recursive = true) { if (is_object($mixed)) { if ($mixed instanceof AbstractPage) { // value is a page instance; return directly return $mixed; } elseif ($mixed instanceof Navigation\Container) { // value is a container; return pages in it $pages = array(); foreach ($mixed as $page) { $pages[] = $page; } return $pages; } elseif ($mixed instanceof \Zend\Config\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 AbstractPage::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 = AbstractPage::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 Zend_Navigation_Page::setParent()}. * * @param Zend_Navigation_Page|array|\Zend\Config\Config $page page to add * @return \Zend\Navigation\Container fluent interface, * returns self * @throws \Zend\Navigation\Exception if page is invalid */ public function addPage($page) { if ($page === $this) { throw new Exception('A page cannot have itself as a parent'); } if (is_array($page) || $page instanceof Config\Config) { $page = AbstractPage::factory($page); } elseif (!$page instanceof AbstractPage) { throw new Exception('Invalid argument: $page must be an instance of ' . 'Zend_Navigation_Page or 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; }
public function testGetChildrenShouldReturnTheCurrentPage() { $container = new Navigation\Navigation(); $page = AbstractPage::factory(array('type' => 'uri')); $container->addPage($page); $this->assertEquals($page, $container->getChildren()); }