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

Allow usage of zend-router with Url helper #58

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"zendframework/zend-navigation": "^2.5",
"zendframework/zend-paginator": "^2.5",
"zendframework/zend-permissions-acl": "^2.6",
"zendframework/zend-router": "^3.0.1",
"zendframework/zend-serializer": "^2.6.1",
"zendframework/zend-session": "^2.6.2",
"zendframework/zend-servicemanager": "^2.7.5 || ^3.0.3",
Expand Down
62 changes: 60 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 51 additions & 20 deletions src/Helper/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

use Traversable;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Router\RouteStackInterface;
use Zend\Mvc\Router\RouteMatch as LegacyRouteMatch;
use Zend\Mvc\Router\RouteStackInterface as LegacyRouteStackInterface;
use Zend\Router\RouteMatch;
use Zend\Router\RouteStackInterface;
use Zend\View\Exception;

/**
Expand All @@ -21,32 +23,36 @@
class Url extends AbstractHelper
{
/**
* RouteStackInterface instance.
* Router instance.
*
* @var RouteStackInterface
* @var LegacyRouteStackInterface|RouteStackInterface
*/
protected $router;

/**
* RouteInterface match returned by the router.
* Route matches returned by the router.
*
* @var RouteMatch.
* @var LegacyRouteMatch|RouteMatch.
*/
protected $routeMatch;

/**
* Generates a url given the name of a route.
*
* @see Zend\Mvc\Router\RouteInterface::assemble()
* @param string $name Name of the route
* @param array $params Parameters for the link
* @param array|Traversable $options Options for the route
* @param bool $reuseMatchedParams Whether to reuse matched parameters
* @return string Url For the link href attribute
* @throws Exception\RuntimeException If no RouteStackInterface was provided
* @throws Exception\RuntimeException If no RouteMatch was provided
* @throws Exception\RuntimeException If RouteMatch didn't contain a matched route name
* @throws Exception\InvalidArgumentException If the params object was not an array or \Traversable object
* @see Zend\Mvc\Router\RouteInterface::assemble()
* @see Zend\Router\RouteInterface::assemble()
* @param string $name Name of the route
* @param array $params Parameters for the link
* @param array|Traversable $options Options for the route
* @param bool $reuseMatchedParams Whether to reuse matched parameters
* @return string Url For the link href attribute
* @throws Exception\RuntimeException If no RouteStackInterface was
* provided
* @throws Exception\RuntimeException If no RouteMatch was provided
* @throws Exception\RuntimeException If RouteMatch didn't contain a
* matched route name
* @throws Exception\InvalidArgumentException If the params object was not
* an array or Traversable object.
*/
public function __invoke($name = null, $params = [], $options = [], $reuseMatchedParams = false)
{
Expand Down Expand Up @@ -103,23 +109,48 @@ public function __invoke($name = null, $params = [], $options = [], $reuseMatche
/**
* Set the router to use for assembling.
*
* @param RouteStackInterface $router
* @param LegacyRouteStackInterface|RouteStackInterface $router
* @return Url
* @throws Exception\InvalidArgumentException for invalid router types.
*/
public function setRouter(RouteStackInterface $router)
public function setRouter($router)
{
if (! $router instanceof RouteStackInterface
&& ! $router instanceof LegacyRouteStackInterface
) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects a %s or %s instance; received %s',
__METHOD__,
RouteStackInterface::class,
LegacyRouteStackInterface::class,
(is_object($router) ? get_class($router) : gettype($router))
));
}

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

/**
* Set route match returned by the router.
*
* @param RouteMatch $routeMatch
* @param LegacyRouteMatch|RouteMatch $routeMatch
* @return Url
*/
public function setRouteMatch(RouteMatch $routeMatch)
public function setRouteMatch($routeMatch)
{
if (! $routeMatch instanceof RouteMatch
&& ! $routeMatch instanceof LegacyRouteMatch
) {
throw new Exception\InvalidArgumentException(sprintf(
'%s expects a %s or %s instance; received %s',
__METHOD__,
RouteMatch::class,
LegacyRouteMatch::class,
(is_object($routeMatch) ? get_class($routeMatch) : gettype($routeMatch))
));
}

$this->routeMatch = $routeMatch;
return $this;
}
Expand Down
18 changes: 18 additions & 0 deletions test/Helper/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Router\SimpleRouteStack as Router;
use Zend\Router\RouteMatch as NextGenRouteMatch;
use Zend\Router\SimpleRouteStack as NextGenRouter;

/**
* Zend\View\Helper\Url Test
Expand Down Expand Up @@ -201,4 +203,20 @@ public function testRemovesModuleRouteListenerParamsWhenReusingMatchedParameters
$url = $helper->__invoke('default/wildcard', ['Twenty' => 'Cooler'], true);
$this->assertEquals('/Rainbow/Dash=Twenty%Cooler', $url);
}

public function testAcceptsNextGenRouterToSetRouter()
{
$router = new NextGenRouter();
$url = new UrlHelper();
$url->setRouter($router);
$this->assertAttributeSame($router, 'router', $url);
}

public function testAcceptsNextGenRouteMatche()
{
$routeMatch = new NextGenRouteMatch([]);
$url = new UrlHelper();
$url->setRouteMatch($routeMatch);
$this->assertAttributeSame($routeMatch, 'routeMatch', $url);
}
}