Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Rework attributes (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
rustamwin committed Jan 20, 2023
1 parent 0bde90f commit 425000d
Show file tree
Hide file tree
Showing 36 changed files with 491 additions and 506 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -30,8 +30,12 @@ The package could be installed with composer:
composer require yiisoft/request-model
```

According to [`yiisoft/middleware-dispatcher`](https://github.com/yiisoft/middleware-dispatcher) docs, you need to set
the implementation of `ParametersResolverInterface` to `HandlerParametersResolver` via container or pass directly.

## General usage


A simple version of the request model looks like the following:

```php
Expand Down Expand Up @@ -72,7 +76,7 @@ Middleware:
```php
Route::post('/test')
->middleware(
fn(\Yiisoft\Middleware\Dispatcher\WrapperFactoryInterface $factory) => $factory->createActionWrapper(
fn(\Yiisoft\Middleware\Dispatcher\MiddlewareFactory $factory) => $factory->create(
SimpleController::class,
'action'
)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -26,7 +26,7 @@
"psr/http-server-middleware": "^1.0",
"yiisoft/arrays": "^2.0|^3.0",
"yiisoft/injector": "^1.0",
"yiisoft/middleware-dispatcher": "^3.0|^4.0",
"yiisoft/middleware-dispatcher": "^5.0",
"yiisoft/router": "^1.2|^2.0",
"yiisoft/validator": "dev-master"
},
Expand Down
46 changes: 0 additions & 46 deletions src/ActionWrapper.php

This file was deleted.

12 changes: 8 additions & 4 deletions src/Attribute/Body.php
Expand Up @@ -6,16 +6,20 @@

use Attribute;

#[Attribute(flags: Attribute::TARGET_PARAMETER)]
#[Attribute(flags: Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)]
final class Body implements HandlerParameterAttributeInterface
{
public function getType(): string
public function __construct(private ?string $name = null)
{
return self::REQUEST_BODY;
}

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

public function getResolverClassName(): string
{
return BodyResolver::class;
}
}
23 changes: 23 additions & 0 deletions src/Attribute/BodyResolver.php
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestModel\Attribute;

use Psr\Http\Message\ServerRequestInterface;

final class BodyResolver implements HandlerParameterResolverInterface
{
public function resolve(HandlerParameterAttributeInterface $attribute, ServerRequestInterface $request): mixed
{
if ($attribute::class !== Body::class) {
throw new \InvalidArgumentException(sprintf('Expected "%s", got "%s".', Body::class, $attribute::class));
}

if ($attribute->getName() !== null) {
return $request->getParsedBody()[$attribute->getName()] ?? null;
}

return $request->getParsedBody();
}
}
10 changes: 1 addition & 9 deletions src/Attribute/HandlerParameterAttributeInterface.php
Expand Up @@ -9,13 +9,5 @@
*/
interface HandlerParameterAttributeInterface
{
public const ROUTE_PARAM = 'route_param';
public const REQUEST_BODY = 'request_body';
public const REQUEST_ATTRIBUTE = 'request_attribute';
public const UPLOADED_FILES = 'uploaded_files';
public const QUERY_PARAM = 'query_param';

public function getName(): ?string;

public function getType(): string;
public function getResolverClassName(): string;
}
15 changes: 15 additions & 0 deletions src/Attribute/HandlerParameterResolverInterface.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestModel\Attribute;

use Psr\Http\Message\ServerRequestInterface;

/**
* Represents action handler parameter [attribute](https://www.php.net/manual/en/language.attributes.php).
*/
interface HandlerParameterResolverInterface
{
public function resolve(HandlerParameterAttributeInterface $attribute, ServerRequestInterface $request): mixed;
}
8 changes: 4 additions & 4 deletions src/Attribute/Query.php
Expand Up @@ -9,17 +9,17 @@
#[Attribute(flags: Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)]
final class Query implements HandlerParameterAttributeInterface
{
public function __construct(private string $name)
public function __construct(private ?string $name = null)
{
}

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

public function getType(): string
public function getResolverClassName(): string
{
return self::QUERY_PARAM;
return QueryResolver::class;
}
}
23 changes: 23 additions & 0 deletions src/Attribute/QueryResolver.php
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestModel\Attribute;

use Psr\Http\Message\ServerRequestInterface;

final class QueryResolver implements HandlerParameterResolverInterface
{
public function resolve(HandlerParameterAttributeInterface $attribute, ServerRequestInterface $request): mixed
{
if ($attribute::class !== Query::class) {
throw new \InvalidArgumentException(sprintf('Expected "%s", got "%s".', Query::class, $attribute::class));
}

if ($attribute->getName() !== null) {
return $request->getQueryParams()[$attribute->getName()] ?? null;
}

return $request->getQueryParams();
}
}
8 changes: 4 additions & 4 deletions src/Attribute/Request.php
Expand Up @@ -13,13 +13,13 @@ public function __construct(private string $name)
{
}

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

public function getName(): ?string
public function getResolverClassName(): string
{
return $this->name;
return RequestResolver::class;
}
}
19 changes: 19 additions & 0 deletions src/Attribute/RequestResolver.php
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestModel\Attribute;

use Psr\Http\Message\ServerRequestInterface;

final class RequestResolver implements HandlerParameterResolverInterface
{
public function resolve(HandlerParameterAttributeInterface $attribute, ServerRequestInterface $request): mixed
{
if ($attribute::class !== Request::class) {
throw new \InvalidArgumentException(sprintf('Expected "%s", got "%s".', Request::class, $attribute::class));
}

return $request->getAttribute($attribute->getName());
}
}
4 changes: 2 additions & 2 deletions src/Attribute/Route.php
Expand Up @@ -18,8 +18,8 @@ public function getName(): string
return $this->name;
}

public function getType(): string
public function getResolverClassName(): string
{
return self::ROUTE_PARAM;
return RouteResolver::class;
}
}
24 changes: 24 additions & 0 deletions src/Attribute/RouteResolver.php
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestModel\Attribute;

use Psr\Http\Message\ServerRequestInterface;
use Yiisoft\Router\CurrentRoute;

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

public function resolve(HandlerParameterAttributeInterface $attribute, ServerRequestInterface $request): mixed
{
if ($attribute::class !== Route::class) {
throw new \InvalidArgumentException(sprintf('Expected "%s", got "%s".', Route::class, $attribute::class));
}

return $this->currentRoute->getArgument($attribute->getName());
}
}
9 changes: 2 additions & 7 deletions src/Attribute/UploadedFiles.php
Expand Up @@ -9,13 +9,8 @@
#[Attribute(flags: Attribute::TARGET_PARAMETER)]
final class UploadedFiles implements HandlerParameterAttributeInterface
{
public function getType(): string
public function getResolverClassName(): string
{
return self::UPLOADED_FILES;
}

public function getName(): ?string
{
return null;
return UploadedFilesResolver::class;
}
}
19 changes: 19 additions & 0 deletions src/Attribute/UploadedFilesResolver.php
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Yiisoft\RequestModel\Attribute;

use Psr\Http\Message\ServerRequestInterface;

final class UploadedFilesResolver implements HandlerParameterResolverInterface
{
public function resolve(HandlerParameterAttributeInterface $attribute, ServerRequestInterface $request): array
{
if ($attribute::class !== UploadedFiles::class) {
throw new \InvalidArgumentException(sprintf('Expected "%s", got "%s".', UploadedFiles::class, $attribute::class));
}

return $request->getUploadedFiles();
}
}
64 changes: 0 additions & 64 deletions src/CallableWrapper.php

This file was deleted.

0 comments on commit 425000d

Please sign in to comment.