dragon-code / simple-data-transfer-object
2.8.0
2024-03-06 17:31 UTC
Requires
- php: ^7.3 || ^8.0
- ext-json: *
- dragon-code/contracts: ^2.17
- dragon-code/support: ^5.0 || ^6.0
- symfony/http-foundation: ^4.0 || ^5.0 || ^6.0 || ^7.0
Requires (Dev)
- phpunit/phpunit: ^9.6
- symfony/var-dumper: ^4.3 || ^5.4 || ^6.0 || ^7.0
Conflicts
README
安装
要获取最新版本的 Simple Data Transfer Object
,只需使用 Composer 依赖该项目。
composer require dragon-code/simple-dto
或者手动更新 composer.json
中的 require
块并运行 composer update
。
{ "require": { "dragon-code/simple-dto": "^2.0" } }
从 andrey-helldar/simple-data-transfer-object
升级
- 在
composer.json
文件中将"andrey-helldar/simple-data-transfer-object": "^1.0"
替换为"dragon-code/simple-dto": "^2.0"
; - 在您的应用程序中将
Helldar\SimpleDataTransferObject
命名空间前缀替换为DragonCode\SimpleDataTransferObject
; - 调用
composer update
控制台命令。
从 dragon-code/simple-data-transfer-object
升级
- 在
composer.json
文件中将dragon-code/simple-data-transfer-object
替换为dragon-code/simple-dto
; - 调用
composer update
控制台命令。
使用
基本
namespace App\DTO; use DragonCode\SimpleDataTransferObject\DataTransferObject; class YourInstance extends DataTransferObject { public $foo; public $bar; public $baz; public $qwerty; } $instance = YourInstance::make([ 'foo' => 'Foo', 'bar' => 'Bar', 'baz' => 'Baz' ]); return $instance->foo; // Foo return $instance->bar; // Bar return $instance->baz; // Baz return $instance->qwerty; // null
映射
namespace App\DTO; use DragonCode\SimpleDataTransferObject\DataTransferObject; class YourInstance extends DataTransferObject { public $foo; public $bar; public $baz; public $qwerty; protected $map = [ 'data.foo' => 'foo', 'data.bar' => 'bar', ]; } $instance = YourInstance::make([ 'data' => [ 'foo' => 'Foo', 'bar' => 'Bar' ], 'baz' => 'Baz' ]); return $instance->foo; // Foo return $instance->bar; // Bar return $instance->baz; // Baz return $instance->qwerty; // null
类型转换
namespace App\DTO; use DragonCode\SimpleDataTransferObject\DataTransferObject; use DragonCode\Support\Facades\Helpers\Str; class YourInstance extends DataTransferObject { public $foo; public $bar; public $baz; public $qwerty; protected $map = [ 'data.foo' => 'foo', 'data.bar' => 'bar', ]; protected function castFoo($value) { return Str::upper($value); } } $instance = YourInstance::make([ 'data' => [ 'foo' => 'Foo', 'bar' => 'Bar' ], 'baz' => 'Baz' ]); return $instance->foo; // FOO return $instance->bar; // Bar return $instance->baz; // Baz return $instance->qwerty; // null
嵌套对象
借助类型转换,您还可以轻松创建嵌套对象
use Tests\Fixtures\Nested\Company; $company = Company::make([ 'title' => 'First Company', 'projects' => [ [ 'title' => 'Project 1', 'domain' => 'https://example.com', 'developers' => [ [ 'name' => 'Andrey Helldar', 'email' => '[email protected]', ], [ 'name' => 'John Doe', 'email' => '[email protected]', ], ], ], ], ]); $company->title; // First Company foreach ($company->projects as $project) { $project->title; // 0: Project 1 foreach ($project->developers as $developer) { $developer->name; // 0: Andrey Helldar // 1: John Doe } }
例如,转换测试公司对象
namespace Tests\Fixtures\Nested; use DragonCode\SimpleDataTransferObject\DataTransferObject; class Company extends DataTransferObject { public string $title; /** @var \Tests\Fixtures\Nested\Project[] */ public array $projects; protected function castProjects(array $projects): array { return array_map(static function (array $project) { return Project::make($project); }, $projects); } }
从
数组
use DragonCode\Contracts\DataTransferObject\DataTransferObject; use Tests\Fixtures\Simple; class Foo { protected array $items = [ 'foo' => 'Foo', 'bar' => 'Bar', ]; protected function dto(): DataTransferObject { return Simple::fromArray($this->items); } }
JSON
use DragonCode\Contracts\DataTransferObject\DataTransferObject; use Tests\Fixtures\Simple; class Foo { protected string $json = '{"foo":"Foo","bar":"Bar"}'; protected function dto(): DataTransferObject { return Simple::fromJson($this->json); } }
对象
use DragonCode\Contracts\DataTransferObject\DataTransferObject; use Tests\Fixtures\CustomObject; use Tests\Fixtures\Simple; class Foo { protected CustomObject $object; protected function dto(): DataTransferObject { return Simple::fromObject($this->object); } }
请求
use DragonCode\Contracts\DataTransferObject\DataTransferObject; use Symfony\Component\HttpFoundation\Request; use Tests\Fixtures\Simple; class Foo { protected Request $request; protected function dto(): DataTransferObject { return Simple::fromRequest($this->request) } }
获取和设置
您可以在应用程序中使用 get
和 set
方法
namespace App\DTO; use DragonCode\SimpleDataTransferObject\DataTransferObject; class YourInstance extends DataTransferObject { public $foo; } $instance = YourInstance::make([ 'foo' => 'none', ]); return $instance->foo; // none return $instance->get('foo'); // none $instance->set('foo', 'Foo'); return $instance->foo; // Foo
辅助函数
为了您的方便,从 2.17 版本的 dragon-code/contracts
包开始,我们添加了一个新的接口,该接口声明了公共 dto
方法的实现。这样,您可以更好地控制应用程序以返回 DTO 对象。
当然,别忘了实现该接口哦!
例如
namespace App\Http\Requests\Companies; use App\Objects\Company; use DragonCode\Contracts\DataTransferObject\DataTransferObject; use DragonCode\Contracts\DataTransferObject\Dtoable; class CreateRequest implements Dtoable { // ... public function dto(): DataTransferObject { return Company::fromRequest($this); } } // Other place public function store(CreateRequest $request) { $name = $request->dto()->name; }