klkvsk/dto-generator 问题修复 & 功能扩展

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

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

klkvsk/dto-generator

Composer 安装命令:

composer require klkvsk/dto-generator

包简介

Generate DTO classes with zero runtime dependencies

README 文档

README

Latest Version on Packagist GitHub GitHub last commit GitHub Repo stars

Generate DTO classes with zero runtime dependencies

This package allows you to generate clean and independent DTO-classes (also called "value objects") by briefly declared schema in PHP-code.

Installation

You only need this package in development environment, since generated classes do not use anything from this library's code. That is why the preferred way is to include it as require-dev:

$ composer require --dev klkvsk/dto-generator

Requirements

  • PHP 8.1+: to run generator in development
  • PHP 7.4 or 8.x: to use generated code in production

Usage

1. Create schema-file

Schema is a regular PHP-file anywhere in your project. The file should return a Schema object from top level.

Example schema:

<?php
use Klkvsk\DtoGenerator\Schema as dto;
use Klkvsk\DtoGenerator\Schema\Types as t;

return dto\schema(
    namespace: 'MyProject\Data',
    objects: [
        new dto\object(
            name: 'Person',
            fields: [
                dto\field('name', t\string(), required: true),
                dto\field('age', t\int(), required: true),
            ]
        ),
    ]
);

2. Generate files

Code generation is done with dto-gen command:

$ ./vendor/bin/dto-gen [schema-file]

By default, generator searches for files named or ending with dto.schema.php, but you can provide schema files manually as arguments.

Generator will try to guess the right path for output by looking at autoload paths in composer.json. If it states PSR-4 mapping for "MyProject\\": "src/" then the file above will be placed in src/Data/Person.php.

To override this behaviour, you can specify outputDir directly:

dto\schema(namespace: "MyProject\\Data", outputDir: "src/generated/", ...);

Features

Back-compatibility

To generate code targeting some minimal version of PHP, use:

./vendor/bin/dto-gen --target 7.4

This option enables or disables some newer language features in the resulting code.

Enumerations

It is possible to generate not only DTOs, but related Enums too:

dto\schema(
    objects: [
        dto\enum(
            name: 'PostStatus'
            cases: [ 'draft', 'published', 'deleted' ]
        ),
        dto\object(
            name: 'Post',
            fields: [
                dto\field('status', t\enum('PostStatus'), required: true),
                ...
            ]
        )
    ]
)

For PHP >= 8.0 native enums will be generated, for older versions a very similar class-based implementation is used.

Type system

DTOs serve a purpose to keep your data strongly typed. Types for schema are:

  • t\int, t\bool, t\string, t\float - basic scalar types
  • t\enum, t\object - for referencing other DTO objects
  • t\date - using DateTimeImmutable
  • t\external - for referencing any other non-DTO classes
  • t\list_(T) - wraps around type T to declare T[]
  • t\mixed - if you really don't know

(where t is an alias to Klkvsk\DtoGenerator\Schema\Types)

Also, you can extend the abstract Type class for your needs.

Hydration and validation

DTOs can be created with a regular constructor or with <DTO>::create(array $data) method.

Method create accepts an associative array with data. That data is then filtered (if it needs some cleaning up beforehand) and imported (converted to proper types). After that, the method calls a default constructor, passing imported fields to it. The constructor get fields validated not only by type, but by a custom logic.

So, there are three stages of data manipulation, each can be described in schema with callables:

  1. filter prepares data to be imported
  2. importer casts value to correct type or instantiates a nested object
  3. validator checks that imported value meets specified criteria

filter and importer closures should return processed value. If a null is returned, further closures are not called.

validator returns true/false, and on false an InvalidArgumentException is thrown automatically. Also, you can throw your own exception and don't return anything.

Closures of type filter and validator are defined in schema:

dto\field('age', t\int(),
    filters: [ fn ($x) => preg_replace('/[^0-9]+/', $x) ],
    validators: [ fn ($x) => $x > 0 && $x < 100 ]
)

Closure of type importer is predefined for all types except t\object:

dto\field('file', t\external(SplFileInfo::class, fn ($x) => new SplFileInfo($x))

You can specify a custom importer if you extend Type for your own needs.

Examples

See /example/ dir for the example schema and generated classes for different PHP versions.

License

The MIT License (MIT). Please see License File for more information.

klkvsk/dto-generator 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-19