lucifer07/php-dto 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

lucifer07/php-dto

Composer 安装命令:

composer require lucifer07/php-dto

包简介

Swift and efficient PHP Data Transfer Object package with constructor-promoted properties, auto-mapping, and built-in serialization

README 文档

README

Lightweight and efficient PHP Data Transfer Object package with constructor-promoted properties, auto-mapping, and built-in serialization.

Features

  • Type-Safe: Uses constructor-promoted properties with full type hints
  • Auto-Mapping: Automatic mapping between snake_case and camelCase
  • Nested DTOs: Full support for nested DTO objects
  • Custom Mapping: Use MapFrom attribute for custom field mappings
  • Type Casting: Automatic type casting for int, float, bool, DateTime, array
  • Zero Dependencies: No external dependencies
  • PHP 8.1+: Modern PHP features
  • Fast: ~25% faster than traditional reflection-based DTOs

Installation

composer require lucifer07/php-dto

Requirements

  • PHP 8.1 or higher

Quick Start

Basic Usage

Define your DTO class by extending the base DTO class:

use SwiftDTO\DTO;

class UserDTO extends DTO
{
    public function __construct(
        public string $fullName,
        public int $age,
        public bool $isActive,
        public \DateTimeImmutable $createdAt
    ) {}
}

Create DTO from array (auto-mapping from snake_case):

$user = UserDTO::fromArray([
    'full_name' => 'John Doe',
    'age' => 30,
    'is_active' => 'true',
    'created_at' => '2023-10-01 10:00:00'
]);

echo $user->fullName; // 'John Doe'
echo $user->age; // 30
echo $user->isActive; // true

Convert to Array

$array = $user->toArray();
// Returns: ['full_name' => 'John Doe', 'age' => 30, 'is_active' => true, 'created_at' => '2023-10-01T10:00:00+07:00']

Convert to JSON

$json = json_encode($user, JSON_PRETTY_PRINT);
// Returns: JSON object with snake_case keys

Features in Detail

Auto-Mapping

The DTO automatically maps between snake_case (array keys) and camelCase (property names):

// Both work the same
$user1 = UserDTO::fromArray(['full_name' => 'John']);
$user2 = UserDTO::fromArray(['fullName' => 'John']);

Custom Mapping with MapFrom

Use the MapFrom attribute for custom field mappings:

use SwiftDTO\DTO;
use SwiftDTO\MapFrom;

class UserDTO extends DTO
{
    public function __construct(
        #[MapFrom('user_id')]
        public readonly int $id,

        #[MapFrom('user_name')]
        public readonly string $username,

        public string $email
    ) {}
}

$user = UserDTO::fromArray([
    'user_id' => 123,
    'user_name' => 'johndoe',
    'email' => 'john@example.com'
]);

echo $user->id; // 123
echo $user->username; // 'johndoe'

Type Casting

The DTO automatically casts values to the correct type:

$user = UserDTO::fromArray([
    'full_name' => 'John Doe',
    'age' => '30',          // string to int
    'is_active' => 'yes',    // string to bool
    'created_at' => '2023-10-01 10:00:00'  // string to DateTimeImmutable
]);

assert(is_int($user->age)); // true
assert(is_bool($user->isActive)); // true
assert($user->createdAt instanceof \DateTimeImmutable); // true

Nested DTOs

Full support for nested DTOs:

class AddressDTO extends DTO
{
    public function __construct(
        public string $street,
        public string $city,
        public string $country
    ) {}
}

class UserDTO extends DTO
{
    public function __construct(
        public string $name,
        public string $email,
        public AddressDTO $address
    ) {}
}

$user = UserDTO::fromArray([
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'address' => [
        'street' => '123 Main St',
        'city' => 'New York',
        'country' => 'USA'
    ]
]);

echo $user->address->city; // 'New York'

Optional Fields and Default Values

Define optional fields with default values:

class ProductDTO extends DTO
{
    public function __construct(
        public string $name,
        public float $price,
        public bool $inStock,
        public string $category = 'general',
        public int $stockLevel = 0
    ) {}
}

$product = ProductDTO::fromArray([
    'name' => 'Widget',
    'price' => 19.99,
    'in_stock' => true
]);

echo $product->category; // 'general' (default)
echo $product->stockLevel; // 0 (default)

Nullable Fields

Use nullable types for optional fields:

class UserDTO extends DTO
{
    public function __construct(
        public string $name,
        public string $email,
        public ?string $address = null
    ) {}
}

$user = UserDTO::fromArray([
    'name' => 'John Doe',
    'email' => 'john@example.com'
]);

echo $user->address; // null

Read-Only Properties

Use readonly for immutable properties:

class UserDTO extends DTO
{
    public function __construct(
        public readonly int $id,
        public string $name,
        public string $email
    ) {}
}

$user = UserDTO::fromArray([
    'id' => 123,
    'name' => 'John Doe',
    'email' => 'john@example.com'
]);

// $user->id = 456; // Error: Cannot modify readonly property

Examples

Complete examples are available in the examples/ directory:

  • basic_usage.php - Basic DTO creation and serialization
  • nested_dtos.php - Working with nested data structures
  • validation.php - Type safety and default values
  • map_from.php - Custom field mapping
  • collections.php - Working with arrays and collections
  • serialization.php - Serialization methods

Run any example:

php examples/basic_usage.php
php examples/nested_dtos.php
php examples/index.php  # Show all examples

API Reference

Methods

static fromArray(array $data): static

Create DTO instance from array.

$user = UserDTO::fromArray(['name' => 'John', 'age' => 30]);

toArray(): array

Convert DTO to array with snake_case keys.

$array = $user->toArray();

jsonSerialize(): array

Implements JsonSerializable interface for JSON encoding.

$json = json_encode($user);

Type Casting Rules

Target Type Source Value Result
int '30' 30
float '19.99' 19.99
bool 'true', 'yes', 'on', '1' true
bool 'false', 'no', 'off', '0', '' false
DateTimeImmutable '2023-10-01 10:00:00' DateTime object
array '["a", "b"]' (JSON string) ['a', 'b']

Validation

Required fields without default values must be provided:

class UserDTO extends DTO
{
    public function __construct(
        public string $name,  // Required
        public string $email  // Required
    ) {}
}

// This will throw InvalidArgumentException
$user = UserDTO::fromArray(['name' => 'John']);
// Missing required parameter: email

Performance

Benchmark results (10,000 iterations, median):

Operation NEW DTO OLD DTO Speedup
snake_case creation 19.07 μs 23.90 μs 1.25x
camelCase creation 18.44 μs 24.00 μs 1.30x
serialization 6.88 μs 6.33 μs 0.92x
full cycle 25.33 μs 30.13 μs 1.19x

Testing

Run the test suite:

composer test

Or directly with PHPUnit:

./vendor/bin/phpunit

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

lucifer07/php-dto 适用场景与选型建议

lucifer07/php-dto 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「json」 「array」 「mapping」 「dto」 「binding」 「type-safe」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lucifer07/php-dto 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lucifer07/php-dto 我们能提供哪些服务?
定制开发 / 二次开发

基于 lucifer07/php-dto 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-04