juststeveking/laravel-data-object-tools 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

juststeveking/laravel-data-object-tools

最新稳定版本:2.0.0

Composer 安装命令:

composer require juststeveking/laravel-data-object-tools

包简介

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

README 文档

README

Latest Version PHP Version Tests Total Downloads

This package is aimed to be a suite of tools to help working with DTOs even easier. It includes an artisan command to generate DTOs as well as a facade to help you hydrate them.

Installation

composer require juststeveking/laravel-data-object-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 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 StoreController
{
    public function __construct(
        private readonly HydratorContract $hydrator,
    ) {}
    
    public function __invoke(StoreRequest $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 StoreController
{
    public function __invoke(StoreRequest $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

Credits

LICENSE

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

统计信息

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

GitHub 信息

  • Stars: 67
  • Watchers: 2
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-07-27

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固