承接 wendelladriel/laravel-validated-dto 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wendelladriel/laravel-validated-dto

Composer 安装命令:

composer require wendelladriel/laravel-validated-dto

包简介

Data Transfer Objects with validation for Laravel applications

README 文档

README

Validated DTO for Laravel

Validated DTO for Laravel

Data Transfer Objects with validation for Laravel applications

Packagist PHP from Packagist Laravel versions GitHub Workflow Status (main) Total Downloads

Installation

You can install the package via composer:

composer require wendelladriel/laravel-validated-dto

You can publish the config file with:

php artisan vendor:publish --tag="validated-dto"

Usage

Create a DTO by extending ValidatedDTO, declaring typed properties, and defining the validation rules for incoming data:

use Illuminate\Validation\Rules\Password;
use WendellAdriel\ValidatedDTO\Casting\BooleanCast;
use WendellAdriel\ValidatedDTO\Casting\StringCast;
use WendellAdriel\ValidatedDTO\ValidatedDTO;

final class UserDTO extends ValidatedDTO
{
    public string $name;

    public string $email;

    public string $password;

    public bool $active;

    protected function rules(): array
    {
        return [
            'name' => ['required', 'string'],
            'email' => ['required', 'email'],
            'password' => ['required', Password::min(8)],
            'active' => ['sometimes', 'boolean'],
        ];
    }

    protected function defaults(): array
    {
        return [
            'active' => true,
        ];
    }

    protected function casts(): array
    {
        return [
            'name' => new StringCast(),
            'email' => new StringCast(),
            'password' => new StringCast(),
            'active' => new BooleanCast(),
        ];
    }
}

Create an instance from an array, request, JSON string, Eloquent model, or artisan command:

$dto = UserDTO::fromArray([
    'name' => 'John Doe',
    'email' => 'john.doe@example.com',
    'password' => 's3CreT!@1a2B',
]);

$dto->name; // John Doe
$dto->toArray();

You can also inject a DTO into a controller action and let the service container resolve it from the current request:

use Illuminate\Http\JsonResponse;

final class StoreUserController
{
    public function __invoke(UserDTO $dto): JsonResponse
    {
        return response()->json($dto->toArray());
    }
}

Access the full documentation here.

Changelog

Please see the changelog for more information on what has changed recently.

Contributing

Thank you for considering contributing to Validated DTO for Laravel! You can read the contribution guide here.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

Validated DTO for Laravel is open-sourced software licensed under the MIT license.

统计信息

  • 总下载量: 645.1k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 762
  • 点击次数: 12
  • 依赖项目数: 11
  • 推荐数: 0

GitHub 信息

  • Stars: 761
  • Watchers: 4
  • Forks: 47
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-12-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固