定制 bpartner/dto 二次开发

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

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

bpartner/dto

Composer 安装命令:

composer require bpartner/dto

包简介

DTO with simple factory

README 文档

README

Latest Version on Packagist Total Downloads

Simple create DTO from any array.

Installation

####Requirements:

  • PHP 8.0
  • Laravel 8 | 9

You can install the package via composer:

composer require bpartner/dto

Usage

Declare you DTO object. You can use any types declarations.

For array or collection you can use PHPDoc annotation with type of data inside. Supported only array and collection.

class DemoDto extends DtoAbstract
{
    //Optional
    protected const CLASS_FORM_REQUEST = UpdateUserFormRequest::class;
    
    /**
    * DTO fields
    */
    public string $name;
    public Carbon $date;
    public DtoOtherObject $otherObject;
    
    /** @var collection<\App\Dto\DtoOtherObject>  */
    public Collection $objectsCollection;
    
    /** @var array<\App\Dto\DtoOtherObject>  */
    public array $objectsArray;
    
    /** @var collection<\App\Dto\DtoOtherObject>  */
    public array $objectsArrayOfCollection;
    }

Create DTO from any array data (example: request()->all()) by facade Dto

//In any place of your code
$data = request()->all(); //array data
$dto = Dto::build(DemoDto::class, $data);

or from FormRequest

/**
 * In controller
 * 
 * Create FormRequest and assign to CLASS_FORM_REQUEST const in DTO
 * 
 */ 
public function store(DemoDto $dto)
{
    //Use $dto made from UpdateUserFormRequest
}

Now you can transfer your DTO to any Object as parameter and use:

$name = $dto->name;
$objectCollection = $dto->objectsCollection;

You can transform input parameters to any string format by third parameter. (example: first_name -> firstName)

class DemoDto extends DtoAbstract
{
    public string $firstName;
}

$inputData = [
    'first_name' => 'John Doe'
];

$dto = Dto::build(DemoDto::class, $inputData, DtoFactory::CAMEL_CASE);

Custom mapping

You can create DTO with custom mapping. Add static method withMap()

public static function withMap(array $data): DtoInterface
{
    $mappedData = [
        'dto_param' => $data['some_param'],
    ];
    
    return new static($mappedData);
}

//Client code
$dto = Dto::build(DemoDto::class, request()->all());

Transform your DTO to array or flat array.

$array = $dto->toArray();

array:4 [
  "name" => "Demo data"
  "date" => "06-11-2021"
  "phone" => array:2 [
    "type" => "home"
    "number" => "500-123-123"
  ]
  "phones" => array:2 [
    0 => array:2 [
      "type" => "work"
      "number" => "500-123-122"
    ]
    1 => array:2 [
      "type" => "private"
      "number" => "500-123-124"
    ]
  ]
]

$flat = $dto->flatArray();

array:5 [                       
  "name" => "Demo data"         
  "date" => "06-11-2021"        
  "type" => "home"              
  "number" => "500-123-123"     
  "phones" => array:2 [         
    0 => array:2 [              
      "type" => "work"          
      "number" => "500-123-122" 
    ]                           
    1 => array:2 [              
      "type" => "private"       
      "number" => "500-123-124" 
    ]                           
  ]                             
]                               

Important! Collections and nested arrays are not flip to flat array.

Credits

License

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-03-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固