|
11 | 11 |
|
12 | 12 | use Traversable;
|
13 | 13 | use Zend\Mvc\ModuleRouteListener;
|
14 |
| -use Zend\Mvc\Router\RouteMatch; |
15 |
| -use Zend\Mvc\Router\RouteStackInterface; |
| 14 | +use Zend\Mvc\Router\RouteMatch as LegacyRouteMatch; |
| 15 | +use Zend\Mvc\Router\RouteStackInterface as LegacyRouteStackInterface; |
| 16 | +use Zend\Router\RouteMatch; |
| 17 | +use Zend\Router\RouteStackInterface; |
16 | 18 | use Zend\View\Exception;
|
17 | 19 |
|
18 | 20 | /**
|
|
21 | 23 | class Url extends AbstractHelper
|
22 | 24 | {
|
23 | 25 | /**
|
24 |
| - * RouteStackInterface instance. |
| 26 | + * Router instance. |
25 | 27 | *
|
26 |
| - * @var RouteStackInterface |
| 28 | + * @var LegacyRouteStackInterface|RouteStackInterface |
27 | 29 | */
|
28 | 30 | protected $router;
|
29 | 31 |
|
30 | 32 | /**
|
31 |
| - * RouteInterface match returned by the router. |
| 33 | + * Route matches returned by the router. |
32 | 34 | *
|
33 |
| - * @var RouteMatch. |
| 35 | + * @var LegacyRouteMatch|RouteMatch. |
34 | 36 | */
|
35 | 37 | protected $routeMatch;
|
36 | 38 |
|
37 | 39 | /**
|
38 | 40 | * Generates a url given the name of a route.
|
39 | 41 | *
|
40 |
| - * @see Zend\Mvc\Router\RouteInterface::assemble() |
41 |
| - * @param string $name Name of the route |
42 |
| - * @param array $params Parameters for the link |
43 |
| - * @param array|Traversable $options Options for the route |
44 |
| - * @param bool $reuseMatchedParams Whether to reuse matched parameters |
45 |
| - * @return string Url For the link href attribute |
46 |
| - * @throws Exception\RuntimeException If no RouteStackInterface was provided |
47 |
| - * @throws Exception\RuntimeException If no RouteMatch was provided |
48 |
| - * @throws Exception\RuntimeException If RouteMatch didn't contain a matched route name |
49 |
| - * @throws Exception\InvalidArgumentException If the params object was not an array or \Traversable object |
| 42 | + * @see Zend\Mvc\Router\RouteInterface::assemble() |
| 43 | + * @see Zend\Router\RouteInterface::assemble() |
| 44 | + * @param string $name Name of the route |
| 45 | + * @param array $params Parameters for the link |
| 46 | + * @param array|Traversable $options Options for the route |
| 47 | + * @param bool $reuseMatchedParams Whether to reuse matched parameters |
| 48 | + * @return string Url For the link href attribute |
| 49 | + * @throws Exception\RuntimeException If no RouteStackInterface was |
| 50 | + * provided |
| 51 | + * @throws Exception\RuntimeException If no RouteMatch was provided |
| 52 | + * @throws Exception\RuntimeException If RouteMatch didn't contain a |
| 53 | + * matched route name |
| 54 | + * @throws Exception\InvalidArgumentException If the params object was not |
| 55 | + * an array or Traversable object. |
50 | 56 | */
|
51 | 57 | public function __invoke($name = null, $params = [], $options = [], $reuseMatchedParams = false)
|
52 | 58 | {
|
@@ -103,23 +109,48 @@ public function __invoke($name = null, $params = [], $options = [], $reuseMatche
|
103 | 109 | /**
|
104 | 110 | * Set the router to use for assembling.
|
105 | 111 | *
|
106 |
| - * @param RouteStackInterface $router |
| 112 | + * @param LegacyRouteStackInterface|RouteStackInterface $router |
107 | 113 | * @return Url
|
| 114 | + * @throws Exception\InvalidArgumentException for invalid router types. |
108 | 115 | */
|
109 |
| - public function setRouter(RouteStackInterface $router) |
| 116 | + public function setRouter($router) |
110 | 117 | {
|
| 118 | + if (! $router instanceof RouteStackInterface |
| 119 | + && ! $router instanceof LegacyRouteStackInterface |
| 120 | + ) { |
| 121 | + throw new Exception\InvalidArgumentException(sprintf( |
| 122 | + '%s expects a %s or %s instance; received %s', |
| 123 | + __METHOD__, |
| 124 | + RouteStackInterface::class, |
| 125 | + LegacyRouteStackInterface::class, |
| 126 | + (is_object($router) ? get_class($router) : gettype($router)) |
| 127 | + )); |
| 128 | + } |
| 129 | + |
111 | 130 | $this->router = $router;
|
112 | 131 | return $this;
|
113 | 132 | }
|
114 | 133 |
|
115 | 134 | /**
|
116 | 135 | * Set route match returned by the router.
|
117 | 136 | *
|
118 |
| - * @param RouteMatch $routeMatch |
| 137 | + * @param LegacyRouteMatch|RouteMatch $routeMatch |
119 | 138 | * @return Url
|
120 | 139 | */
|
121 |
| - public function setRouteMatch(RouteMatch $routeMatch) |
| 140 | + public function setRouteMatch($routeMatch) |
122 | 141 | {
|
| 142 | + if (! $routeMatch instanceof RouteMatch |
| 143 | + && ! $routeMatch instanceof LegacyRouteMatch |
| 144 | + ) { |
| 145 | + throw new Exception\InvalidArgumentException(sprintf( |
| 146 | + '%s expects a %s or %s instance; received %s', |
| 147 | + __METHOD__, |
| 148 | + RouteMatch::class, |
| 149 | + LegacyRouteMatch::class, |
| 150 | + (is_object($routeMatch) ? get_class($routeMatch) : gettype($routeMatch)) |
| 151 | + )); |
| 152 | + } |
| 153 | + |
123 | 154 | $this->routeMatch = $routeMatch;
|
124 | 155 | return $this;
|
125 | 156 | }
|
|
0 commit comments