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

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

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

php-collective/dto

最新稳定版本:0.1.5

Composer 安装命令:

composer require php-collective/dto

包简介

Framework-agnostic Data Transfer Object library with code generation

README 文档

README

CI codecov PHPStan Latest Stable Version PHP License

Framework-agnostic DTO library with code generation for PHP.

Unlike runtime reflection libraries, this library generates optimized DTO classes at build time, giving you:

  • Zero runtime reflection overhead
  • Perfect IDE autocomplete with real methods
  • Excellent static analysis support (PHPStan/Psalm work out of the box)
  • Reviewable generated code in pull requests

See Motivation for why code generation beats runtime reflection.

Installation

composer require php-collective/dto

Quick Start

Define DTOs in PHP (or XML, YAML, NEON):

// config/dto.php use PhpCollective\Dto\Config\Dto; use PhpCollective\Dto\Config\Field; use PhpCollective\Dto\Config\Schema; return Schema::create() ->dto(Dto::create('Car')->fields( Field::string('color'), Field::dto('owner', 'Owner'), )) ->dto(Dto::create('Owner')->fields( Field::string('name'), )) ->toArray();

Generate and use:

vendor/bin/dto generate
$car = CarDto::createFromArray(['color' => 'red']); $car->setOwner(OwnerDto::create(['name' => 'John'])); $array = $car->toArray();

See Quick Start Guide for detailed examples.

Immutable DTOs

A more realistic example using immutable DTOs for a blog system:

// config/dto.php return Schema::create() ->dto(Dto::create('Article')->immutable()->fields( Field::int('id')->required(), Field::string('title')->required(), Field::string('slug')->required(), Field::string('content'), Field::dto('author', 'Author')->required(), Field::collection('tags', 'Tag')->singular('tag'), Field::bool('published')->defaultValue(false), Field::string('publishedAt'), )) ->dto(Dto::create('Author')->immutable()->fields( Field::string('name')->required(), Field::string('email'), Field::string('avatarUrl'), )) ->dto(Dto::create('Tag')->immutable()->fields( Field::string('name')->required(), Field::string('slug')->required(), )) ->toArray();
// Creating from API/database response $article = ArticleDto::createFromArray($apiResponse);

Reading in a template (e.g., Twig, Blade, or plain PHP):

<!-- templates/article/view.php --> <article> <h1><?= htmlspecialchars($article->getTitle()) ?></h1> <p class="meta"> By <?= htmlspecialchars($article->getAuthor()->getName()) ?> <?php if ($article->getPublishedAt()) { ?> on <?= $article->getPublishedAt() ?> <?php } ?> </p> <div class="tags"> <?php foreach ($article->getTags() as $tag) { ?> <a href="/tag/<?= $tag->getSlug() ?>"><?= htmlspecialchars($tag->getName()) ?></a> <?php } ?> </div> <div class="content"> <?= $article->getContent() ?> </div> </article>

Features

  • Types: int, float, string, bool, array, mixed, DTOs, classes, enums
  • Union types: int|string, int|float|string (PHP 8.0+)
  • Collections: 'type' => 'Item[]', 'collection' => true with add/get/has methods
  • Associative collections: keyed access with 'associative' => true
  • Immutable DTOs: 'immutable' => true with with*() methods
  • Default values: 'defaultValue' => 0
  • Required fields: 'required' => true
  • Deprecations: 'deprecated' => 'Use newField instead'
  • Inflection: automatic snake_case/camelCase/dash-case conversion
  • Deep cloning: $dto->clone()
  • Nested reading: $dto->read(['path', 'to', 'field'])
  • PHPDoc generics: @var \ArrayObject<int, ItemDto> for static analysis
  • TypeScript generation: Generate TypeScript interfaces from your DTO configs
  • Schema Importer: Auto-create DTOs from JSON data or JSON Schema

Configuration Formats

  • PHP - native arrays or fluent builder
  • XML - XSD validation, IDE autocomplete
  • YAML - requires pecl install yaml
  • NEON - requires nette/neon

TypeScript Generation

Generate TypeScript interfaces directly from your DTO configuration - keeping frontend and backend types in sync:

# Single file output vendor/bin/dto typescript --config-path=config/ --output=frontend/src/types/ # Multi-file with separate imports vendor/bin/dto typescript --multi-file --file-case=dashed --output=types/
// Generated: types/dto.ts export interface UserDto { id: number; name: string; email: string; address?: AddressDto; roles?: RoleDto[]; }

Options: --readonly, --strict-nulls, --file-case=pascal|dashed|snake

See TypeScript Generation for full documentation.

Schema Importer

Bootstrap DTO configurations from existing JSON data or JSON Schema definitions:

use PhpCollective\Dto\Importer\Importer; $importer = new Importer(); // From API response example $json = '{"name": "John", "age": 30, "email": "john@example.com"}'; $config = $importer->import($json); // From JSON Schema $schema = file_get_contents('openapi-schema.json'); $config = $importer->import($schema, ['format' => 'xml']);

Outputs to PHP, XML, YAML, or NEON format. Perfect for integrating with external APIs.

See Schema Importer for full documentation.

Documentation

Integrations

This is the standalone core library. For framework-specific integrations:

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固