定制 mohamad-zatar/laravel-dto-tools 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

mohamad-zatar/laravel-dto-tools

最新稳定版本:1.0.0

Composer 安装命令:

composer require mohamad-zatar/laravel-dto-tools

包简介

A set of tools to make working with Data Transfer Objects easier in Laravel

README 文档

README

This package provides a comprehensive set of tools designed to simplify working with Data Transfer Objects (DTOs). It features an artisan command for generating DTOs and includes a facade to facilitate their hydration.

Installation

composer require mohamad-zatar/laravel-dto-tools

Usage

To generate a new DTO all you need to do is run the following artisan command:

php artisan make:dto MyDto

This will generate the following class: app/DataObjects/MyDto.php. By default this class will be a final class that implements a DataObjectContract which enforces a method toArray so that you can easily cast your DTOs to arrays.

If you are using PHP 8.2 or 8.3 however, you will by default get a readonly class generated, so that you do not have to declare each property as readonly.

To work with the hydration functionality you can either use Laravels DI container, or the ready made facade.

Using the container:

class InventoryController
{
    public function __construct(
        private readonly HydratorContract $hydrator,
    ) {}
    
    public function __invoke(InventoryRequest $request)
    {
        $model = Model::query()->create(
            attributes: $this->hydrator->fill(
                class: ModelObject::class,
                parameters: $request->validated(),
            )->toArray(),
        );
    }
}

To work with the facade, you can do something very similar:

class InventoryController
{
    public function __invoke(InventoryRequest $request)
    {
        $model = Model::query()->create(
            attributes: Hydrator::fill(
                class: ModelObject::class,
                parameters: $request->validated(),
            )->toArray(),
        );
    }
}

Object Hydration

Under the hood this package uses an EventSauce package, created by Frank de Jonge. It is possibly the best package I have found to hydrate objects nicely in PHP. You can find the documentation here if you would like to see what else you are able to do with the package to suit your needs.

Testing

To run the test suite:

composer run test

LICENSE

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-23