定制 laxity7/dto 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laxity7/dto

最新稳定版本:v1.1.4

Composer 安装命令:

composer require laxity7/dto

包简介

An easy-to-use generic DTO package for PHP

README 文档

README

Fast and light Data Transfer Object (DTO) with nested objects and attribute change control, without checking scalar types, use PHP type casting to check them.

License Latest Stable Version Total Downloads

You can control whether the DTO can be mutable, but by default, it is immutable. To be able to change, use public properties or public setters.

This package supports PHP 8.1+

Install

Install via composer

composer require laxity7/dto

How to use

<?php

use Laxity7\DataTransferObject;

require_once 'vendor/autoload.php';

/**
 * @property-read string $time
 */
class FooDto extends DataTransferObject
{
    public int $id;
    public string $name;
    public array $data; // just array
    /** @var BarDto[] */
    public array $bars; // array of objects BarDto
    public BarDto $bar;
    /** @var BarDto */
    public $baz;
    public BarReadonlyDto $readonlyBar;
    public ReadonlyDto $readonly;
    /** @var ReadonlyDto[] */
    public array $readonlyArr;
    protected string $time;

    // setter can be protected/public
    // setter has higher priority than field
    protected function setName(string $name): void
    {
        $this->name = 'Foo' . $name;
    }

    protected function getTime(): string
    {
        return (new DateTime($this->time))->format('H:i:s');
    }
}

// optional to inherit DataTransferObject
class BarDto
{
    public int $id;
}

// optional to inherit DataTransferObject
class BarReadonlyDto
{
    public function __construct(
        readonly public int $id
    ){}
}

class ReadonlyDto extends DataTransferObject
{
    public function __construct(
        readonly public string $foo,
        readonly public string $bar,
    ) {
        parent::__construct();
    }
}

$fooDto = new FooDto([
    'id' => 1,
    'name' => 'Bar', // FooBar
    'data' => [1, 2, 3],
    'bars' => [ // array of objects
        ['id' => 1], // transforms into an object BarDto
        ['id' => 2], // transforms into an object BarDto
    ],
    'bar' => ['id' => 3], // transforms into an object BarDto
    'baz' => new BarReadonlyDto(4), // just set object
    'readonlyBar' => ['id' => 5], // transforms into an object BarReadonlyDto
    'readonly' => [ // transforms into an object ReadonlyDto
        'bar' => 'baz',
        'foo' => 'gaz',
    ],
    'readonlyArr' => [ // array of objects
        ['bar' => 'baz', 'foo' => 'gaz'], // transforms into an object ReadonlyDto
        ['bar' => 'baz1', 'foo' => 'gaz1'], // transforms into an object ReadonlyDto
    ],
    'time' => '05:59',
]);

// Get all attributes
$arr = $fooDto->toArray();
// Serialize to json (also serializes all nested DTOs)
$json = json_encode($fooDto);

统计信息

  • 总下载量: 21
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固