dragon-code/simple-dto
最新稳定版本:2.8.0
Composer 安装命令:
composer require dragon-code/simple-dto
包简介
Simple Data Transfer Objects
README 文档
README
Tip
Use the spatie/laravel-data instead.
Installation
To get the latest version of Simple Data Transfer Object, simply require the project using Composer:
composer require dragon-code/simple-dto
Or manually update require block of composer.json and run composer update.
{
"require": {
"dragon-code/simple-dto": "^2.0"
}
}
Upgrade from andrey-helldar/simple-data-transfer-object
- Replace
"andrey-helldar/simple-data-transfer-object": "^1.0"with"dragon-code/simple-dto": "^2.0"in thecomposer.jsonfile; - Replace
Helldar\SimpleDataTransferObjectnamespace prefix withDragonCode\SimpleDataTransferObjectin your application; - Call the
composer updateconsole command.
Upgrade from dragon-code/simple-data-transfer-object
- Replace
dragon-code/simple-data-transfer-objectwithdragon-code/simple-dtoin thecomposer.jsonfile; - Call the
composer updateconsole command.
Using
Basic
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
Mappings
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
Casts
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
Nested Objects
With the help of casts, you can also easily create nested objects:
use Tests\Fixtures\Nested\Company; $company = Company::make([ 'title' => 'First Company', 'projects' => [ [ 'title' => 'Project 1', 'domain' => 'https://example.com', 'developers' => [ [ 'name' => 'Andrey Helldar', 'email' => 'helldar@ai-rus.com', ], [ 'name' => 'John Doe', 'email' => 'doe@example.com', ], ], ], ], ]); $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 } }
For example, casting test company object:
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); } }
From
Array
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); } }
Object
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); } }
Request
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 And Set
You can use the get and set methods in your app:
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
Helpers
For your convenience, starting from version 2.17 of
the dragon-code/contracts package, we have added a new interface that declares the implementation of the
public dto method. This way you can better control your application to return DTO objects.
Of course, don't forget to implement the interface ????
For example:
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; }
dragon-code/simple-dto 适用场景与选型建议
dragon-code/simple-dto 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.12M 次下载、GitHub Stars 达 8, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dto」 「dragon」 「data-transfer-object」 「andrey-helldar」 「dragon-code」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dragon-code/simple-dto 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dragon-code/simple-dto 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dragon-code/simple-dto 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Performing any actions during the deployment process
Support package is a collection of helpers and tools for any project.
Simple conversion of an array to a pretty view
Various helper files for the Laravel and Lumen frameworks
Data transfer objects with batteries included
Automatically always return a response in JSON format
统计信息
- 总下载量: 2.12M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 31
- 依赖项目数: 16
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04