-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
RouteArgument
attribute for Yii Hydrator (#203)
- Loading branch information
Showing
6 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\Attribute\Parameter\ParameterAttributeInterface; | ||
|
||
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)] | ||
final class RouteArgument implements ParameterAttributeInterface | ||
{ | ||
public function __construct( | ||
private ?string $name = null | ||
) { | ||
} | ||
|
||
public function getName(): ?string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function getResolver(): string | ||
{ | ||
return RouteArgumentResolver::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Router\HydratorAttribute; | ||
|
||
use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeInterface; | ||
use Yiisoft\Hydrator\Attribute\Parameter\ParameterAttributeResolverInterface; | ||
use Yiisoft\Hydrator\AttributeHandling\Exception\UnexpectedAttributeException; | ||
use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext; | ||
use Yiisoft\Hydrator\Result; | ||
use Yiisoft\Router\CurrentRoute; | ||
|
||
use function array_key_exists; | ||
|
||
final class RouteArgumentResolver implements ParameterAttributeResolverInterface | ||
{ | ||
public function __construct( | ||
private CurrentRoute $currentRoute, | ||
) { | ||
} | ||
|
||
public function getParameterValue( | ||
ParameterAttributeInterface $attribute, | ||
ParameterAttributeResolveContext $context, | ||
): Result { | ||
if (!$attribute instanceof RouteArgument) { | ||
throw new UnexpectedAttributeException(RouteArgument::class, $attribute); | ||
} | ||
|
||
$arguments = $this->currentRoute->getArguments(); | ||
|
||
$name = $attribute->getName() ?? $context->getParameter()->getName(); | ||
|
||
if (array_key_exists($name, $arguments)) { | ||
return Result::success($arguments[$name]); | ||
} | ||
|
||
return Result::fail(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Router\Tests\HydratorAttribute; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ReflectionFunction; | ||
use Yiisoft\Hydrator\ArrayData; | ||
use Yiisoft\Hydrator\Attribute\Parameter\ToString; | ||
use Yiisoft\Hydrator\AttributeHandling\Exception\UnexpectedAttributeException; | ||
use Yiisoft\Hydrator\AttributeHandling\ParameterAttributeResolveContext; | ||
use Yiisoft\Hydrator\AttributeHandling\ResolverFactory\ContainerAttributeResolverFactory; | ||
use Yiisoft\Hydrator\Hydrator; | ||
use Yiisoft\Hydrator\Result; | ||
use Yiisoft\Router\CurrentRoute; | ||
use Yiisoft\Router\HydratorAttribute\RouteArgument; | ||
use Yiisoft\Router\HydratorAttribute\RouteArgumentResolver; | ||
use Yiisoft\Router\Route as RouterRoute; | ||
use Yiisoft\Test\Support\Container\SimpleContainer; | ||
|
||
final class RouteArgumentTest extends TestCase | ||
{ | ||
public function testBase(): void | ||
{ | ||
$hydrator = $this->createHydrator([ | ||
'a' => 'one', | ||
'b' => 'two', | ||
'c' => 'three', | ||
]); | ||
|
||
$input = new class () { | ||
#[RouteArgument('a')] | ||
public string $a = ''; | ||
#[RouteArgument('b')] | ||
public string $b = ''; | ||
#[RouteArgument] | ||
public string $c = ''; | ||
}; | ||
|
||
$hydrator->hydrate($input); | ||
|
||
$this->assertSame('one', $input->a); | ||
$this->assertSame('two', $input->b); | ||
$this->assertSame('three', $input->c); | ||
} | ||
|
||
public function testWithoutArguments(): void | ||
{ | ||
$hydrator = $this->createHydrator([]); | ||
|
||
$input = new class () { | ||
#[RouteArgument('a')] | ||
public string $a = ''; | ||
#[RouteArgument('b')] | ||
public string $b = ''; | ||
#[RouteArgument] | ||
public string $c = ''; | ||
}; | ||
|
||
$hydrator->hydrate($input); | ||
|
||
$this->assertSame('', $input->a); | ||
$this->assertSame('', $input->b); | ||
$this->assertSame('', $input->c); | ||
} | ||
|
||
public function testUnexpectedAttributeException(): void | ||
{ | ||
$resolver = new RouteArgumentResolver(new CurrentRoute()); | ||
|
||
$attribute = new ToString(); | ||
$context = $this->createParameterAttributeResolveContext(); | ||
|
||
$this->expectException(UnexpectedAttributeException::class); | ||
$this->expectExceptionMessage('Expected "' . RouteArgument::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( | ||
attributeResolverFactory: new ContainerAttributeResolverFactory( | ||
new SimpleContainer([ | ||
RouteArgumentResolver::class => new RouteArgumentResolver($currentRoute), | ||
]) | ||
), | ||
); | ||
} | ||
|
||
private function createParameterAttributeResolveContext(): ParameterAttributeResolveContext | ||
{ | ||
$reflection = new ReflectionFunction(static fn (int $a) => null); | ||
|
||
return new ParameterAttributeResolveContext($reflection->getParameters()[0], Result::fail(), new ArrayData()); | ||
} | ||
} |