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

Add RouteArgument attribute for Yii Hydrator #203

Merged
merged 14 commits into from
Jan 29, 2024
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Yii Router Change Log

## 3.0.1 under development
## 3.1.0 under development

- no changes in this release.
- New #203: Added `Route` attribute for Yii Hydrator (@vjik)

## 3.0.0 February 17, 2023

Expand Down
9 changes: 9 additions & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"symbol-whitelist": [
"Yiisoft\\Hydrator\\Context",
"Yiisoft\\Hydrator\\NotResolvedException",
"Yiisoft\\Hydrator\\ParameterAttributeInterface",
"Yiisoft\\Hydrator\\ParameterAttributeResolverInterface",
"Yiisoft\\Hydrator\\UnexpectedAttributeException"
]
}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"vimeo/psalm": "^4.30|^5.6",
"yiisoft/di": "^1.0",
"yiisoft/dummy-provider": "^1.0.0",
"yiisoft/hydrator": "dev-master as 1.0",
"yiisoft/test-support": "^3.0"
},
"autoload": {
Expand All @@ -52,7 +53,8 @@
}
},
"suggest": {
"yiisoft/router-fastroute": "Router implementation based on nikic/FastRoute"
"yiisoft/router-fastroute": "Router implementation based on nikic/FastRoute",
"yiisoft/hydrator": "Need to use `Route` attribute"
vjik marked this conversation as resolved.
Show resolved Hide resolved
},
"extra": {
"config-plugin-options": {
Expand Down
27 changes: 27 additions & 0 deletions src/HydratorAttribute/Route.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\HydratorAttribute;

use Attribute;
use Yiisoft\Hydrator\ParameterAttributeInterface;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)]
final class Route implements ParameterAttributeInterface
{
public function __construct(
private ?string $name = null
) {
}

public function getName(): ?string
{
return $this->name;
}

public function getResolver(): string
{
return RouteResolver::class;
}
}
36 changes: 36 additions & 0 deletions src/HydratorAttribute/RouteResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\HydratorAttribute;

use Yiisoft\Hydrator\Context;
use Yiisoft\Hydrator\NotResolvedException;
use Yiisoft\Hydrator\ParameterAttributeInterface;
use Yiisoft\Hydrator\ParameterAttributeResolverInterface;
use Yiisoft\Hydrator\UnexpectedAttributeException;
use Yiisoft\Router\CurrentRoute;

final class RouteResolver implements ParameterAttributeResolverInterface
{
public function __construct(
private CurrentRoute $currentRoute,
) {
}

public function getParameterValue(ParameterAttributeInterface $attribute, Context $context): mixed
{
if (!$attribute instanceof Route) {
throw new UnexpectedAttributeException(Route::class, $attribute);
}

$arguments = $this->currentRoute->getArguments();

$name = $attribute->getName();
if ($name === null) {
return $arguments;
}

return $arguments[$name] ?? throw new NotResolvedException();
}
}
93 changes: 93 additions & 0 deletions tests/HydratorAttribute/RouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Router\Tests\HydratorAttribute;

use PHPUnit\Framework\TestCase;
use ReflectionFunction;
use Yiisoft\Hydrator\Attribute\Parameter\ToString;
use Yiisoft\Hydrator\Context;
use Yiisoft\Hydrator\Hydrator;
use Yiisoft\Hydrator\UnexpectedAttributeException;
use Yiisoft\Router\CurrentRoute;
use Yiisoft\Router\HydratorAttribute\Route;
use Yiisoft\Router\HydratorAttribute\RouteResolver;
use Yiisoft\Router\Route as RouterRoute;
use Yiisoft\Test\Support\Container\SimpleContainer;

final class RouteTest extends TestCase
{
public function testBase(): void
{
$hydrator = $this->createHydrator([
'a' => 'one',
'b' => 'two',
]);

$input = new class () {
#[Route('a')]
public string $a = '';
#[Route('b')]
public string $b = '';
#[Route]
public array $all = [];
};

$hydrator->hydrate($input);

$this->assertSame('one', $input->a);
$this->assertSame('two', $input->b);
$this->assertSame(['a' => 'one', 'b' => 'two'], $input->all);
}

public function testWithoutArguments(): void
{
$hydrator = $this->createHydrator([]);

$input = new class () {
#[Route('a')]
public string $a = '';
#[Route('b')]
public string $b = '';
#[Route]
public array $all = [];
};

$hydrator->hydrate($input);

$this->assertSame('', $input->a);
$this->assertSame('', $input->b);
$this->assertSame([], $input->all);
}

public function testUnexpectedAttributeException(): void
{
$resolver = new RouteResolver(new CurrentRoute());

$attribute = new ToString();
$context = $this->createContext();

$this->expectException(UnexpectedAttributeException::class);
$this->expectExceptionMessage('Expected "' . Route::class . '", but "' . ToString::class . '" given.');
$resolver->getParameterValue($attribute, $context);
}

private function createHydrator(array $arguments): Hydrator
{
$currentRoute = new CurrentRoute();
$currentRoute->setRouteWithArguments(RouterRoute::get('/'), $arguments);

return new Hydrator(
new SimpleContainer([
RouteResolver::class => new RouteResolver($currentRoute),
]),
);
}

private function createContext(): Context
{
$reflection = new ReflectionFunction(static fn (int $a) => null);
return new Context($reflection->getParameters()[0], false, null, [], []);
}
}