andrey-helldar/simple-data-transfer-object

此包已被废弃且不再维护。作者建议使用 dragon-code/simple-data-transfer-object 包。

简单数据传输对象

2.8.0 2024-03-06 17:31 UTC

This package is auto-updated.

Last update: 2024-03-06 19:41:56 UTC


README

Simple DTO

Stable Version Unstable Version Total Downloads License

安装

要获取最新版本的 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 升级

  1. composer.json 文件中将 "andrey-helldar/simple-data-transfer-object": "^1.0" 替换为 "dragon-code/simple-dto": "^2.0"
  2. 在您的应用程序中将 Helldar\SimpleDataTransferObject 命名空间前缀替换为 DragonCode\SimpleDataTransferObject
  3. 调用 composer update 控制台命令。

dragon-code/simple-data-transfer-object 升级

  1. composer.json 文件中将 dragon-code/simple-data-transfer-object 替换为 dragon-code/simple-dto
  2. 调用 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)
    }
}

获取和设置

您可以在您的应用程序中使用 getset 方法

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;
}