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
其他信息
- 授权协议: MIT
- 更新时间: 2024-08-23