定制 idoit/dto 二次开发

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

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

idoit/dto

最新稳定版本:v1.0.0

Composer 安装命令:

composer require idoit/dto

包简介

i-doit DTO package provides infrastructure to work with DTO

README 文档

README

This library provides methods to simplify working with DTOs in PHP.

It provides serialization and validation helpers to define behaviour of the DTO via attributes.

Setup

Require library via composer

composer require idoit/dto

Usage

For the following examples we will use this DTO class that defines some public readonly properpties. Please note the attributes which will be used for validation and, in case of DateFormat, serialization:

use Idoit\Dto\Validation\IsNull;
use Idoit\Dto\Validation\OrX;
use Idoit\Dto\Validation\PositiveInteger;
use Idoit\Dto\Validation\Required;
use Idoit\Dto\Serialization\DateFormat;

/**
 * Object read DTO. Will be returned when reading objects via processor.
 */
class MyDto
{
    public function __construct(
        #[Required]
        #[PositiveInteger]
        public readonly int $id = 0,
        
        public readonly string|null $title = null,
        
        #[OrX(new IsNull(), new PositiveInteger())]
        public readonly int|null $type = null,
        
        #[DateFormat]
        public readonly DateTime|null $created = null
        // ...
    ) {
    }
}

Validation

In order to validate this DTO, you can use Validation::validate($dto);, like so:

use Idoit\Dto\Validation\Validation;

$dto = new MyDto(5, 'My Title', -1); // Has a negative type, but SHOULD be null or positive.

$errors = Validation::validate($dto);
/*
 * '$errors' will contain the following data:
 * 
 * array (
 *   0 => \Idoit\Dto\Validation\ValidationMessage::__set_state(array(
 *     'path' =>
 *       array (
 *         0 => 'type'
 *       ),
 *     'message' => 'The value must be a null.'
 *   )),
 *   1 => \Idoit\Dto\Validation\ValidationMessage::__set_state(array(
 *     'path' =>
 *       array (
 *         0 => 'type'
 *       ),
 *     'message' => 'The value must be positive number.'
 *   ))
 * )
 */

If you need specific logic for validation you can simply create your own classes to do so. Simply implement the \Idoit\Dto\Validation\ValidatorInterface interface and write your own validate method.

Serialization

We can use Serializer to convert JSON structures to DTOs and the other way around:

$dto = \Idoit\Dto\Serialization\Serializer::fromJson(MyDto::class, [
    'id' => 123, 
    'title' => 'Silvester', 
    'type' => 2,
    'created' => '2025-12-31T00:00:00+00:00'
]);
/*
 * '$dto' will contain the following data:
 * 
 * \MyDto::__set_state(array(
 *    'id' => 123,
 *    'title' => 'Silvester',
 *    'type' => 2,
 *    'created' => 
 *   \DateTime::__set_state(array(
 *      'date' => '2025-12-31 00:00:00.000000',
 *      'timezone_type' => 1,
 *      'timezone' => '+00:00'
 *   ))
 * ))
 */

$array = \Idoit\Dto\Serialization\Serializer::toJSON($dto);
/**
 * '$array' will contain the following data: 
 * 
 * array (
 *   'id' => 123,
 *   'title' => 'Silvester',
 *   'type' => 2,
 *   'created' => '2025-12-31T00:00:00+00:00'
 * )
 */

If you need specific logic for serialization you can simply create your own classes to do so. Simply extend the abstract Idoit\Dto\Serialization\Format class and implement your own toJson and fromJson methods.

Please note that the validation will not be executed, when serializing data.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-30