承接 lodipay/php-dto 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

lodipay/php-dto

最新稳定版本:v1.2.0

Composer 安装命令:

composer require lodipay/php-dto

包简介

README 文档

README

PHP Data Transfer Object Library

Getting starts

You should extend your class by TseDTO.

Installation

$ composer require tsetsee/php-dto

Features

  1. toArray() method only based on the initialized array data.
<?php

namespace Tsetsee\DTO\Tests\DTO;

use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Spatie\DataTransferObject\Attributes\CastWith;
use Spatie\DataTransferObject\Attributes\MapTo;
use Tsetsee\DTO\Casters\CarbonCaster;
use Tsetsee\DTO\DTO\TseDTO;

class TestDTO extends TseDTO
{
    public string $firstName;
    public string $lastName;
    public ?string $familyName = null;
    public ?int age = null;
}
...

$dto = new TestDTO([
  'firstName' => 'Tsetsentsengel',
  'lastName' => 'Munkhbayar',
  // 'familyName' => 'Galzuud',
  'age' => 31,
]);

var_dump($dto->toArray());
...

[Output]
array() {
  ["firstName"] =>
  string(14) "Tsetsentsengel"
  ["lastName"] =>
  string(10) "Munkhbayar"
  ["age"] =>
  int 31
}

Handling DateTime

  1. php-dto supports symfony's DateTimeNormalizer which handles \DateTimeInterface,\DateTimeImmutable, \DateTime. @see. By default, it uses the RFC3339 format.

  2. php-dto supports Carbon. By default, it uses the RFC3339 format.

<?php

namespace Tsetsee\DTO\Tests\DTO;

use Symfony\Component\Serializer\Annotation\Context;
use Tsetsee\DTO\Serializer\Normalizer\CarbonNormalizer;
use Tsetsee\DTO\DTO\TseDTO;
use Carbon\CarbonImmutable;

class TestDTO extends TseDTO
{
    #[Context(
        normalizationContext: [
            CarbonNormalizer::FORMAT_KEY => 'c',
        ],
        denormalizationContext: [
            CarbonNormalizer::FORMAT_KEY => 'm-d-Y H:i:s',
        ],
    )]
    public ?CarbonImmutable $date = null;
}
  1. Cast from timestamp.
Format Description
x timestamp by milliseconds
X timestamp by seconds
<?php

namespace Tsetsee\DTO\Tests\DTO;

use Carbon\Carbon;
use Tsetsee\DTO\DTO\TseDTO;
use Symfony\Component\Serializer\Annotation\Context;
use Tsetsee\DTO\Serializer\Normalizer\CarbonNormalizer;

class TestDTO extends TseDTO
{
    #[Context(
        denormalizationContext: [
            CarbonNormalizer::FORMAT_KEY => 'X',
        ],
    )]
    public ?Carbon $dateFromTimestamp = null;
}

Casting Array

You should add adder method if the property is an array of DTO.

<?php

namespace Tsetsee\DTO\Tests\DTO;

use Carbon\Carbon;
use Tsetsee\DTO\DTO\TseDTO;
use Symfony\Component\Serializer\Annotation\Context;
use Tsetsee\DTO\Serializer\Normalizer\CarbonNormalizer;

class TestDTO extends TseDTO
{
    /**
     * @var array<ChildDTO>
     */
    public array $children = [];

    public function addChildren(ChildDTO $children): void
    {
        $this->children[] = $children;
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-18