diff --git a/src/Container.php b/src/Container.php index a6e00d5..23f723d 100644 --- a/src/Container.php +++ b/src/Container.php @@ -14,7 +14,7 @@ * * @category Zend * @package Zend_Navigation - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -22,45 +22,45 @@ * @namespace */ namespace Zend\Navigation; -use Zend\Config; + +use Countable, + RecursiveIterator, + RecursiveIteratorIterator, + Traversable, + Zend\Stdlib\ArrayUtils; /** * Zend_Navigation_Container * - * Container class for Zend_Navigation_Page classes. + * Container class for Zend\Navigation\Page classes. * - * @uses Countable - * @uses RecursiveIterator - * @uses RecursiveIteratorIterator - * @uses \Zend\Navigation\InvalidArgumentException - * @uses \Zend\Navigation\AbstractPage * @category Zend * @package Zend_Navigation - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ -abstract class Container implements \RecursiveIterator, \Countable +abstract class Container implements RecursiveIterator, Countable { /** * Contains sub pages * * @var array */ - protected $_pages = array(); + protected $pages = array(); /** * An index that contains the order in which to iterate pages * * @var array */ - protected $_index = array(); + protected $index = array(); /** * Whether index is dirty and needs to be re-arranged * * @var bool */ - protected $_dirtyIndex = false; + protected $dirtyIndex = false; // Internal methods: @@ -69,26 +69,28 @@ abstract class Container implements \RecursiveIterator, \Countable * * @return void */ - protected function _sort() + protected function sort() { - if ($this->_dirtyIndex) { - $newIndex = array(); - $index = 0; - - foreach ($this->_pages as $hash => $page) { - $order = $page->getOrder(); - if ($order === null) { - $newIndex[$hash] = $index; - $index++; - } else { - $newIndex[$hash] = $order; - } - } + if (!$this->dirtyIndex) { + return; + } - asort($newIndex); - $this->_index = $newIndex; - $this->_dirtyIndex = false; + $newIndex = array(); + $index = 0; + + foreach ($this->pages as $hash => $page) { + $order = $page->getOrder(); + if ($order === null) { + $newIndex[$hash] = $index; + $index++; + } else { + $newIndex[$hash] = $order; + } } + + asort($newIndex); + $this->index = $newIndex; + $this->dirtyIndex = false; } // Public methods: @@ -100,46 +102,48 @@ protected function _sort() */ public function notifyOrderUpdated() { - $this->_dirtyIndex = true; + $this->dirtyIndex = true; } /** * 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()}. + * calling {@link Page\AbstractPage::setParent()}. * - * @param Zend_Navigation_Page|array|\Zend\Config\Config $page page to add - * @return \Zend\Navigation\Container fluent interface, - * returns self - * @throws \Zend\Navigation\InvalidArgumentException if page is invalid + * @param Page\AbstractPage|array|Traversable $page page to add + * @return Container fluent interface, returns self + * @throws Exception\InvalidArgumentException if page is invalid */ public function addPage($page) { if ($page === $this) { throw new Exception\InvalidArgumentException( - 'A page cannot have itself as a parent'); + '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\InvalidArgumentException( - 'Invalid argument: $page must be an instance of ' . - 'Zend_Navigation_Page or Zend_Config, or an array'); + if (!$page instanceof Page\AbstractPage) { + if (!is_array($page) && !$page instanceof Traversable) { + throw new Exception\InvalidArgumentException( + 'Invalid argument: $page must be an instance of ' + . 'Zend\Navigation\Page\AbstractPage or Traversable, or an array' + ); + } + $page = Page\AbstractPage::factory($page); } $hash = $page->hashCode(); - if (array_key_exists($hash, $this->_index)) { + 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; + $this->pages[$hash] = $page; + $this->index[$hash] = $page->getOrder(); + $this->dirtyIndex = true; // inject self as page parent $page->setParent($this); @@ -150,21 +154,27 @@ public function addPage($page) /** * Adds several pages at once * - * @param array|\Zend\Config\Config $pages pages to add - * @return \Zend\Navigation\Container fluent interface, returns self - * @throws \Zend\Navigation\InvalidArgumentException if $pages is not array - * or \Zend\Config\Config + * @param array|Traversable|Container $pages pages to add + * @return Container fluent interface, returns self + * @throws Exception\InvalidArgumentException if $pages is not array, + * Traversable or Container */ public function addPages($pages) { - if ($pages instanceof Config\Config) { - $pages = $pages->toArray(); + if (!is_array($pages) && !$pages instanceof Traversable) { + throw new Exception\InvalidArgumentException( + 'Invalid argument: $pages must be an array, an ' + . 'instance of Traversable or an instance of ' + . 'Zend\Navigation\Container' + ); } - if (!is_array($pages)) { - throw new Exception\InvalidArgumentException( - 'Invalid argument: $pages must be an array or an ' . - 'instance of Zend_Config'); + // Because adding a page to a container removes it from the original + // (see {@link Page\AbstractPage::setParent()}), iteration of the + // original container will break. As such, we need to iterate the + // container into an array first. + if ($pages instanceof Container) { + $pages = iterator_to_array($pages); } foreach ($pages as $page) { @@ -177,8 +187,8 @@ public function addPages($pages) /** * Sets pages this container should have, removing existing pages * - * @param array $pages pages to set - * @return \Zend\Navigation\Container fluent interface, returns self + * @param array $pages pages to set + * @return Container fluent interface, returns self */ public function setPages(array $pages) { @@ -189,38 +199,37 @@ public function setPages(array $pages) /** * Returns pages in the container * - * @return array array of \Zend\Navigation\AbstractPage instances + * @return array array of Page\AbstractPage instances */ public function getPages() { - return $this->_pages; + return $this->pages; } /** * Removes the given page from the container * - * @param \Zend\Navigation\AbstractPage|int $page page to remove, either a page - * instance or a specific page order - * @return bool whether the removal was - * successful + * @param Page\AbstractPage|int $page page to remove, either a page + * instance or a specific page order + * @return bool whether the removal was successful */ public function removePage($page) { - if ($page instanceof AbstractPage) { + if ($page instanceof Page\AbstractPage) { $hash = $page->hashCode(); } elseif (is_int($page)) { - $this->_sort(); - if (!$hash = array_search($page, $this->_index)) { + $this->sort(); + if (!$hash = array_search($page, $this->index)) { return false; } } else { return false; } - if (isset($this->_pages[$hash])) { - unset($this->_pages[$hash]); - unset($this->_index[$hash]); - $this->_dirtyIndex = true; + if (isset($this->pages[$hash])) { + unset($this->pages[$hash]); + unset($this->index[$hash]); + $this->dirtyIndex = true; return true; } @@ -230,29 +239,29 @@ public function removePage($page) /** * Removes all pages in container * - * @return \Zend\Navigation\Container fluent interface, returns self + * @return Container fluent interface, returns self */ public function removePages() { - $this->_pages = array(); - $this->_index = array(); + $this->pages = array(); + $this->index = array(); return $this; } /** * Checks if the container has the given page * - * @param \Zend\Navigation\AbstractPage $page page to look for - * @param bool $recursive [optional] whether to search - * recursively. Default is false. - * @return bool whether page is in container + * @param Page\AbstractPage $page page to look for + * @param bool $recursive [optional] whether to search recursively. + * Default is false. + * @return bool whether page is in container */ - public function hasPage(AbstractPage $page, $recursive = false) + public function hasPage(Page\AbstractPage $page, $recursive = false) { - if (array_key_exists($page->hashCode(), $this->_index)) { + if (array_key_exists($page->hashCode(), $this->index)) { return true; } elseif ($recursive) { - foreach ($this->_pages as $childPage) { + foreach ($this->pages as $childPage) { if ($childPage->hasPage($page, true)) { return true; } @@ -269,20 +278,19 @@ public function hasPage(AbstractPage $page, $recursive = false) */ public function hasPages() { - return count($this->_index) > 0; + return count($this->index) > 0; } /** * Returns a child page matching $property == $value, or null if not found * - * @param string $property name of property to match against - * @param mixed $value value to match property against - * @return \Zend\Navigation\AbstractPage|null matching page or null + * @param string $property name of property to match against + * @param mixed $value value to match property against + * @return Page\AbstractPage|null matching page or null */ public function findOneBy($property, $value) { - $iterator = new \RecursiveIteratorIterator($this, - \RecursiveIteratorIterator::SELF_FIRST); + $iterator = new RecursiveIteratorIterator($this, RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $page) { if ($page->get($property) == $value) { @@ -299,15 +307,13 @@ public function findOneBy($property, $value) * * @param string $property name of property to match against * @param mixed $value value to match property against - * @return array array containing only \Zend\Navigation\AbstractPage - * instances + * @return array array containing only Page\AbstractPage instances */ public function findAllBy($property, $value) { $found = array(); - $iterator = new \RecursiveIteratorIterator($this, - \RecursiveIteratorIterator::SELF_FIRST); + $iterator = new RecursiveIteratorIterator($this, RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $page) { if ($page->get($property) == $value) { @@ -329,7 +335,7 @@ public function findAllBy($property, $value) * matching pages are found. If false, null will * be returned if no matching page is found. * Default is false. - * @return \Zend\Navigation\AbstractPage|null matching page or null + * @return Page\AbstractPage|null matching page or null */ public function findBy($property, $value, $all = false) { @@ -353,7 +359,7 @@ public function findBy($property, $value, $all = false) * * @param string $method method name * @param array $arguments method arguments - * @throws \Zend\Navigation\InvalidArgumentException if method does not exist + * @throws Exception\BadMethodCallException if method does not exist */ public function __call($method, $arguments) { @@ -361,10 +367,13 @@ public function __call($method, $arguments) return $this->{$match[1]}($match[2], $arguments[0]); } - throw new Exception\BadMethodCallException(sprintf( + throw new Exception\BadMethodCallException( + sprintf( 'Bad method call: Unknown method %s::%s', get_class($this), - $method)); + $method + ) + ); } /** @@ -374,13 +383,11 @@ public function __call($method, $arguments) */ public function toArray() { - $pages = array(); - - $this->_dirtyIndex = true; - $this->_sort(); - $indexes = array_keys($this->_index); + $this->sort(); + $pages = array(); + $indexes = array_keys($this->index); foreach ($indexes as $hash) { - $pages[] = $this->_pages[$hash]->toArray(); + $pages[] = $this->pages[$hash]->toArray(); } return $pages; } @@ -392,22 +399,23 @@ public function toArray() * * Implements RecursiveIterator interface. * - * @return \Zend\Navigation\AbstractPage current page or null - * @throws \Zend\Navigation\InvalidArgumentException if the index is invalid + * @return Page\AbstractPage current page or null + * @throws Exception\OutOfBoundsException if the index is invalid */ public function current() { - $this->_sort(); - current($this->_index); - $hash = key($this->_index); + $this->sort(); - if (isset($this->_pages[$hash])) { - return $this->_pages[$hash]; - } else { + current($this->index); + $hash = key($this->index); + if (!isset($this->pages[$hash])) { throw new Exception\OutOfBoundsException( - 'Corruption detected in container; ' . - 'invalid key found in internal iterator'); + 'Corruption detected in container; ' + . 'invalid key found in internal iterator' + ); } + + return $this->pages[$hash]; } /** @@ -419,8 +427,8 @@ public function current() */ public function key() { - $this->_sort(); - return key($this->_index); + $this->sort(); + return key($this->index); } /** @@ -432,8 +440,8 @@ public function key() */ public function next() { - $this->_sort(); - next($this->_index); + $this->sort(); + next($this->index); } /** @@ -445,8 +453,8 @@ public function next() */ public function rewind() { - $this->_sort(); - reset($this->_index); + $this->sort(); + reset($this->index); } /** @@ -458,8 +466,8 @@ public function rewind() */ public function valid() { - $this->_sort(); - return current($this->_index) !== false; + $this->sort(); + return current($this->index) !== false; } /** @@ -479,14 +487,14 @@ public function hasChildren() * * Implements RecursiveIterator interface. * - * @return \Zend\Navigation\AbstractPage|null + * @return Page\AbstractPage|null */ public function getChildren() { - $hash = key($this->_index); + $hash = key($this->index); - if (isset($this->_pages[$hash])) { - return $this->_pages[$hash]; + if (isset($this->pages[$hash])) { + return $this->pages[$hash]; } return null; @@ -503,6 +511,6 @@ public function getChildren() */ public function count() { - return count($this->_index); + return count($this->index); } } diff --git a/src/Exception.php b/src/Exception.php index c4a4e46..0b87a8c 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -12,10 +12,10 @@ * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * - * @category Zend - * @package Zend_Navigation - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) - * @license http://framework.zend.com/license/new-bsd New BSD License + * @category Zend + * @package Zend_Navigation + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License */ /** @@ -28,7 +28,7 @@ * * @category Zend * @package Zend_Navigation - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ interface Exception diff --git a/src/Exception/BadMethodCallException.php b/src/Exception/BadMethodCallException.php index 8afa363..312f590 100644 --- a/src/Exception/BadMethodCallException.php +++ b/src/Exception/BadMethodCallException.php @@ -1,10 +1,36 @@ addPages($pages); - } elseif (null !== $pages) { + if ($pages && (!is_array($pages) && !$pages instanceof Traversable)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $pages must be an array, an ' . - 'instance of Zend_Config, or null'); + 'Invalid argument: $pages must be an array, an ' + . 'instance of Traversable, or null' + ); + } + + if ($pages) { + $this->addPages($pages); } } } diff --git a/src/AbstractPage.php b/src/Page/AbstractPage.php similarity index 55% rename from src/AbstractPage.php rename to src/Page/AbstractPage.php index 1d5d106..b773595 100644 --- a/src/AbstractPage.php +++ b/src/Page/AbstractPage.php @@ -12,31 +12,29 @@ * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * - * @category Zend - * @package Zend_Navigation - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) - * @license http://framework.zend.com/license/new-bsd New BSD License + * @category Zend + * @package Zend_Navigation + * @subpackage Page + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License */ -/** - * @namespace - */ -namespace Zend\Navigation; +namespace Zend\Navigation\Page; -use Zend\Config\Config; +use Traversable, + Zend\Acl\Resource as AclResource, + Zend\Navigation\Container, + Zend\Navigation\Exception, + Zend\Stdlib\ArrayUtils; /** - * Base class for Zend_Navigation_Page pages + * Base class for Zend\Navigation\Page pages * - * @uses \Zend\Loader - * @uses \Zend\Navigation\Container - * @uses \Zend\Navigation\InvalidArgumentException - * @uses \Zend\Navigation\Page\Mvc - * @uses \Zend\Navigation\Page\Uri - * @category Zend - * @package Zend_Navigation - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) - * @license http://framework.zend.com/license/new-bsd New BSD License + * @category Zend + * @package Zend_Navigation + * @subpackage Page + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) + * @license http://framework.zend.com/license/new-bsd New BSD License */ abstract class AbstractPage extends Container { @@ -45,49 +43,49 @@ abstract class AbstractPage extends Container * * @var string|null */ - protected $_label; + protected $label; /** * Fragment identifier (anchor identifier) - * - * The fragment identifier (anchor identifier) pointing to an anchor within + * + * The fragment identifier (anchor identifier) pointing to an anchor within * a resource that is subordinate to another, primary resource. * The fragment identifier introduced by a hash mark "#". * Example: http://www.example.org/foo.html#bar ("bar" is the fragment identifier) - * + * * @link http://www.w3.org/TR/html401/intro/intro.html#fragment-uri - * + * * @var string|null */ - protected $_fragment; + protected $fragment; /** * Page id * * @var string|null */ - protected $_id; + protected $id; /** * Style class for this page (CSS) * * @var string|null */ - protected $_class; + protected $class; /** * A more descriptive title for this page * * @var string|null */ - protected $_title; + protected $title; /** * This page's target * * @var string|null */ - protected $_target; + protected $target; /** * Forward links to other pages @@ -96,7 +94,7 @@ abstract class AbstractPage extends Container * * @var array */ - protected $_rel = array(); + protected $rel = array(); /** * Reverse links to other pages @@ -105,56 +103,56 @@ abstract class AbstractPage extends Container * * @var array */ - protected $_rev = array(); + protected $rev = array(); /** * Page order used by parent container * * @var int|null */ - protected $_order; + protected $order; /** * ACL resource associated with this page * * @var string|\Zend\Acl\Resource|null */ - protected $_resource; + protected $resource; /** * ACL privilege associated with this page * * @var string|null */ - protected $_privilege; + protected $privilege; /** * Whether this page should be considered active * * @var bool */ - protected $_active = false; + protected $active = false; /** * Whether this page should be considered visible * * @var bool */ - protected $_visible = true; + protected $visible = true; /** * Parent container * * @var \Zend\Navigation\Container|null */ - protected $_parent; + protected $parent; /** * Custom page properties, used by __set(), __get() and __isset() * * @var array */ - protected $_properties = array(); + protected $properties = array(); // Initialization: @@ -170,35 +168,35 @@ abstract class AbstractPage extends Container * If 'type' is not given, the type of page to construct will be determined * by the following rules: * - If $options contains either of the keys 'action', 'controller', - * 'module', or 'route', a Zend_Navigation_Page_Mvc page will be created. + * or 'route', a Zend_Navigation_Page_Mvc page will be created. * - If $options contains the key 'uri', a Zend_Navigation_Page_Uri page * will be created. * - * @param array|\Zend\Config\Config $options options used for creating page - * @return \Zend\Navigation\AbstractPage a page instance - * @throws \Zend\Navigation\InvalidArgumentException if $options is not - * array/\Zend\Config\Config - * @throws \Zend\Navigation\InvalidArgumentException if 'type' is specified - * and Zend_Loader is unable - * to load the class - * @throws \Zend\Navigation\InvalidArgumentException if something goes wrong - * during instantiation of - * the page - * @throws \Zend\Navigation\InvalidArgumentException if 'type' is given, and - * the specified type does - * not extend this class - * @throws \Zend\Navigation\InvalidArgumentException if unable to determine - * which class to instantiate + * @param array|Traversable $options options used for creating page + * @return AbstractPage a page instance + * @throws Exception\InvalidArgumentException if $options is not + * array/Traversable + * @throws Exception\InvalidArgumentException if 'type' is specified + * but class not found + * @throws Exception\InvalidArgumentException if something goes wrong + * during instantiation of + * the page + * @throws Exception\InvalidArgumentException if 'type' is given, and + * the specified type does + * not extend this class + * @throws Exception\InvalidArgumentException if unable to determine + * which class to instantiate */ public static function factory($options) { - if ($options instanceof Config) { - $options = $options->toArray(); + if ($options instanceof Traversable) { + $options = ArrayUtils::iteratorToArray($options); } if (!is_array($options)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $options must be an array or Zend\Config\Config'); + 'Invalid argument: $options must be an array or Traversable' + ); } if (isset($options['type'])) { @@ -214,51 +212,58 @@ public static function factory($options) } if (!class_exists($type, true)) { - throw new Exception\InvalidArgumentException('Cannot find class ' . $type); + throw new Exception\InvalidArgumentException( + 'Cannot find class ' . $type + ); } $page = new $type($options); if (!$page instanceof self) { - throw new Exception\InvalidArgumentException(sprintf( + throw new Exception\InvalidArgumentException( + sprintf( 'Invalid argument: Detected type "%s", which ' . - 'is not an instance of Zend_Navigation_Page', - $type)); + 'is not an instance of Zend\Navigation\Page', + $type + ) + ); } return $page; } } $hasUri = isset($options['uri']); - $hasMvc = isset($options['action']) || isset($options['controller']) || - isset($options['module']) || isset($options['route']); + $hasMvc = isset($options['action']) || isset($options['controller']) + || isset($options['route']); if ($hasMvc) { - return new Page\Mvc($options); + return new Mvc($options); } elseif ($hasUri) { - return new Page\Uri($options); + return new Uri($options); } else { throw new Exception\InvalidArgumentException( - 'Invalid argument: Unable to determine class to instantiate'); + 'Invalid argument: Unable to determine class to instantiate' + ); } } /** * Page constructor * - * @param array|\Zend\Config\Config $options [optional] page options. Default is - * null, which should set defaults. - * @throws \Zend\Navigation\Exception if invalid options are given + * @param array|Traversable $options [optional] page options. Default is + * null, which should set defaults. + * @throws Exception if invalid options are given */ public function __construct($options = null) { + if ($options instanceof Traversable) { + $options = ArrayUtils::iteratorToArray($options); + } if (is_array($options)) { $this->setOptions($options); - } elseif ($options instanceof Config) { - $this->setConfig($options); } // do custom initialization - $this->_init(); + $this->init(); } /** @@ -266,20 +271,8 @@ public function __construct($options = null) * * @return void */ - protected function _init() - { - } - - /** - * Sets page properties using a Zend_Config object - * - * @param \Zend\Config\Config $config config object to get properties from - * @return \Zend\Navigation\AbstractPage fluent interface, returns self - * @throws \Zend\Navigation\InvalidArgumentException if invalid options are given - */ - public function setConfig(Config $config) + protected function init() { - return $this->setOptions($config->toArray()); } /** @@ -290,9 +283,9 @@ public function setConfig(Config $config) * corresponds to setTarget(), and the option 'reset_params' corresponds to * the method setResetParams(). * - * @param array $options associative array of options to set - * @return \Zend\Navigation\Page\Page fluent interface, returns self - * @throws \Zend\Navigation\InvalidArgumentException if invalid options are given + * @param array $options associative array of options to set + * @return AbstractPage fluent interface, returns self + * @throws Exception\InvalidArgumentException if invalid options are given */ public function setOptions(array $options) { @@ -308,18 +301,19 @@ public function setOptions(array $options) /** * Sets page label * - * @param string $label new page label - * @return \Zend\Navigation\Page\Page fluent interface, returns self - * @throws \Zend\Navigation\InvalidArgumentException if empty/no string is given + * @param string $label new page label + * @return AbstractPage fluent interface, returns self + * @throws Exception\InvalidArgumentException if empty/no string is given */ public function setLabel($label) { if (null !== $label && !is_string($label)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $label must be a string or null'); + 'Invalid argument: $label must be a string or null' + ); } - $this->_label = $label; + $this->label = $label; return $this; } @@ -330,53 +324,55 @@ public function setLabel($label) */ public function getLabel() { - return $this->_label; + return $this->label; } /** * Sets a fragment identifier * - * @param string $fragment new fragment identifier - * @return Zend_Navigation_Page fluent interface, returns self - * @throws Zend_Navigation_Exception if empty/no string is given + * @param string $fragment new fragment identifier + * @return AbstractPage fluent interface, returns self + * @throws Exception\InvalidArgumentException if empty/no string is given */ public function setFragment($fragment) { if (null !== $fragment && !is_string($fragment)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $fragment must be a string or null'); + 'Invalid argument: $fragment must be a string or null' + ); } - - $this->_fragment = $fragment; + + $this->fragment = $fragment; return $this; } - - /** + + /** * Returns fragment identifier * * @return string|null fragment identifier */ public function getFragment() { - return $this->_fragment; + return $this->fragment; } /** * Sets page id * - * @param string|null $id [optional] id to set. Default is null, - * which sets no id. - * @return \Zend\Navigation\AbstractPage fluent interface, returns self - * @throws \Zend\Navigation\InvalidArgumentException if not given string or null + * @param string|null $id [optional] id to set. Default is null, + * which sets no id. + * @return AbstractPage fluent interface, returns self + * @throws Exception\InvalidArgumentException if not given string or null */ public function setId($id = null) { if (null !== $id && !is_string($id) && !is_numeric($id)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $id must be a string, number or null'); + 'Invalid argument: $id must be a string, number or null' + ); } - $this->_id = null === $id ? $id : (string) $id; + $this->id = null === $id ? $id : (string) $id; return $this; } @@ -388,25 +384,26 @@ public function setId($id = null) */ public function getId() { - return $this->_id; + return $this->id; } /** * Sets page CSS class * - * @param string|null $class [optional] CSS class to set. Default - * is null, which sets no CSS class. - * @return \Zend\Navigation\AbstractPage fluent interface, returns self - * @throws \Zend\Navigation\InvalidArgumentException if not given string or null + * @param string|null $class [optional] CSS class to set. Default + * is null, which sets no CSS class. + * @return AbstractPage fluent interface, returns self + * @throws Exception\InvalidArgumentException if not given string or null */ public function setClass($class = null) { if (null !== $class && !is_string($class)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $class must be a string or null'); + 'Invalid argument: $class must be a string or null' + ); } - $this->_class = $class; + $this->class = $class; return $this; } @@ -417,25 +414,26 @@ public function setClass($class = null) */ public function getClass() { - return $this->_class; + return $this->class; } /** * Sets page title * - * @param string $title [optional] page title. Default is - * null, which sets no title. - * @return \Zend\Navigation\AbstractPage fluent interface, returns self - * @throws \Zend\Navigation\InvalidArgumentException if not given string or null + * @param string $title [optional] page title. Default is + * null, which sets no title. + * @return AbstractPage fluent interface, returns self + * @throws Exception\InvalidArgumentException if not given string or null */ public function setTitle($title = null) { if (null !== $title && !is_string($title)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $title must be a non-empty string'); + 'Invalid argument: $title must be a non-empty string' + ); } - $this->_title = $title; + $this->title = $title; return $this; } @@ -446,25 +444,27 @@ public function setTitle($title = null) */ public function getTitle() { - return $this->_title; + return $this->title; } /** * Sets page target * - * @param string|null $target [optional] target to set. Default is - * null, which sets no target. - * @return \Zend\Navigation\AbstractPage fluent interface, returns self - * @throws \Zend\Navigation\InvalidArgumentException if target is not string or null + * @param string|null $target [optional] target to set. Default is + * null, which sets no target. + * + * @return AbstractPage fluent interface, returns self + * @throws Exception\InvalidArgumentException if target is not string or null */ public function setTarget($target = null) { if (null !== $target && !is_string($target)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $target must be a string or null'); + 'Invalid argument: $target must be a string or null' + ); } - $this->_target = $target; + $this->target = $target; return $this; } @@ -475,7 +475,7 @@ public function setTarget($target = null) */ public function getTarget() { - return $this->_target; + return $this->target; } /** @@ -486,28 +486,29 @@ public function getTarget() * prev, next, help, etc), and the value is a mixed value that could somehow * be considered a page. * - * @param array|\Zend\Config\Config $relations [optional] an associative array of - * forward links to other pages - * @return \Zend\Navigation\AbstractPage fluent interface, returns self + * @param array|Traversable $relations [optional] an associative array of + * forward links to other pages + * @return AbstractPage fluent interface, returns self */ public function setRel($relations = null) { - $this->_rel = array(); + $this->rel = array(); if (null !== $relations) { - if ($relations instanceof Config) { - $relations = $relations->toArray(); + if ($relations instanceof Traversable) { + $relations = ArrayUtils::iteratorToArray($relations); } if (!is_array($relations)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $relations must be an ' . - 'array or an instance of Zend\Config'); + 'Invalid argument: $relations must be an ' . + 'array or an instance of Traversable' + ); } foreach ($relations as $name => $relation) { if (is_string($name)) { - $this->_rel[$name] = $relation; + $this->rel[$name] = $relation; } } } @@ -523,21 +524,21 @@ public function setRel($relations = null) * prev, next, help, etc), and the value is a mixed value that could somehow * be considered a page. * - * @param string $relation [optional] name of relation to return. If not - * given, all relations will be returned. - * @return array an array of relations. If $relation is not - * specified, all relations will be returned in - * an associative array. + * @param string $relation [optional] name of relation to return. If not + * given, all relations will be returned. + * @return array an array of relations. If $relation is not + * specified, all relations will be returned in + * an associative array. */ public function getRel($relation = null) { if (null !== $relation) { - return isset($this->_rel[$relation]) ? - $this->_rel[$relation] : - null; + return isset($this->rel[$relation]) + ? $this->rel[$relation] + : null; } - return $this->_rel; + return $this->rel; } /** @@ -548,28 +549,30 @@ public function getRel($relation = null) * prev, next, help, etc), and the value is a mixed value that could somehow * be considered a page. * - * @param array|\Zend\Config\Config $relations [optional] an associative array of - * reverse links to other pages - * @return \Zend\Navigation\AbstractPage fluent interface, returns self + * @param array|Traversable $relations [optional] an associative array of + * reverse links to other pages + * + * @return AbstractPage fluent interface, returns self */ public function setRev($relations = null) { - $this->_rev = array(); + $this->rev = array(); if (null !== $relations) { - if ($relations instanceof Config) { - $relations = $relations->toArray(); + if ($relations instanceof Traversable) { + $relations = ArrayUtils::iteratorToArray($relations); } if (!is_array($relations)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $relations must be an ' . - 'array or an instance of Zend\Config'); + 'Invalid argument: $relations must be an ' . + 'array or an instance of Traversable' + ); } foreach ($relations as $name => $relation) { if (is_string($name)) { - $this->_rev[$name] = $relation; + $this->rev[$name] = $relation; } } } @@ -587,6 +590,7 @@ public function setRev($relations = null) * * @param string $relation [optional] name of relation to return. If not * given, all relations will be returned. + * * @return array an array of relations. If $relation is not * specified, all relations will be returned in * an associative array. @@ -594,22 +598,24 @@ public function setRev($relations = null) public function getRev($relation = null) { if (null !== $relation) { - return isset($this->_rev[$relation]) ? - $this->_rev[$relation] : - null; + return isset($this->rev[$relation]) + ? + $this->rev[$relation] + : + null; } - return $this->_rev; + return $this->rev; } /** * Sets page order to use in parent container * - * @param int $order [optional] page order in container. - * Default is null, which sets no - * specific order. - * @return \Zend\Navigation\AbstractPage fluent interface, returns self - * @throws \Zend\Navigation\InvalidArgumentException if order is not integer or null + * @param int $order [optional] page order in container. + * Default is null, which sets no + * specific order. + * @return AbstractPage fluent interface, returns self + * @throws Exception\InvalidArgumentException if order is not integer or null */ public function setOrder($order = null) { @@ -622,15 +628,16 @@ public function setOrder($order = null) if (null !== $order && !is_int($order)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $order must be an integer or null, ' . - 'or a string that casts to an integer'); + 'Invalid argument: $order must be an integer or null, ' . + 'or a string that casts to an integer' + ); } - $this->_order = $order; + $this->order = $order; // notify parent, if any - if (isset($this->_parent)) { - $this->_parent->notifyOrderUpdated(); + if (isset($this->parent)) { + $this->parent->notifyOrderUpdated(); } return $this; @@ -643,31 +650,30 @@ public function setOrder($order = null) */ public function getOrder() { - return $this->_order; + return $this->order; } /** * Sets ACL resource assoicated with this page * - * @param string|\Zend\Acl\Resource $resource [optional] resource - * to associate with - * page. Default is - * null, which sets no - * resource. - * @throws \Zend\Navigation\InvalidArgumentException if $resource is - * invalid - * @return \Zend\Navigation\AbstractPage fluent interface, - * returns self + * @param string|AclResource $resource [optional] resource to associate + * with page. Default is null, which + * sets no resource. + * @return AbstractPage fluent interface, returns self + * @throws Exception\InvalidArgumentException if $resource is invalid */ public function setResource($resource = null) { - if (null === $resource || is_string($resource) || - $resource instanceof \Zend\Acl\Resource) { - $this->_resource = $resource; + if (null === $resource + || is_string($resource) + || $resource instanceof AclResource + ) { + $this->resource = $resource; } else { throw new Exception\InvalidArgumentException( - 'Invalid argument: $resource must be null, a string, ' . - ' or an instance of Zend_Acl_Resource_Interface'); + 'Invalid argument: $resource must be null, a string, ' . + 'or an instance of Zend\Acl\Resource' + ); } return $this; @@ -676,11 +682,11 @@ public function setResource($resource = null) /** * Returns ACL resource assoicated with this page * - * @return string|\Zend\Acl\Resource|null ACL resource or null + * @return string|AclResource|null ACL resource or null */ public function getResource() { - return $this->_resource; + return $this->resource; } /** @@ -689,11 +695,12 @@ public function getResource() * @param string|null $privilege [optional] ACL privilege to associate * with this page. Default is null, which * sets no privilege. - * @return \Zend\Navigation\AbstractPage fluent interface, returns self + * + * @return AbstractPage fluent interface, returns self */ public function setPrivilege($privilege = null) { - $this->_privilege = is_string($privilege) ? $privilege : null; + $this->privilege = is_string($privilege) ? $privilege : null; return $this; } @@ -704,19 +711,20 @@ public function setPrivilege($privilege = null) */ public function getPrivilege() { - return $this->_privilege; + return $this->privilege; } /** * Sets whether page should be considered active or not * - * @param bool $active [optional] whether page should be - * considered active or not. Default is true. - * @return \Zend\Navigation\AbstractPage fluent interface, returns self + * @param bool $active [optional] whether page should be + * considered active or not. Default is true. + * + * @return AbstractPage fluent interface, returns self */ public function setActive($active = true) { - $this->_active = (bool) $active; + $this->active = (bool) $active; return $this; } @@ -730,8 +738,8 @@ public function setActive($active = true) */ public function isActive($recursive = false) { - if (!$this->_active && $recursive) { - foreach ($this->_pages as $page) { + if (!$this->active && $recursive) { + foreach ($this->pages as $page) { if ($page->isActive(true)) { return true; } @@ -739,7 +747,7 @@ public function isActive($recursive = false) return false; } - return $this->_active; + return $this->active; } /** @@ -748,6 +756,7 @@ public function isActive($recursive = false) * @param bool $recursive [optional] whether page should be considered * active if any child pages are active. Default * is false. + * * @return bool whether page should be considered active */ public function getActive($recursive = false) @@ -758,16 +767,16 @@ public function getActive($recursive = false) /** * Sets whether the page should be visible or not * - * @param bool $visible [optional] whether page should be - * considered visible or not. Default is true. - * @return \Zend\Navigation\AbstractPage fluent interface, returns self + * @param bool $visible [optional] whether page should be + * considered visible or not. Default is true. + * @return AbstractPage fluent interface, returns self */ public function setVisible($visible = true) { if (is_string($visible) && 'false' == strtolower($visible)) { $visible = false; } - $this->_visible = (bool) $visible; + $this->visible = (bool)$visible; return $this; } @@ -777,18 +786,21 @@ public function setVisible($visible = true) * @param bool $recursive [optional] whether page should be considered * invisible if parent is invisible. Default is * false. + * * @return bool whether page should be considered visible */ public function isVisible($recursive = false) { - if ($recursive && isset($this->_parent) && - $this->_parent instanceof self) { - if (!$this->_parent->isVisible(true)) { + if ($recursive + && isset($this->parent) + && $this->parent instanceof self + ) { + if (!$this->parent->isVisible(true)) { return false; } } - return $this->_visible; + return $this->visible; } /** @@ -799,6 +811,7 @@ public function isVisible($recursive = false) * @param bool $recursive [optional] whether page should be considered * invisible if parent is invisible. Default is * false. + * * @return bool whether page should be considered visible */ public function getVisible($recursive = false) @@ -809,34 +822,34 @@ public function getVisible($recursive = false) /** * Sets parent container * - * @param \Zend\Navigation\Container $parent [optional] new parent to set. - * Default is null which will set - * no parent. - * @return \Zend\Navigation\AbstractPage fluent interface, returns self + * @param Container $parent [optional] new parent to set. + * Default is null which will set no parent. + * @return AbstractPage fluent interface, returns self */ public function setParent(Container $parent = null) { if ($parent === $this) { throw new Exception\InvalidArgumentException( - 'A page cannot have itself as a parent'); + 'A page cannot have itself as a parent' + ); } // return if the given parent already is parent - if ($parent === $this->_parent) { + if ($parent === $this->parent) { return $this; } // remove from old parent - if (null !== $this->_parent) { - $this->_parent->removePage($this); + if (null !== $this->parent) { + $this->parent->removePage($this); } // set new parent - $this->_parent = $parent; + $this->parent = $parent; // add to parent if page and not already a child - if (null !== $this->_parent && !$this->_parent->hasPage($this, false)) { - $this->_parent->addPage($this); + if (null !== $this->parent && !$this->parent->hasPage($this, false)) { + $this->parent->addPage($this); } return $this; @@ -845,11 +858,11 @@ public function setParent(Container $parent = null) /** * Returns parent container * - * @return \Zend\Navigation\Container|null parent container or null + * @return Container|null parent container or null */ public function getParent() { - return $this->_parent; + return $this->parent; } /** @@ -858,25 +871,27 @@ public function getParent() * If the given property is native (id, class, title, etc), the matching * set method will be used. Otherwise, it will be set as a custom property. * - * @param string $property property name - * @param mixed $value value to set - * @return \Zend\Navigation\AbstractPage fluent interface, returns self - * @throws \Zend\Navigation\InvalidArgumentException if property name is invalid + * @param string $property property name + * @param mixed $value value to set + * @return AbstractPage fluent interface, returns self + * @throws Exception\InvalidArgumentException if property name is invalid */ public function set($property, $value) { if (!is_string($property) || empty($property)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $property must be a non-empty string'); + 'Invalid argument: $property must be a non-empty string' + ); } - $method = 'set' . self::_normalizePropertyName($property); + $method = 'set' . self::normalizePropertyName($property); - if ($method != 'setOptions' && $method != 'setConfig' && - method_exists($this, $method)) { + if ($method != 'setOptions' && $method != 'setConfig' + && method_exists($this, $method) + ) { $this->$method($value); } else { - $this->_properties[$property] = $value; + $this->properties[$property] = $value; } return $this; @@ -889,23 +904,24 @@ public function set($property, $value) * get method will be used. Otherwise, it will return the matching custom * property, or null if not found. * - * @param string $property property name - * @return mixed the property's value or null - * @throws \Zend\Navigation\InvalidArgumentException if property name is invalid + * @param string $property property name + * @return mixed the property's value or null + * @throws Exception\InvalidArgumentException if property name is invalid */ public function get($property) { if (!is_string($property) || empty($property)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $property must be a non-empty string'); + 'Invalid argument: $property must be a non-empty string' + ); } - $method = 'get' . self::_normalizePropertyName($property); + $method = 'get' . self::normalizePropertyName($property); if (method_exists($this, $method)) { return $this->$method(); - } elseif (isset($this->_properties[$property])) { - return $this->_properties[$property]; + } elseif (isset($this->properties[$property])) { + return $this->properties[$property]; } return null; @@ -918,10 +934,10 @@ public function get($property) * * Magic overload for enabling $page->propname = $value. * - * @param string $name property name - * @param mixed $value value to set + * @param string $name property name + * @param mixed $value value to set * @return void - * @throws \Zend\Navigation\InvalidArgumentException if property name is invalid + * @throws Exception\InvalidArgumentException if property name is invalid */ public function __set($name, $value) { @@ -933,9 +949,9 @@ public function __set($name, $value) * * Magic overload for enabling $page->propname. * - * @param string $name property name - * @return mixed property value or null - * @throws \Zend\Navigation\InvalidArgumentException if property name is invalid + * @param string $name property name + * @return mixed property value or null + * @throws Exception\InvalidArgumentException if property name is invalid */ public function __get($name) { @@ -951,17 +967,17 @@ public function __get($name) * true or false if it's a custom property (depending on whether the * property actually is set). * - * @param string $name property name - * @return bool whether the given property exists + * @param string $name property name + * @return bool whether the given property exists */ public function __isset($name) { - $method = 'get' . self::_normalizePropertyName($name); + $method = 'get' . self::normalizePropertyName($name); if (method_exists($this, $method)) { return true; } - return isset($this->_properties[$name]); + return isset($this->properties[$name]); } /** @@ -969,21 +985,24 @@ public function __isset($name) * * Magic overload for enabling unset($page->propname). * - * @param string $name property name + * @param string $name property name * @return void - * @throws \Zend\Navigation\InvalidArgumentException if the property is native + * @throws Exception\InvalidArgumentException if the property is native */ public function __unset($name) { - $method = 'set' . self::_normalizePropertyName($name); + $method = 'set' . self::normalizePropertyName($name); if (method_exists($this, $method)) { - throw new Exception\InvalidArgumentException(sprintf( + throw new Exception\InvalidArgumentException( + sprintf( 'Unsetting native property "%s" is not allowed', - $name)); + $name + ) + ); } - if (isset($this->_properties[$name])) { - unset($this->_properties[$name]); + if (isset($this->properties[$name])) { + unset($this->properties[$name]); } } @@ -996,7 +1015,7 @@ public function __unset($name) */ public function __toString() { - return $this->_label; + return $this->label; } // Public methods: @@ -1004,15 +1023,15 @@ public function __toString() /** * Adds a forward relation to the page * - * @param string $relation relation name (e.g. alternate, glossary, - * canonical, etc) - * @param mixed $value value to set for relation - * @return \Zend\Navigation\AbstractPage fluent interface, returns self + * @param string $relation relation name (e.g. alternate, glossary, + * canonical, etc) + * @param mixed $value value to set for relation + * @return AbstractPage fluent interface, returns self */ public function addRel($relation, $value) { if (is_string($relation)) { - $this->_rel[$relation] = $value; + $this->rel[$relation] = $value; } return $this; } @@ -1020,15 +1039,15 @@ public function addRel($relation, $value) /** * Adds a reverse relation to the page * - * @param string $relation relation name (e.g. alternate, glossary, - * canonical, etc) - * @param mixed $value value to set for relation - * @return \Zend\Navigation\AbstractPage fluent interface, returns self + * @param string $relation relation name (e.g. alternate, glossary, + * canonical, etc) + * @param mixed $value value to set for relation + * @return AbstractPage fluent interface, returns self */ public function addRev($relation, $value) { if (is_string($relation)) { - $this->_rev[$relation] = $value; + $this->rev[$relation] = $value; } return $this; } @@ -1036,13 +1055,13 @@ public function addRev($relation, $value) /** * Removes a forward relation from the page * - * @param string $relation name of relation to remove - * @return \Zend\Navigation\AbstractPage fluent interface, returns self + * @param string $relation name of relation to remove + * @return AbstractPage fluent interface, returns self */ public function removeRel($relation) { - if (isset($this->_rel[$relation])) { - unset($this->_rel[$relation]); + if (isset($this->rel[$relation])) { + unset($this->rel[$relation]); } return $this; @@ -1051,13 +1070,13 @@ public function removeRel($relation) /** * Removes a reverse relation from the page * - * @param string $relation name of relation to remove - * @return \Zend\Navigation\AbstractPage fluent interface, returns self + * @param string $relation name of relation to remove + * @return AbstractPage fluent interface, returns self */ public function removeRev($relation) { - if (isset($this->_rev[$relation])) { - unset($this->_rev[$relation]); + if (isset($this->rev[$relation])) { + unset($this->rev[$relation]); } return $this; @@ -1070,7 +1089,7 @@ public function removeRev($relation) */ public function getDefinedRel() { - return array_keys($this->_rel); + return array_keys($this->rel); } /** @@ -1080,7 +1099,7 @@ public function getDefinedRel() */ public function getDefinedRev() { - return array_keys($this->_rev); + return array_keys($this->rev); } /** @@ -1090,7 +1109,7 @@ public function getDefinedRev() */ public function getCustomProperties() { - return $this->_properties; + return $this->properties; } /** @@ -1110,25 +1129,23 @@ public final function hashCode() */ public function toArray() { - return array_merge( - $this->getCustomProperties(), - array( - 'label' => $this->getLabel(), - 'fragment' => $this->getFragment(), - 'id' => $this->getId(), - 'class' => $this->getClass(), - 'title' => $this->getTitle(), - 'target' => $this->getTarget(), - 'rel' => $this->getRel(), - 'rev' => $this->getRev(), - 'order' => $this->getOrder(), - 'resource' => $this->getResource(), - 'privilege' => $this->getPrivilege(), - 'active' => $this->isActive(), - 'visible' => $this->isVisible(), - 'type' => get_class($this), - 'pages' => parent::toArray() - )); + return array_merge($this->getCustomProperties(), array( + 'label' => $this->getLabel(), + 'fragment' => $this->getFragment(), + 'id' => $this->getId(), + 'class' => $this->getClass(), + 'title' => $this->getTitle(), + 'target' => $this->getTarget(), + 'rel' => $this->getRel(), + 'rev' => $this->getRev(), + 'order' => $this->getOrder(), + 'resource' => $this->getResource(), + 'privilege' => $this->getPrivilege(), + 'active' => $this->isActive(), + 'visible' => $this->isVisible(), + 'type' => get_class($this), + 'pages' => parent::toArray(), + )); } // Internal methods: @@ -1139,7 +1156,7 @@ public function toArray() * @param string $property property name to normalize * @return string normalized property name */ - protected static function _normalizePropertyName($property) + protected static function normalizePropertyName($property) { return str_replace(' ', '', ucwords(str_replace('_', ' ', $property))); } diff --git a/src/Page/Mvc.php b/src/Page/Mvc.php index 788b59d..babe539 100644 --- a/src/Page/Mvc.php +++ b/src/Page/Mvc.php @@ -15,28 +15,24 @@ * @category Zend * @package Zend_Navigation * @subpackage Page - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ -/** - * @namespace - */ namespace Zend\Navigation\Page; use Zend\Mvc\Router\RouteMatch, - Zend\Navigation\AbstractPage, Zend\Navigation\Exception, Zend\View\Helper\Url as UrlHelper; /** - * Represents a page that is defined using module, controller, action, route + * Represents a page that is defined using controller, action, route * name and route params to assemble the href * * @category Zend * @package Zend_Navigation * @subpackage Page - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class Mvc extends AbstractPage @@ -48,11 +44,6 @@ class Mvc extends AbstractPage */ protected $action; - /** - * @var bool - */ - protected $active = false; - /** * Controller name to use when assembling URL * @@ -89,19 +80,28 @@ class Mvc extends AbstractPage /** * Route matches; used for routing parameters and testing validity - * + * * @var RouteMatch */ protected $routeMatch; /** - * Action helper for assembling URLs + * View helper for assembling URLs * * @see getHref() * @var UrlHelper */ protected $urlHelper = null; + /** + * Default urlHelper to be used if urlHelper is not given. + * + * @see getHref() + * + * @var UrlHelper + */ + protected static $defaultUrlHelper = null; + // Accessors: /** @@ -121,8 +121,16 @@ public function isActive($recursive = false) $reqParams = array(); if ($this->routeMatch instanceof RouteMatch) { $reqParams = $this->routeMatch->getParams(); + + if (null !== $this->getRoute() + && $this->routeMatch->getMatchedRouteName() === $this->getRoute() + ) { + $this->active = true; + return true; + } } + $myParams = $this->params; if (null !== $this->controller) { @@ -144,7 +152,8 @@ public function isActive($recursive = false) } if (count(array_intersect_assoc($reqParams, $myParams)) == - count($myParams)) { + count($myParams) + ) { $this->active = true; return true; } @@ -156,10 +165,12 @@ public function isActive($recursive = false) /** * Returns href for this page * - * This method uses {@link Zend_Controller_Action_Helper_Url} to assemble + * This method uses {@link UrlHelper} to assemble * the href based on the page's properties. * + * @see UrlHelper * @return string page href + * @throws Exception\DomainException if no UrlHelper is set */ public function getHref() { @@ -167,8 +178,16 @@ public function getHref() return $this->hrefCache; } - if (null === $this->urlHelper) { - throw new Exception\DomainException(__METHOD__ . ' cannot execute as no Zend\View\Helper\Url instance is composed'); + $helper = $this->urlHelper; + if (null === $helper) { + $helper = self::$defaultUrlHelper; + } + + if (!$helper instanceof UrlHelper) { + throw new Exception\DomainException( + __METHOD__ + . ' cannot execute as no Zend\View\Helper\Url instance is composed' + ); } $params = $this->getParams(); @@ -180,18 +199,17 @@ public function getHref() if (($param = $this->getAction()) != null) { $params['action'] = $param; } - - $helper = $this->urlHelper; - $url = $helper( + + $url = $helper( $this->getRoute(), $params ); // Add the fragment identifier if it is set - $fragment = $this->getFragment(); + $fragment = $this->getFragment(); if (null !== $fragment) { $url .= '#' . $fragment; - } + } return $this->hrefCache = $url; } @@ -209,10 +227,11 @@ public function setAction($action) { if (null !== $action && !is_string($action)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $action must be a string or null'); + 'Invalid argument: $action must be a string or null' + ); } - $this->action = $action; + $this->action = $action; $this->hrefCache = null; return $this; } @@ -242,11 +261,12 @@ public function setController($controller) { if (null !== $controller && !is_string($controller)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $controller must be a string or null'); + 'Invalid argument: $controller must be a string or null' + ); } $this->controller = $controller; - $this->hrefCache = null; + $this->hrefCache = null; return $this; } @@ -262,47 +282,13 @@ public function getController() return $this->controller; } - /** - * Sets module name to use when assembling URL - * - * @see getHref() - * - * @param string|null $module module name - * @return Mvc fluent interface, returns self - * @throws Exception\InvalidArgumentException if invalid module name is given - */ - public function setModule($module) - { - if (null !== $module && !is_string($module)) { - throw new Exception\InvalidArgumentException( - 'Invalid argument: $module must be a string or null'); - } - - $this->module = $module; - $this->hrefCache = null; - return $this; - } - - /** - * Returns module name to use when assembling URL - * - * @see getHref() - * - * @return string|null module name or null - */ - public function getModule() - { - return $this->module; - } - /** * Sets params to use when assembling URL * * @see getHref() - * - * @param array|null $params [optional] page params. Default is null - * which sets no params. - * @return \Zend\Navigation\Page\Mvc fluent interface, returns self + * @param array|null $params [optional] page params. Default is null + * which sets no params. + * @return Mvc fluent interface, returns self */ public function setParams(array $params = null) { @@ -342,10 +328,11 @@ public function setRoute($route) { if (null !== $route && (!is_string($route) || strlen($route) < 1)) { throw new Exception\InvalidArgumentException( - 'Invalid argument: $route must be a non-empty string or null'); + 'Invalid argument: $route must be a non-empty string or null' + ); } - $this->route = $route; + $this->route = $route; $this->hrefCache = null; return $this; } @@ -364,9 +351,9 @@ public function getRoute() /** * Set route match object from which parameters will be retrieved - * - * @param RouteMatch $matches - * @return Mvc + * + * @param RouteMatch $matches + * @return Mvc fluent interface, returns self */ public function setRouteMatch(RouteMatch $matches) { @@ -379,8 +366,8 @@ public function setRouteMatch(RouteMatch $matches) * * @see getHref() * - * @param UrlHelper $uh URL plugin - * @return Mvc + * @param UrlHelper $helper URL helper plugin + * @return Mvc fluent interface, returns self */ public function setUrlHelper(UrlHelper $helper) { @@ -388,6 +375,28 @@ public function setUrlHelper(UrlHelper $helper) return $this; } + /** + * Sets the default view helper for assembling URLs. + * + * @see getHref() + * @param null|UrlHelper $helper URL helper + * @return void + */ + public static function setDefaultUrlHelper($helper) + { + self::$defaultUrlHelper = $helper; + } + + /** + * Gets the default view helper for assembling URLs. + * + * @return UrlHelper + */ + public static function getDefaultUrlHelper() + { + return self::$defaultUrlHelper; + } + // Public methods: /** @@ -400,10 +409,11 @@ public function toArray() return array_merge( parent::toArray(), array( - 'action' => $this->getAction(), - 'controller' => $this->getController(), - 'params' => $this->getParams(), - 'route' => $this->getRoute(), - )); + 'action' => $this->getAction(), + 'controller' => $this->getController(), + 'params' => $this->getParams(), + 'route' => $this->getRoute(), + ) + ); } } diff --git a/src/Page/Uri.php b/src/Page/Uri.php index ffa6218..1dacd0b 100644 --- a/src/Page/Uri.php +++ b/src/Page/Uri.php @@ -15,27 +15,21 @@ * @category Zend * @package Zend_Navigation * @subpackage Page - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ -/** - * @namespace - */ namespace Zend\Navigation\Page; -use Zend\Navigation\AbstractPage, - Zend\Navigation\Exception\InvalidArgumentException; +use Zend\Navigation\Exception; /** * Represents a page that is defined by specifying a URI * - * @uses \Zend\Navigation\Exception - * @uses \Zend\Navigation\Page\Page * @category Zend * @package Zend_Navigation * @subpackage Page - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class Uri extends AbstractPage @@ -45,23 +39,25 @@ class Uri extends AbstractPage * * @var string|null */ - protected $_uri = null; + protected $uri = null; /** * Sets page URI * * @param string $uri page URI, must a string or null - * @return \Zend\Navigation\Page\Uri fluent interface, returns self - * @throws \Zend\Navigation\InvalidArgumentException if $uri is invalid + * + * @return Uri fluent interface, returns self + * @throws Exception\InvalidArgumentException if $uri is invalid */ public function setUri($uri) { if (null !== $uri && !is_string($uri)) { - throw new InvalidArgumentException( - 'Invalid argument: $uri must be a string or null'); + throw new Exception\InvalidArgumentException( + 'Invalid argument: $uri must be a string or null' + ); } - $this->_uri = $uri; + $this->uri = $uri; return $this; } @@ -72,12 +68,12 @@ public function setUri($uri) */ public function getUri() { - return $this->_uri; + return $this->uri; } /** * Returns href for this page - * + * * Includes the fragment identifier if it is set. * * @return string @@ -85,21 +81,19 @@ public function getUri() public function getHref() { $uri = $this->getUri(); - - $fragment = $this->getFragment(); + + $fragment = $this->getFragment(); if (null !== $fragment) { if ('#' == substr($uri, -1)) { return $uri . $fragment; - } else { + } else { return $uri . '#' . $fragment; } } - + return $uri; } - // Public methods: - /** * Returns an array representation of the page * @@ -110,7 +104,8 @@ public function toArray() return array_merge( parent::toArray(), array( - 'uri' => $this->getUri() - )); + 'uri' => $this->getUri(), + ) + ); } } diff --git a/test/ContainerTest.php b/test/ContainerTest.php index 6033130..0291546 100644 --- a/test/ContainerTest.php +++ b/test/ContainerTest.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -25,7 +25,6 @@ namespace ZendTest\Navigation; use Zend\Navigation, - Zend\Navigation\AbstractPage, Zend\Navigation\Page, Zend\Config; @@ -35,7 +34,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @group Zend_Navigation */ @@ -242,7 +241,7 @@ public function testSettingPageOrderShouldUpdateContainerOrder() ) )); - $page3 = AbstractPage::factory(array( + $page3 = Page\AbstractPage::factory(array( 'label' => 'Page 3', 'uri' => '#' )); @@ -309,7 +308,7 @@ public function testAddPageShouldWorkWithPageInstance() $nav = new Navigation\Navigation(array($pageOptions)); - $page = AbstractPage::factory($pageOptions); + $page = Page\AbstractPage::factory($pageOptions); $nav->addPage($page); $this->assertEquals(2, count($nav)); @@ -366,7 +365,7 @@ public function testAddPagesShouldWorkWithMixedArray() 'action' => 'index', 'controller' => 'index' )), - AbstractPage::factory(array( + Page\AbstractPage::factory(array( 'label' => 'Page 3', 'uri' => '#' )) @@ -376,6 +375,24 @@ public function testAddPagesShouldWorkWithMixedArray() 'Expected 3 pages, found ' . count($nav)); } + /** + * @group ZF-9815 + */ + public function testAddPagesShouldWorkWithNavigationContainer() + { + $nav = new Navigation\Navigation(); + $nav->addPages($this->_getFindByNavigation()); + + $this->assertEquals( + 3, count($nav), 'Expected 3 pages, found ' . count($nav) + ); + + $this->assertEquals( + $this->_getFindByNavigation()->toArray(), + $nav->toArray() + ); + } + public function testAddPagesShouldThrowExceptionWhenGivenString() { $nav = new Navigation\Navigation(); @@ -567,7 +584,7 @@ public function testRemovingPageByInstance() ) )); - $page3 = AbstractPage::factory(array( + $page3 = Page\AbstractPage::factory(array( 'label' => 'Page 3', 'uri' => '#' )); @@ -590,7 +607,7 @@ public function testRemovingPageByInstanceShouldReturnFalseIfPageIsNotInContaine ) )); - $page = AbstractPage::factory(array( + $page = Page\AbstractPage::factory(array( 'label' => 'Page lol', 'uri' => '#' )); @@ -600,42 +617,42 @@ public function testRemovingPageByInstanceShouldReturnFalseIfPageIsNotInContaine public function testHasPage() { - $page0 = AbstractPage::factory(array( + $page0 = Page\AbstractPage::factory(array( 'label' => 'Page 0', 'uri' => '#' )); - $page1 = AbstractPage::factory(array( + $page1 = Page\AbstractPage::factory(array( 'label' => 'Page 1', 'uri' => '#' )); - $page1_1 = AbstractPage::factory(array( + $page1_1 = Page\AbstractPage::factory(array( 'label' => 'Page 1.1', 'uri' => '#' )); - $page1_2 = AbstractPage::factory(array( + $page1_2 = Page\AbstractPage::factory(array( 'label' => 'Page 1.2', 'uri' => '#' )); - $page1_2_1 = AbstractPage::factory(array( + $page1_2_1 = Page\AbstractPage::factory(array( 'label' => 'Page 1.2.1', 'uri' => '#' )); - $page1_3 = AbstractPage::factory(array( + $page1_3 = Page\AbstractPage::factory(array( 'label' => 'Page 1.3', 'uri' => '#' )); - $page2 = AbstractPage::factory(array( + $page2 = Page\AbstractPage::factory(array( 'label' => 'Page 2', 'uri' => '#' )); - $page3 = AbstractPage::factory(array( + $page3 = Page\AbstractPage::factory(array( 'label' => 'Page 3', 'uri' => '#' )); @@ -688,12 +705,12 @@ public function testHasPages() public function testSetParentShouldWorkWithPage() { - $page1 = AbstractPage::factory(array( + $page1 = Page\AbstractPage::factory(array( 'label' => 'Page 1', 'uri' => '#' )); - $page2 = AbstractPage::factory(array( + $page2 = Page\AbstractPage::factory(array( 'label' => 'Page 2', 'uri' => '#' )); @@ -715,12 +732,12 @@ public function testSetParentShouldWorkWithPage() public function testSetParentShouldWorkWithNull() { - $page1 = AbstractPage::factory(array( + $page1 = Page\AbstractPage::factory(array( 'label' => 'Page 1', 'uri' => '#' )); - $page2 = AbstractPage::factory(array( + $page2 = Page\AbstractPage::factory(array( 'label' => 'Page 2', 'uri' => '#' )); @@ -733,12 +750,12 @@ public function testSetParentShouldWorkWithNull() public function testSetParentShouldRemoveFromOldParentPage() { - $page1 = AbstractPage::factory(array( + $page1 = Page\AbstractPage::factory(array( 'label' => 'Page 1', 'uri' => '#' )); - $page2 = AbstractPage::factory(array( + $page2 = Page\AbstractPage::factory(array( 'label' => 'Page 2', 'uri' => '#' )); @@ -764,7 +781,7 @@ public function testFinderMethodsShouldWorkWithCustomProperties() $nav = $this->_getFindByNavigation(); $found = $nav->findOneBy('page2', 'page2'); - $this->assertInstanceOf('Zend\\Navigation\\AbstractPage', $found); + $this->assertInstanceOf('Zend\\Navigation\\Page\\AbstractPage', $found); $this->assertEquals('Page 2', $found->getLabel()); } @@ -773,7 +790,7 @@ public function testFindOneByShouldReturnOnlyOnePage() $nav = $this->_getFindByNavigation(); $found = $nav->findOneBy('id', 'page_2_and_3'); - $this->assertInstanceOf('Zend\\Navigation\\AbstractPage', $found); + $this->assertInstanceOf('Zend\\Navigation\\Page\\AbstractPage', $found); $this->assertEquals('Page 2', $found->getLabel()); } @@ -790,7 +807,7 @@ public function testFindAllByShouldReturnAllMatchingPages() $nav = $this->_getFindByNavigation(); $found = $nav->findAllBy('id', 'page_2_and_3'); - $this->assertContainsOnly('Zend\Navigation\AbstractPage', $found, false); + $this->assertContainsOnly('Zend\Navigation\Page\AbstractPage', $found, false); $expected = array('Page 2', 'Page 3'); $actual = array(); @@ -817,7 +834,7 @@ public function testFindByShouldDefaultToFindOneBy() $nav = $this->_getFindByNavigation(); $found = $nav->findBy('id', 'page_2_and_3'); - $this->assertInstanceOf('Zend\\Navigation\\AbstractPage', $found); + $this->assertInstanceOf('Zend\\Navigation\\Page\\AbstractPage', $found); } public function testFindOneByMagicMethodNativeProperty() @@ -825,7 +842,7 @@ public function testFindOneByMagicMethodNativeProperty() $nav = $this->_getFindByNavigation(); $found = $nav->findOneById('page_2_and_3'); - $this->assertInstanceOf('Zend\\Navigation\\AbstractPage', $found); + $this->assertInstanceOf('Zend\\Navigation\\Page\\AbstractPage', $found); $this->assertEquals('Page 2', $found->getLabel()); } @@ -834,7 +851,7 @@ public function testFindOneByMagicMethodCustomProperty() $nav = $this->_getFindByNavigation(); $found = $nav->findOneBypage2('page2'); - $this->assertInstanceOf('Zend\\Navigation\\AbstractPage', $found); + $this->assertInstanceOf('Zend\\Navigation\\Page\\AbstractPage', $found); $this->assertEquals('Page 2', $found->getLabel()); } @@ -843,7 +860,7 @@ public function testFindAllByWithMagicMethodNativeProperty() $nav = $this->_getFindByNavigation(); $found = $nav->findAllById('page_2_and_3'); - $this->assertContainsOnly('Zend\Navigation\AbstractPage', $found, false); + $this->assertContainsOnly('Zend\Navigation\Page\\AbstractPage', $found, false); $expected = array('Page 2', 'Page 3'); $actual = array(); @@ -859,7 +876,7 @@ public function testFindAllByMagicUcfirstPropDoesNotFindCustomLowercaseProps() $nav = $this->_getFindByNavigation(); $found = $nav->findAllByAction('about'); - $this->assertContainsOnly('Zend\Navigation\AbstractPage', $found, false); + $this->assertContainsOnly('Zend\Navigation\Page\\AbstractPage', $found, false); $expected = array('Page 3'); $actual = array(); @@ -875,7 +892,7 @@ public function testFindAllByMagicLowercaseFindsBothNativeAndCustomProps() $nav = $this->_getFindByNavigation(); $found = $nav->findAllByaction('about'); - $this->assertContainsOnly('Zend\Navigation\AbstractPage', $found, false); + $this->assertContainsOnly('Zend\Navigation\Page\\AbstractPage', $found, false); $expected = array('Page 1.3', 'Page 3'); $actual = array(); @@ -891,7 +908,7 @@ public function testFindByMagicMethodIsEquivalentToFindOneBy() $nav = $this->_getFindByNavigation(); $found = $nav->findById('page_2_and_3'); - $this->assertInstanceOf('Zend\\Navigation\\AbstractPage', $found); + $this->assertInstanceOf('Zend\\Navigation\\Page\\AbstractPage', $found); $this->assertEquals('Page 2', $found->getLabel()); } @@ -1025,7 +1042,7 @@ public function testKeyWhenContainerIsEmpty() public function testKeyShouldReturnCurrentPageHash() { $container = new Navigation\Navigation(); - $page = AbstractPage::factory(array( + $page = Page\AbstractPage::factory(array( 'type' => 'uri' )); $container->addPage($page); @@ -1036,7 +1053,7 @@ public function testKeyShouldReturnCurrentPageHash() public function testGetChildrenShouldReturnTheCurrentPage() { $container = new Navigation\Navigation(); - $page = AbstractPage::factory(array( + $page = Page\AbstractPage::factory(array( 'type' => 'uri' )); $container->addPage($page); diff --git a/test/NavigationTest.php b/test/NavigationTest.php index 35d6f20..4fbe58e 100644 --- a/test/NavigationTest.php +++ b/test/NavigationTest.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -33,7 +33,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @group Zend_Navigation */ @@ -73,10 +73,11 @@ public function testNavigationArraySortsCorrectly() $page1->setOrder(1); $page3->setOrder(0); $page2->setOrder(2); - + $pages = $this->_navigation->toArray(); + $this->assertSame(3, count($pages)); - $this->assertEquals('page3', $pages[0]['uri']); + $this->assertEquals('page3', $pages[0]['uri'], var_export($pages, 1)); $this->assertEquals('page1', $pages[1]['uri']); $this->assertEquals('page2', $pages[2]['uri']); } diff --git a/test/Page/MvcTest.php b/test/Page/MvcTest.php index 5347355..39a2e61 100644 --- a/test/Page/MvcTest.php +++ b/test/Page/MvcTest.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -28,6 +28,7 @@ Zend\View\Helper\Url as UrlHelper, Zend\Mvc\Router\RouteMatch, Zend\Mvc\Router\Http\Regex as RegexRoute, + Zend\Mvc\Router\Http\Literal as LiteralRoute, Zend\Mvc\Router\Http\TreeRouteStack, Zend\Navigation\Page, Zend\Navigation, @@ -39,7 +40,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @group Zend_Navigation */ @@ -129,6 +130,43 @@ public function testHrefGeneratedIsRouteAware() $this->assertEquals('/lolcat/myaction/1337', $page->getHref()); } + public function testIsActiveReturnsTrueWhenMatchingRoute() + { + $page = new Page\Mvc(array( + 'label' => 'spiffyjrwashere', + 'route' => 'lolfish' + )); + + $route = new LiteralRoute('/lolfish'); + + $router = new TreeRouteStack; + $router->addRoute('lolfish', $route); + + $routeMatch = new RouteMatch(array()); + $routeMatch->setMatchedRouteName('lolfish'); + + $urlHelper = new UrlHelper; + $urlHelper->setRouter($router); + $urlHelper->setRouteMatch($routeMatch); + + $page->setUrlHelper($urlHelper); + $page->setRouteMatch($routeMatch); + + $this->assertEquals(true, $page->isActive()); + } + + public function testIsActiveReturnsFalseWhenNoRouteAndNoMatchedRouteNameIsSet() + { + $page = new Page\Mvc(); + + $routeMatch = new RouteMatch(array()); + $this->urlHelper->setRouteMatch($routeMatch); + + $page->setRouteMatch($routeMatch); + + $this->assertFalse($page->isActive()); + } + /** * @group ZF-8922 */ @@ -149,7 +187,6 @@ public function testGetHrefWithFragmentIdentifier() '(lolcat/(?[^/]+)/(?\d+))', '/lolcat/%action%/%page%', array( - 'module' => 'default', 'controller' => 'foobar', 'action' => 'bazbat', 'page' => 1, @@ -160,24 +197,22 @@ public function testGetHrefWithFragmentIdentifier() $page->setRouteMatch($this->routeMatch); $page->setUrlHelper($this->urlHelper); - + $this->assertEquals('/lolcat/myaction/1337#qux', $page->getHref()); } - public function testIsActiveReturnsTrueOnIdenticalModuleControllerAction() + public function testIsActiveReturnsTrueOnIdenticalControllerAction() { $page = new Page\Mvc(array( - 'label' => 'foo', 'action' => 'index', 'controller' => 'index' )); $routeMatch = new RouteMatch(array( - 'module' => 'application', 'controller' => 'index', 'action' => 'index', )); - $routeMatch->setMatchedRouteName('default'); + $this->urlHelper->setRouteMatch($routeMatch); $page->setRouteMatch($routeMatch); @@ -185,20 +220,18 @@ public function testIsActiveReturnsTrueOnIdenticalModuleControllerAction() $this->assertTrue($page->isActive()); } - public function testIsActiveReturnsFalseOnDifferentModuleControllerAction() + public function testIsActiveReturnsFalseOnDifferentControllerAction() { $page = new Page\Mvc(array( - 'label' => 'foo', 'action' => 'bar', 'controller' => 'index' )); $routeMatch = new RouteMatch(array( - 'module' => 'default', 'controller' => 'index', 'action' => 'index', )); - $routeMatch->setMatchedRouteName('default'); + $this->urlHelper->setRouteMatch($routeMatch); $page->setRouteMatch($routeMatch); @@ -212,19 +245,17 @@ public function testIsActiveReturnsTrueOnIdenticalIncludingPageParams() 'label' => 'foo', 'action' => 'view', 'controller' => 'post', - 'module' => 'blog', 'params' => array( 'id' => '1337' ) )); $routeMatch = new RouteMatch(array( - 'module' => 'blog', 'controller' => 'post', 'action' => 'view', 'id' => '1337' )); - $routeMatch->setMatchedRouteName('default'); + $this->urlHelper->setRouteMatch($routeMatch); $page->setRouteMatch($routeMatch); @@ -238,16 +269,14 @@ public function testIsActiveReturnsTrueWhenRequestHasMoreParams() 'label' => 'foo', 'action' => 'view', 'controller' => 'post', - 'module' => 'blog' )); $routeMatch = new RouteMatch(array( - 'module' => 'blog', 'controller' => 'post', 'action' => 'view', - 'id' => '1337' + 'id' => '1337', )); - $routeMatch->setMatchedRouteName('default'); + $this->urlHelper->setRouteMatch($routeMatch); $page->setRouteMatch($routeMatch); @@ -261,19 +290,17 @@ public function testIsActiveReturnsFalseWhenRequestHasLessParams() 'label' => 'foo', 'action' => 'view', 'controller' => 'post', - 'module' => 'blog', 'params' => array( 'id' => '1337' ) )); $routeMatch = new RouteMatch(array( - 'module' => 'blog', 'controller' => 'post', 'action' => 'view', 'id' => null )); - $routeMatch->setMatchedRouteName('default'); + $this->urlHelper->setRouteMatch($routeMatch); $page->setRouteMatch($routeMatch); @@ -415,4 +442,45 @@ public function testSpecifyingAnotherUrlHelperToGenerateHrefs() $this->assertEquals($expected, $actual); } + + public function testDefaultUrlHelperCanBeSetWithConstructor() + { + $page = new Page\Mvc(array( + 'label' => 'foo', + 'action' => 'index', + 'controller' => 'index', + 'defaultUrlHelper' => $this->urlHelper + )); + + $this->assertEquals($this->urlHelper, $page->getDefaultUrlHelper()); + $page->setDefaultUrlHelper(null); + } + + public function testDefaultUrlHelperCanBeSetWithGetter() + { + $page = new Page\Mvc(array( + 'label' => 'foo', + 'action' => 'index', + 'controller' => 'index', + )); + $page->setDefaultUrlHelper($this->urlHelper); + + $this->assertEquals($this->urlHelper, $page->getDefaultUrlHelper()); + $page->setDefaultUrlHelper(null); + } + + public function testNoExceptionForGetHrefIfDefaultUrlHelperIsSet() + { + $page = new Page\Mvc(array( + 'label' => 'foo', + 'action' => 'index', + 'controller' => 'index', + 'defaultUrlHelper' => $this->urlHelper + )); + + // If the default url helper is not used an exception will be thrown. + // This method intentionally has no assertion. + $page->getHref(); + $page->setDefaultUrlHelper(null); + } } diff --git a/test/PageFactoryTest.php b/test/Page/PageFactoryTest.php similarity index 93% rename from test/PageFactoryTest.php rename to test/Page/PageFactoryTest.php index 39fafdf..d110c51 100644 --- a/test/PageFactoryTest.php +++ b/test/Page/PageFactoryTest.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -24,7 +24,7 @@ */ namespace ZendTest\Navigation; -use Zend\Navigation\AbstractPage, +use Zend\Navigation\Page\AbstractPage, Zend\Navigation; /** @@ -34,7 +34,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @group Zend_Navigation */ @@ -53,10 +53,6 @@ public function testDetectMvcPage() 'label' => 'MVC Page', 'controller' => 'index' )), - AbstractPage::factory(array( - 'label' => 'MVC Page', - 'module' => 'index' - )), AbstractPage::factory(array( 'label' => 'MVC Page', 'route' => 'home' diff --git a/test/PageTest.php b/test/Page/PageTest.php similarity index 93% rename from test/PageTest.php rename to test/Page/PageTest.php index e20ba74..d2d1892 100644 --- a/test/PageTest.php +++ b/test/Page/PageTest.php @@ -15,17 +15,18 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ /** * @namespace */ -namespace ZendTest\Navigation; +namespace ZendTest\Navigation\Page; -use Zend\Navigation\AbstractPage, - Zend\Navigation\Page, +use Zend\Navigation\Page\AbstractPage, + Zend\Navigation\Page\Mvc, + Zend\Navigation\Page\Uri, Zend\Navigation, Zend\Config; @@ -36,7 +37,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @group Zend_Navigation */ @@ -549,39 +550,39 @@ public function testSetPrivilegeString() public function testGetActiveOnNewlyConstructedPageShouldReturnFalse() { - $page = new Page\Uri(); + $page = new Uri(); $this->assertFalse($page->getActive()); } public function testIsActiveOnNewlyConstructedPageShouldReturnFalse() { - $page = new Page\Uri(); + $page = new Uri(); $this->assertFalse($page->isActive()); } public function testGetActiveShouldReturnTrueIfPageIsActive() { - $page = new Page\Uri(array('active' => true)); + $page = new Uri(array('active' => true)); $this->assertTrue($page->getActive()); } public function testIsActiveShouldReturnTrueIfPageIsActive() { - $page = new Page\Uri(array('active' => true)); + $page = new Uri(array('active' => true)); $this->assertTrue($page->isActive()); } public function testIsActiveWithRecursiveTrueShouldReturnTrueIfChildActive() { - $page = new Page\Uri(array( + $page = new Uri(array( 'label' => 'Page 1', 'active' => false, 'pages' => array( - new Page\Uri(array( + new Uri(array( 'label' => 'Page 1.1', 'active' => false, 'pages' => array( - new Page\Uri(array( + new Uri(array( 'label' => 'Page 1.1', 'active' => true )) @@ -596,15 +597,15 @@ public function testIsActiveWithRecursiveTrueShouldReturnTrueIfChildActive() public function testGetActiveWithRecursiveTrueShouldReturnTrueIfChildActive() { - $page = new Page\Uri(array( + $page = new Uri(array( 'label' => 'Page 1', 'active' => false, 'pages' => array( - new Page\Uri(array( + new Uri(array( 'label' => 'Page 1.1', 'active' => false, 'pages' => array( - new Page\Uri(array( + new Uri(array( 'label' => 'Page 1.1', 'active' => true )) @@ -619,14 +620,14 @@ public function testGetActiveWithRecursiveTrueShouldReturnTrueIfChildActive() public function testSetActiveWithNoParamShouldSetFalse() { - $page = new Page\Uri(); + $page = new Uri(); $page->setActive(); $this->assertTrue($page->getActive()); } public function testSetActiveShouldJuggleValue() { - $page = new Page\Uri(); + $page = new Uri(); $page->setActive(1); $this->assertTrue($page->getActive()); @@ -643,38 +644,38 @@ public function testSetActiveShouldJuggleValue() public function testIsVisibleOnNewlyConstructedPageShouldReturnTrue() { - $page = new Page\Uri(); + $page = new Uri(); $this->assertTrue($page->isVisible()); } public function testGetVisibleOnNewlyConstructedPageShouldReturnTrue() { - $page = new Page\Uri(); + $page = new Uri(); $this->assertTrue($page->getVisible()); } public function testIsVisibleShouldReturnFalseIfPageIsNotVisible() { - $page = new Page\Uri(array('visible' => false)); + $page = new Uri(array('visible' => false)); $this->assertFalse($page->isVisible()); } public function testGetVisibleShouldReturnFalseIfPageIsNotVisible() { - $page = new Page\Uri(array('visible' => false)); + $page = new Uri(array('visible' => false)); $this->assertFalse($page->getVisible()); } public function testIsVisibleRecursiveTrueShouldReturnFalseIfParentInivisble() { - $page = new Page\Uri(array( + $page = new Uri(array( 'label' => 'Page 1', 'visible' => false, 'pages' => array( - new Page\Uri(array( + new Uri(array( 'label' => 'Page 1.1', 'pages' => array( - new Page\Uri(array( + new Uri(array( 'label' => 'Page 1.1' )) ) @@ -689,14 +690,14 @@ public function testIsVisibleRecursiveTrueShouldReturnFalseIfParentInivisble() public function testGetVisibleRecursiveTrueShouldReturnFalseIfParentInivisble() { - $page = new Page\Uri(array( + $page = new Uri(array( 'label' => 'Page 1', 'visible' => false, 'pages' => array( - new Page\Uri(array( + new Uri(array( 'label' => 'Page 1.1', 'pages' => array( - new Page\Uri(array( + new Uri(array( 'label' => 'Page 1.1' )) ) @@ -711,14 +712,14 @@ public function testGetVisibleRecursiveTrueShouldReturnFalseIfParentInivisble() public function testSetVisibleWithNoParamShouldSetVisble() { - $page = new Page\Uri(array('visible' => false)); + $page = new Uri(array('visible' => false)); $page->setVisible(); $this->assertTrue($page->isVisible()); } public function testSetVisibleShouldJuggleValue() { - $page = new Page\Uri(); + $page = new Uri(); $page->setVisible(1); $this->assertTrue($page->isVisible()); @@ -812,7 +813,6 @@ public function testSetOptionsShouldTranslateToAccessor() 'label' => 'bar', 'action' => 'baz', 'controller' => 'bat', - 'module' => 'test', 'id' => 'foo-test' ); @@ -822,7 +822,6 @@ public function testSetOptionsShouldTranslateToAccessor() 'label' => 'bar', 'action' => 'baz', 'controller' => 'bat', - 'module' => 'test', 'id' => 'foo-test' ); @@ -830,44 +829,6 @@ public function testSetOptionsShouldTranslateToAccessor() 'label' => $page->getLabel(), 'action' => $page->getAction(), 'controller' => $page->getController(), - 'module' => $page->getModule(), - 'id' => $page->getId() - ); - - $this->assertEquals($expected, $actual); - } - - public function testSetConfig() - { - $page = AbstractPage::factory(array( - 'label' => 'foo', - 'action' => 'index', - 'controller' => 'index' - )); - - $options = array( - 'label' => 'bar', - 'action' => 'baz', - 'controller' => 'bat', - 'module' => 'test', - 'id' => 'foo-test' - ); - - $page->setConfig(new Config\Config($options)); - - $expected = array( - 'label' => 'bar', - 'action' => 'baz', - 'controller' => 'bat', - 'module' => 'test', - 'id' => 'foo-test' - ); - - $actual = array( - 'label' => $page->getLabel(), - 'action' => $page->getAction(), - 'controller' => $page->getController(), - 'module' => $page->getModule(), 'id' => $page->getId() ); diff --git a/test/Page/UriTest.php b/test/Page/UriTest.php index b3b1d0d..f9afa70 100644 --- a/test/Page/UriTest.php +++ b/test/Page/UriTest.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -32,7 +32,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @group Zend_Navigation */ diff --git a/test/TestAsset/Container.php b/test/TestAsset/Container.php index 99acb96..6b7d4ad 100644 --- a/test/TestAsset/Container.php +++ b/test/TestAsset/Container.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -28,7 +28,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class Container extends \Zend\Navigation\Container @@ -36,6 +36,6 @@ class Container extends \Zend\Navigation\Container public function addPage($page) { parent::addPage($page); - $this->_pages = array(); + $this->pages = array(); } } diff --git a/test/TestAsset/InvalidPage.php b/test/TestAsset/InvalidPage.php index 8172332..6322c59 100644 --- a/test/TestAsset/InvalidPage.php +++ b/test/TestAsset/InvalidPage.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -28,7 +28,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class InvalidPage diff --git a/test/TestAsset/Page.php b/test/TestAsset/Page.php index 96e2bd4..d33bdc2 100644 --- a/test/TestAsset/Page.php +++ b/test/TestAsset/Page.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -24,13 +24,13 @@ */ namespace ZendTest\Navigation\TestAsset; -use Zend\Navigation\AbstractPage; +use Zend\Navigation\Page\AbstractPage; /** * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class Page extends AbstractPage diff --git a/test/TestAsset/UrlHelper.php b/test/TestAsset/UrlHelper.php index a4ad6ee..f597646 100644 --- a/test/TestAsset/UrlHelper.php +++ b/test/TestAsset/UrlHelper.php @@ -15,7 +15,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ @@ -28,7 +28,7 @@ * @category Zend * @package Zend_Navigation * @subpackage UnitTests - * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) + * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class UrlHelper extends \Zend\View\Helper\Url