Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/zend-navigation-updates' of https://github.com/…
Browse files Browse the repository at this point in the history
…SpiffyJr/zf2 into hotfix/navigation-isactive
  • Loading branch information
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 40 deletions.
49 changes: 25 additions & 24 deletions src/AbstractPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ abstract class AbstractPage extends Container

/**
* 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;
Expand Down Expand Up @@ -198,7 +198,8 @@ public static function factory($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 Zend\Config\Config'
);
}

if (isset($options['type'])) {
Expand All @@ -221,8 +222,8 @@ public static function factory($options)
if (!$page instanceof self) {
throw new Exception\InvalidArgumentException(
sprintf(
'Invalid argument: Detected type "%s", which '
. 'is not an instance of Zend_Navigation_Page',
'Invalid argument: Detected type "%s", which ' .
'is not an instance of Zend_Navigation_Page',
$type
)
);
Expand Down Expand Up @@ -262,15 +263,15 @@ public function __construct($options = null)
}

// do custom initialization
$this->_init();
$this->init();
}

/**
* Initializes page (used by subclasses)
*
* @return void
*/
protected function _init()
protected function init()
{
}

Expand Down Expand Up @@ -352,11 +353,11 @@ public function setFragment($fragment)
'Invalid argument: $fragment must be a string or null'
);
}

$this->fragment = $fragment;
return $this;
}

/**
* Returns fragment identifier
*
Expand Down Expand Up @@ -511,8 +512,8 @@ public function setRel($relations = null)

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 Zend\Config'
);
}

Expand Down Expand Up @@ -574,8 +575,8 @@ public function setRev($relations = null)

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 Zend\Config'
);
}

Expand Down Expand Up @@ -634,8 +635,8 @@ 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'
);
}

Expand Down Expand Up @@ -679,8 +680,8 @@ public function setResource($resource = null)
$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_Interface'
);
}

Expand Down Expand Up @@ -886,7 +887,7 @@ public function set($property, $value)
);
}

$method = 'set' . self::_normalizePropertyName($property);
$method = 'set' . self::normalizePropertyName($property);

if ($method != 'setOptions' && $method != 'setConfig' &&
method_exists($this, $method)) {
Expand Down Expand Up @@ -917,7 +918,7 @@ public function get($property)
);
}

$method = 'get' . self::_normalizePropertyName($property);
$method = 'get' . self::normalizePropertyName($property);

if (method_exists($this, $method)) {
return $this->$method();
Expand Down Expand Up @@ -973,7 +974,7 @@ public function __get($name)
*/
public function __isset($name)
{
$method = 'get' . self::_normalizePropertyName($name);
$method = 'get' . self::normalizePropertyName($name);
if (method_exists($this, $method)) {
return true;
}
Expand All @@ -992,7 +993,7 @@ public function __isset($name)
*/
public function __unset($name)
{
$method = 'set' . self::_normalizePropertyName($name);
$method = 'set' . self::normalizePropertyName($name);
if (method_exists($this, $method)) {
throw new Exception\InvalidArgumentException(
sprintf(
Expand Down Expand Up @@ -1159,7 +1160,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)));
}
Expand Down
20 changes: 5 additions & 15 deletions src/Page/Mvc.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,6 @@ class Mvc extends AbstractPage
*/
protected $controller;

/**
* Module name to use when assembling URL
*
* @var string
*/
protected $module;

/**
* Params to use when assembling URL
*
Expand Down Expand Up @@ -105,7 +98,6 @@ class Mvc extends AbstractPage
* View helper for assembling URLs
*
* @see getHref()
*
* @var UrlHelper
*/
protected $urlHelper = null;
Expand All @@ -130,7 +122,6 @@ class Mvc extends AbstractPage
* @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 or not
*/
public function isActive($recursive = false)
Expand All @@ -139,6 +130,11 @@ public function isActive($recursive = false)
$reqParams = array();
if ($this->routeMatch instanceof RouteMatch) {
$reqParams = $this->routeMatch->getParams();

if ($this->routeMatch->getMatchedRouteName() === $this->getRoute()) {
$this->active = true;
return true;
}
}

$myParams = $this->params;
Expand Down Expand Up @@ -230,7 +226,6 @@ public function getHref()
* @see getHref()
*
* @param string $action action name
*
* @return Mvc fluent interface, returns self
* @throws Exception\InvalidArgumentException if invalid $action is given
*/
Expand Down Expand Up @@ -265,7 +260,6 @@ public function getAction()
* @see getHref()
*
* @param string|null $controller controller name
*
* @return Mvc fluent interface, returns self
* @throws Exception\InvalidArgumentException if invalid controller name is given
*/
Expand Down Expand Up @@ -300,7 +294,6 @@ public function getController()
* @see getHref()
*
* @param string|null $module module name
*
* @return Mvc fluent interface, returns self
* @throws Exception\InvalidArgumentException if invalid module name is given
*/
Expand Down Expand Up @@ -336,7 +329,6 @@ public function getModule()
*
* @param array|null $params [optional] page params. Default is null
* which sets no params.
*
* @return \Zend\Navigation\Page\Mvc fluent interface, returns self
*/
public function setParams(array $params = null)
Expand Down Expand Up @@ -370,7 +362,6 @@ public function getParams()
* @see getHref()
*
* @param string $route route name to use when assembling URL
*
* @return Mvc fluent interface, returns self
* @throws Exception\InvalidArgumentException if invalid $route is given
*/
Expand Down Expand Up @@ -403,7 +394,6 @@ public function getRoute()
* Set route match object from which parameters will be retrieved
*
* @param RouteMatch $matches
*
* @return Mvc
*/
public function setRouteMatch(RouteMatch $matches)
Expand Down
28 changes: 27 additions & 1 deletion test/Page/MvcTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -129,6 +130,31 @@ 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());
}

/**
* @group ZF-8922
*/
Expand Down Expand Up @@ -160,7 +186,7 @@ public function testGetHrefWithFragmentIdentifier()

$page->setRouteMatch($this->routeMatch);
$page->setUrlHelper($this->urlHelper);

$this->assertEquals('/lolcat/myaction/1337#qux', $page->getHref());
}

Expand Down

0 comments on commit 8d162ec

Please sign in to comment.