bkief29/laravel-dto
Composer 安装命令:
composer require bkief29/laravel-dto
包简介
Data Transfer Objects complete with castable attributes and validation.
README 文档
README
Data Transfer Objects complete with castable attributes and validation.
TODO
Example
<?php namespace Domain\DTO\Requests; use bkief29\DTO\DataTransferObject; /** * Class PricesRequest. */ class PricesRequest extends DataTransferObject { /** * @var string */ public $serviceCode; /** * @var string */ public $effectiveDate; /** * @var int */ public $quantity; protected $casts = [ 'serviceCode' => 'string', 'quantity' => 'int', ]; protected $dates = [ 'effectiveDate' ]; public function getEffectiveDateAttribute($date) { return $date->format($this->getDateFormat()); } // OR public function getEffectiveDateAttribute() { return $this->getOriginal('effectiveDate')->format($this->getDateFormat()); } }
Usage
Mutators
class User extends DataTransferObject { ... public function getNameAttribute($value) { return ucwords($value); } // OR public function getNameAttribute() { return ucwords($this->getOriginal('name')); } }
echo $array['name']; // john smith $user = new User($array); echo $user->name; // John Smith echo $user['name']; // John Smith echo $user->getAttribute('name'); // John Smith echo $user->getOriginal('name'); // john smith
Types
Cast variables to other DTOs automatically
class PostData extends DataTransferObject { /** @var string */ public $title; /** @var string|null */ public $body; /** @var App\DataTransferObjects\Author */ public $author; /** @var App\DataTransferObjects\Tag[] */ public $tags; }
$postData = new $postData($array); $postData->author; // Instance of App\DataTransferObjects\Author $postData->tags; // Array of App\DataTransferObjects\Tag
Helpers
$postData->all(); $postData ->only('title', 'body') ->toArray(); $postData ->except('author') ->toArray(); $postData->toCollection();
统计信息
- 总下载量: 626
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-18