Skip to content

Commit

Permalink
Add query_params to UrlGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Apr 23, 2022
1 parent fb0199b commit 78bbd6e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 27 deletions.
32 changes: 9 additions & 23 deletions lib/UrlGenerator.php
Expand Up @@ -11,33 +11,19 @@

namespace ICanBoogie\Routing;

use ICanBoogie\Routing\Exception\RouteNotFound;
use ICanBoogie\Routing\RouteProvider\ByIdOrAction;

use function is_string;

class UrlGenerator
interface UrlGenerator
{
public function __construct(
private readonly RouteProvider $routes
) {
}

/**
* @phpstan-param string|(callable(Route): bool) $predicate_or_id_or_action
*
* @param array<string, mixed>|object|null $params
* @param array<string, mixed>|object|null $path_params
* Parameters that reference placeholders in the route pattern.
* @param array<string, mixed>|object|null $query_params
* Parameters for the query string.
*/
public function generate_url(string|callable $predicate_or_id_or_action, array|object|null $params = null): string
{
$predicate = is_string($predicate_or_id_or_action)
? new ByIdOrAction($predicate_or_id_or_action)
: $predicate_or_id_or_action;

$route = $this->routes->route_for_predicate($predicate)
?? throw new RouteNotFound(predicate: $predicate);

return $route->pattern->format($params);
}
public function generate_url(
string|callable $predicate_or_id_or_action,
array|object|null $path_params = null,
array|object $query_params = null,
): string;
}
50 changes: 50 additions & 0 deletions lib/UrlGenerator/UrlGeneratorWithRouteProvider.php
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the ICanBoogie package.
*
* (c) Olivier Laviale <olivier.laviale@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ICanBoogie\Routing\UrlGenerator;

use ICanBoogie\Routing\Exception\RouteNotFound;
use ICanBoogie\Routing\RouteProvider;
use ICanBoogie\Routing\RouteProvider\ByIdOrAction;

use ICanBoogie\Routing\UrlGenerator;

use function http_build_query;
use function is_string;

class UrlGeneratorWithRouteProvider implements UrlGenerator
{
public function __construct(
private readonly RouteProvider $routes
) {
}

public function generate_url(
string|callable $predicate_or_id_or_action,
array|object|null $path_params = null,
array|object|null $query_params = null,
): string {
$predicate = is_string($predicate_or_id_or_action)
? new ByIdOrAction($predicate_or_id_or_action)
: $predicate_or_id_or_action;

$route = $this->routes->route_for_predicate($predicate)
?? throw new RouteNotFound(predicate: $predicate);

$url = $route->pattern->format($path_params);

if ($query_params) {
$url .= '?' . http_build_query($query_params);
}

return $url;
}
}
Expand Up @@ -9,10 +9,10 @@
* file that was distributed with this source code.
*/

namespace Test\ICanBoogie\Routing;
namespace Test\ICanBoogie\Routing\UrlGenerator;

use ICanBoogie\Routing\RouteCollection;
use ICanBoogie\Routing\UrlGenerator;
use ICanBoogie\Routing\UrlGenerator\UrlGeneratorWithRouteProvider;
use PHPUnit\Framework\TestCase;

final class UrlGeneratorTest extends TestCase
Expand All @@ -22,9 +22,12 @@ public function test_generate_url(): void
$routes = new RouteCollection();
$routes->resource('articles');

$generator = new UrlGenerator($routes);
$url = $generator->generate_url('articles:show', [ 'id' => 123 ]);
$generator = new UrlGeneratorWithRouteProvider($routes);

$url = $generator->generate_url('articles:show', [ 'id' => 123, 'title' => 'madonna' ]);
$this->assertEquals("/articles/123", $url);

$url = $generator->generate_url('articles:list', query_params: [ 'page' => 1, 'order' => '-date' ]);
$this->assertEquals("/articles?page=1&order=-date", $url);
}
}

0 comments on commit 78bbd6e

Please sign in to comment.