Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move parameters group into current route #128

Merged
merged 17 commits into from
Nov 22, 2021
Merged
24 changes: 24 additions & 0 deletions src/CurrentRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ final class CurrentRoute implements CurrentRouteInterface
*/
private ?UriInterface $uri = null;

/**
* Current Route parameters
samdark marked this conversation as resolved.
Show resolved Hide resolved
*/
private array $parameters = [];

/**
* Returns the current route name.
*
Expand Down Expand Up @@ -77,4 +82,23 @@ public function setUri(UriInterface $uri): void
}
throw new RuntimeException('Can not set URI since it was already set.');
}

public function getParameters(): array
{
return $this->parameters;
}
rustamwin marked this conversation as resolved.
Show resolved Hide resolved

public function getParameter(string $name, $default = null): ?string
{
return $this->parameters[$name] ?? $default;
}
rustamwin marked this conversation as resolved.
Show resolved Hide resolved

public function setParameters(array $parameters)
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
{
if ($this->parameters === []) {
$this->parameters = $parameters;
return;
}
throw new RuntimeException('Can not set parameters since it was already set.');
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
}
}
17 changes: 17 additions & 0 deletions src/CurrentRouteInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,21 @@ public function getRoute(): ?RouteParametersInterface;
* @return UriInterface|null The current URI.
*/
public function getUri(): ?UriInterface;

/**
* Returns the current route parameters.
*
* @return array
*/
public function getParameters(): array;
rustamwin marked this conversation as resolved.
Show resolved Hide resolved

/**
* Returns the current route parameter.
*
* @param string $name
* @param mixed|null $default
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
*
* @return mixed
*/
public function getParameter(string $name, $default = null);
}
5 changes: 1 addition & 4 deletions src/Middleware/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}

$this->currentRoute->setRoute($result->route());

foreach ($result->parameters() as $parameter => $value) {
$request = $request->withAttribute($parameter, $value);
}
$this->currentRoute->setParameters($result->parameters());

return $result->withDispatcher($this->dispatcher)->process($request, $handler);
}
Expand Down
32 changes: 32 additions & 0 deletions tests/CurrentRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,36 @@ public function testGetCurrentUri(): void

$this->assertSame($uri, $currentRoute->getUri());
}

public function testGetParameters(): void
{
$parameters = [
'test' => 'test',
'foo' => 'bar',
];
$currentRoute = new CurrentRoute();
$currentRoute->setParameters($parameters);

$this->assertSame($parameters, $currentRoute->getParameters());
}

public function testGetParameter(): void
{
$parameters = [
'test' => 'test',
'foo' => 'bar',
];
$currentRoute = new CurrentRoute();
$currentRoute->setParameters($parameters);

$this->assertSame('bar', $currentRoute->getParameter('foo'));
}

public function testGetParameterWithNonExist()
samdark marked this conversation as resolved.
Show resolved Hide resolved
{
$currentRoute = new CurrentRoute();
$currentRoute->setParameters(['test' => 1]);

$this->assertNull($currentRoute->getParameter('foo'));
}
rustamwin marked this conversation as resolved.
Show resolved Hide resolved
}
10 changes: 10 additions & 0 deletions tests/Middleware/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@ public function testGetCurrentUri(): void
$this->assertSame($request->getUri(), $currentRoute->getUri());
}

public function testGetParameters(): void
{
$currentRoute = new CurrentRoute();
$request = new ServerRequest('GET', '/');

$this->processWithRouter($request, $currentRoute);

$this->assertSame(['parameter' => 'value'], $currentRoute->getParameters());
}

private function getMatcher(): UrlMatcherInterface
{
$middleware = $this->createRouteMiddleware();
Expand Down