norbertkranitz/gophry-dto-ssp

该软件包最新版本(dev-master)没有提供许可证信息。

dev-master 2016-12-04 21:31 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:31:41 UTC


README

Gophry DTO 服务提供商帮助您将 gophry-dto (了解更多) 集成到您的 Silex 应用程序中。

此 Silex 服务提供商允许您在 Silex 动作中使用经过验证的请求对象 (了解更多)。

该提供程序合并了请求正文和请求查询参数,并将它们的组合转换为预定义的 \Gophry\DTO\RequestDTO 基于对象。

当然,您可以在 DTO 对象上定义一些验证规则 - 使用内置的 ValidationServiceProvider (了解更多) - 当控制器动作被触发时,您将有一个经过验证的请求对象,因为请求验证是首先完成的。如果您的请求数据无效,则提供程序将抛出 \Gophry\Provider\DTO\InvalidRequestException,因此服务器将以状态代码 422 Unprocessable Entity 响应,因为这个异常是 \Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException 的扩展。

如果您想以其他方式处理这些无效请求,则可以轻松配置提供程序(请参阅以下内容)。您需要做的事情只是更改异常类值。

安装

您可以使用 composer 安装此软件包(了解更多)。

composer require norbertkranitz/gophry-dto-spp "dev-master"

配置

$app['gophry.invalid.request.exception.class'] = \My\Exception\Class;

异常的第一个参数是消息,第二个参数是验证错误列表。

注册

您可以将此服务提供商简单地注册为常见的 Silex 服务提供商(了解更多

$app->register(new DTOServiceProvider());

简单使用

假设您在 POST /login 端点上有登录动作,并且客户端还发送了 JSON 请求正文。

{
    "email": "test@user.app",
    "password": "password"
}

创建 LogiRequestDTO 类

use \Symfony\Component\Validator\Mapping\ClassMetadata;
use \Symfony\Component\Validator\Constraints as Assert;

class LoginRequestDTO extends \Gophry\DTO\RequestDTO {

    protected $email;
    
    protected $password;
        
    public function getEmail() {
        return $this->email;
    }

    public function getPassword() {
        return $this->password;
    }

    static public function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addPropertyConstraint('email', new Assert\NotBlank());
        $metadata->addPropertyConstraint('email', new Assert\Email());
        $metadata->addPropertyConstraint('password', new Assert\NotBlank());
        $metadata->addPropertyConstraint('password', new Assert\Length(['min' => 5]));
    }

}

注册端点

$app->post('/login', function(LoginRequestDTO $dto) {
    //do something with the valid request data
});

请查看动作参数中的类型提示。扩展控制器解析器通过反射方法获取引用的类型,因此它可以提供参数实例

如果您想验证上面编写的请求,别忘了注册 ValidatorServiceProvider

高级使用

假设客户端发送了一个多级 JSON 请求正文以创建新用户。

{
    "email": "test@user.app",
    "password": "password",
    "name": {
        "first_name": "First",
        "last_name": "Last",
    }
}

创建请求 NameRequestDTO 类

use \Symfony\Component\Validator\Mapping\ClassMetadata;
use \Symfony\Component\Validator\Constraints as Assert;

class NameRequestDTO extends \Gophry\DTO\RequestDTO {

    protected $first_name;
    
    protected $last_name;
        
    public function getFirstName() {
        return $this->first_name;
    }

    public function getLastName() {
        return $this->last_name;
    }

    static public function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addPropertyConstraint('first_name', new Assert\NotBlank());
        $metadata->addPropertyConstraint('last_name', new Assert\NotBlank());
    }

}

创建请求 RegisterRequestDTO 类

use \Symfony\Component\Validator\Mapping\ClassMetadata;
use \Symfony\Component\Validator\Constraints as Assert;

class RegisterRequestDTO extends \Gophry\DTO\RequestDTO {

    protected $email;
    
    protected $password;

    protected $name;

    //Important!
    public function __construct() {
        $this->name = new NameRequestDTO();
    }
        
    public function getEmail() {
        return $this->email;
    }

    public function getPassword() {
        return $this->password;
    }

    static public function loadValidatorMetadata(ClassMetadata $metadata)
    {
        $metadata->addPropertyConstraint('email', new Assert\NotBlank());
        $metadata->addPropertyConstraint('email', new Assert\Email());
        $metadata->addPropertyConstraint('password', new Assert\NotBlank());
        $metadata->addPropertyConstraint('password', new Assert\Length(['min' => 5]));
        $metadata->addPropertyConstraint('name', new Assert\Valid());
    }

}

处理 URI 属性

URI 属性首先传递。如果您的端点定义了 URI 属性并使用请求正文,只需将它们传递给动作即可。

$app->post('/login/{attribute}', function($attribute, LoginRequestDTO $dto) {
    //do something with the attribute and the valid request data
});

对于子 DTO,\Gophry\DTO\RequestDTO 需要将其作为空对象传递,这可以在构造函数中初始化

在请求 DTO 类中使用“Request”一词是一个好习惯,因为 Gophry 也可以处理 Response DTO!

此提供程序针对 JSON 通信进行了优化,也仅在 Silex 上进行了测试!