承接 michaelalexeevweb/openapi-php-dto-generator 相关项目开发

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

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

michaelalexeevweb/openapi-php-dto-generator

最新稳定版本:2.0.29

Composer 安装命令:

composer require michaelalexeevweb/openapi-php-dto-generator

包简介

Generate PHP DTOs from OpenAPI and validate incoming HTTP requests against OpenAPI schema.

README 文档

README

MIT License CI Latest Version PHP Version Total Downloads

Generate PHP DTOs from OpenAPI and validate incoming HTTP requests against OpenAPI schema.

Stop writing boilerplate PHP data transfer objects by hand. This library reads your OpenAPI 3.x YAML specification and automatically generates strictly-typed, immutable PHP 8.4 DTO classes. On top of that, it provides runtime services to deserialize Symfony Request objects into those DTOs, validate HTTP requests against the original OpenAPI schema rules (OpenAPI request validation), and normalize them back to arrays or JSON — all in one package.

Features

  • 🚀 Code generation — generate immutable PHP DTO classes directly from OpenAPI 3.0 / 3.1 YAML specs
  • OpenAPI request validation — validate HTTP requests against OpenAPI constraints (required fields, types, enums, formats, etc.)
  • 🔄 Normalization — convert DTOs to plain arrays or JSON, with or without validation
  • 📦 Symfony Request support — deserialize Symfony Request objects directly into typed PHP DTOs
  • 🔒 Immutable by design — all generated classes are read-only value objects
  • Supports OpenAPI 3.0.x and 3.1.x

Table of Contents

Installation

composer require michaelalexeevweb/openapi-php-dto-generator:^2.0.29

Requirements

  • PHP 8.4+
  • Symfony 7.4 components (console, http-foundation, mime, yaml)

Quick Start

  1. Generate DTOs from your OpenAPI YAML spec
  2. Deserialize and validate an incoming HTTP request into a generated DTO
  3. Validate and normalize the DTO for response
use OpenapiPhpDtoGenerator\Service\DtoDeserializer;
use OpenapiPhpDtoGenerator\Service\DtoNormalizer;
use Symfony\Component\HttpFoundation\Request;
use YourApp\Generated\UserPostRequest; // generated DTO from OpenAPI spec
use YourApp\Generated\UserViewResponse; // generated DTO from OpenAPI spec

$deserializer = new DtoDeserializer();
$normalizer   = new DtoNormalizer();

/** @var Request $request */
// request: deserialize -> validate
$requestDto = $deserializer->deserialize($request, UserPostRequest::class);

// response: validate -> normalize
$responseData = $normalizer->validateAndNormalizeToArray($requestDto);
// response: normalize without validation for faster response
$responseData = $normalizer->toArray(new UserViewResponse(name: 'John', surname: 'Doe'));

Usage

Add script in your project composer.json

{
  "scripts": {
    "openapi:generate-dto": "php vendor/michaelalexeevweb/openapi-php-dto-generator/bin/console openapi:generate-dto"
  }
}

Generate DTO classes from YAML OpenAPI spec

Use one canonical command:

composer openapi:generate-dto -- \
  --file=OpenApiExamples/test.yaml \
  --directory=generated/test \
  --namespace=Generated\\Test \
  --dto-generator-directory=Common \
  --dto-generator-namespace=Generated\\Common

Parameters:

Option Alias Required Description
--file -f Path to OpenAPI YAML spec file
--directory -d Output directory for generated DTOs
--namespace Explicit DTO namespace (derived from --directory if omitted)
--dto-generator-directory Copy runtime services into this directory (Common by default)
--dto-generator-namespace Explicit namespace for copied runtime services

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固