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

Commit

Permalink
Merge pull request zendframework/zendframework#4178 from macnibblet/f…
Browse files Browse the repository at this point in the history
…eature/url-helper-model-support

Allow passing objects to the url helper

Conflicts:
	library/Zend/View/Helper/Url.php
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/Helper/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@

namespace Zend\View\Helper;

use Traversable;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Router\RouteStackInterface;
use Zend\Stdlib\ArrayUtils;
use Zend\View\Exception;
use Zend\Stdlib\Exception as StdlibException;

/**
* Helper for making easy links and getting urls that depend on the routes and router.
Expand All @@ -37,16 +40,17 @@ class Url extends AbstractHelper
* Generates an 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 $options Options for the route
* @param bool $reuseMatchedParams Whether to reuse matched parameters
* @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
* @return string Url For the link href attribute
* @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, array $params = array(), $options = array(), $reuseMatchedParams = false)
public function __invoke($name = null, $params = array(), $options = array(), $reuseMatchedParams = false)
{
if (null === $this->router) {
throw new Exception\RuntimeException('No RouteStackInterface instance provided');
Expand All @@ -69,6 +73,15 @@ public function __invoke($name = null, array $params = array(), $options = array
}
}

if (!is_array($params)) {
if (!$params instanceof Traversable) {
throw new Exception\InvalidArgumentException(
'Params is expected to be an array or a Traversable object'
);
}
$params = iterator_to_array($params);
}

if ($reuseMatchedParams && $this->routeMatch !== null) {
$routeMatchParams = $this->routeMatch->getParams();

Expand Down
16 changes: 16 additions & 0 deletions test/Helper/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ public function testModuleRoute()
$this->assertEquals('/ctrl/act', $url);
}

public function testModel()
{
$it = new \ArrayIterator(array('controller' => 'ctrl', 'action' => 'act'));

$url = $this->url->__invoke('default', $it);
$this->assertEquals('/ctrl/act', $url);
}

/**
* @expectedException \Zend\View\Exception\InvalidArgumentException
*/
public function testThrowsExceptionOnInvalidParams()
{
$this->url->__invoke('default', 'invalid params');
}

public function testPluginWithoutRouteMatchesInEventRaisesExceptionWhenNoRouteProvided()
{
$this->setExpectedException('Zend\View\Exception\RuntimeException', 'RouteMatch');
Expand Down

0 comments on commit ff1d300

Please sign in to comment.