yiisoft/request-model

Yii Framework Request Model

dev-master / 1.0.x-dev 2021-09-07 21:00 UTC

This package is auto-updated.

Last update: 2021-09-07 21:00:41 UTC


README

68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667

Yii Request Model


Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage

Request model simplifies working with request data. It allows you to decorate data for easy retrieval and automatically validate it when needed.

Installation

The package could be installed with composer:

composer require yiisoft/request-model

General usage

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

use Yiisoft\RequestModel\RequestModel;
use Yiisoft\RequestModel\ValidatableModelInterface;
use Yiisoft\Validator\Rule\Required;

final class AuthRequest extends RequestModel implements ValidatableModelInterface
{
    public function getLogin(): string
    {
        return (string)$this->getAttributeValue('body.login');
    }

    public function getPassword(): string
    {
        return (string)$this->getAttributeValue('body.password');
    }

    public function getRules(): array
    {
        return [
            'body.login' => [
                Required::rule(),
            ],
            'body.password' => [
                Required::rule(),
            ]
        ];
    }
}

Usage in controller:

use Psr\Http\Message\ResponseInterface;

final class SimpleController
{
    public function action(AuthRequest $request): ResponseInterface
    {
        echo $request->getLogin();
        ...
    }
}

If the data does not pass validation, RequestValidationException will be thrown. If you need to handle an exception and, for example, send a response, you can intercept its middleware.

For example:

final class ExceptionMiddleware implements MiddlewareInterface
{
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        try {
            return $handler->handle($request);
        } catch (RequestValidationException $e) {
            return new Response(400, [], $e->getFirstError());
        }
    }
}

You can use the request model without validation. To do this, you need to remove the ValidatableModelInterface. In this case, the data will be included into the model, but will not be validated. For example:

final class ViewPostRequest extends RequestModel
{
    public function getId(): int
    {
        return (int)$this->getAttributeValue('attributes.id');
    }
}

Inside the request model class, data is available using the following keys:

key source
query $request->getQueryParams()
body $request->getParsedBody()
attributes $request->getAttributes()
headers $request->getHeaders()
files $request->getUploadedFiles()
cookie $request->getCookieParams()

This data can be obtained as follows

$this->requestData['attributes.id'];

or through the methods

$this->hasAttribute('body.user_id');
$this->getAttributeValue('body.user_id');

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework. To run it:

./vendor/bin/infection

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

License

The Yii Request Model is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.