摘要Dto

维护者

详细信息

github.com/laradev/dto

源代码

问题

安装: 123

依赖关系: 0

建议者: 0

安全: 0

星星: 0

关注者: 0

分支: 1

开放问题: 0

类型:

dev-main 2024-09-19 20:11 UTC

This package is not auto-updated.

Last update: 2024-09-19 20:11:43 UTC


README

此PHP包实现了DTO功能。此抽象对象可以让您快速将您的命名数据结构转换为已知对象。其他开发者会感谢您使用这个功能 :)

入门

    composer require wearelaradev/dto

用法

简单用法

首先创建一个表示您的数据结构的对象。例如,我们将选择创建一个具有电子邮件和密码属性的“用户”对象

<?php

use Laradev\Dto;

class UserDto extends Dto
{
    private string $password;
    public string $email;

    public function setPassword(string $password): self
    {
        $this->password = $password;
        return $this;
    }
    
    public function getPassword(): string 
    {
        return $this->password;
    }
    

    public function toArray(): array
    {
        return [
            'password'  => $this->password,
            'email'     => $this->email,
        ];
    }
}

$user = UserDto::create([
    "password" => "Test1234",
    "email" => "fake@fake.com"
]);

echo $user->email // "fake@fake.com"
echo $userDto->toJson(); // {"password":"Test1234","email":"fake@fake.com"}
echo $userDto->toArray(); // ["password" => "Test1234","email" => "fake@fake.com"]

高级用法

您是否有可能需要对象嵌套的复杂数据结构?那么简单地创建另一个DTO!让我们想象一下,当我们的用户被创建时,负载还需要结构化数据来创建一个配置文件。因此,我们创建了一个ProfileDto,并将配置文件属性添加到我们的UserDto中

<?php

use Laradev\Dto;

enum ProfileStatus: string
{
    case PUBLIC = "public";
    case PRIVATE = "private";
}

class ProfileDto extends Dto 
{
    public string $firstname;
    public string $lastname;
    public ProfileStatus $status;
    
    public function toArray(): array
    {
        return [
            "firstname" => $this->firstname,
            "lastname"  => $this->lastname,
            "status"    => $this->status->value
        ];
    }
}

class UserDto extends Dto 
{
    // ... LOGIC
    
    public ProfileDto $profile;
    
    // ... LOGIC
    
    public function toArray(): array
    {
        return [
            "password"  => $this->password,
            "email"     => $this->email,
            "profile"   => $this->profile->toArray()
        ];
    }
}

$user = UserDto::create([
    "password" => "Test1234",
    "email" => "fake@fake.com",
    "profile" => [
        "firstname" => "John",
        "lastname" => "Doe",
        "status" => "public"
    ]
]);

echo "{$user->profile->firstname} {$user->profile->lastname}" // John Doe

可用方法

/**
 * This method sets the object's properties from an array or another object. 
 * It uses reflection to detect object properties and initializes them if possible.
 */
static create(array $data): self

/**
 * This method sets the object's properties from an array. 
 * It uses reflection to detect object properties and initializes them if possible.
 */
fromArray(array $data): self

/**
 * This method sets the object's properties from another object. 
 * It uses reflection to detect object properties and initializes them if possible.
 */
fromObject(array $data): self

/**
 * This abstract method must be implemented in each child class to convert the object into an array.
 */
toArray(): array

/**
 * This method converts the object into JSON by calling toArray() and encoding the result in JSON.
 */
toJson(): string

/**
 * This method clone the current dto 
 */
clone(): self

/**
 * This method returns an array of property names that have been initialized via setData.
 */
getInitializedProperties(): array

附加信息

如果您遇到错误或对改进有任何想法,请不要犹豫,发送给我一个PR或通过电子邮件联系我 florian@laradev.ca :)