承接 amorphine/data-transfer-object 相关项目开发

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

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

amorphine/data-transfer-object

Composer 安装命令:

composer require amorphine/data-transfer-object

包简介

DTO library for arrays

README 文档

README

Zero-dependency library to convert associative arrays to an object model

Installation

You can install the package via composer:

composer require amorphine/data-transfer-object

Purpose

The goal of this package is to give a convenient declared way to manipulate data extracted from associative arrays. Advantages of object structure:

  • Type safety (as long as PHP makes it possible)
  • Statically analyze and auto completion.
  • Scalar type casting

Let's look at the example of a JSON API call:

$post = $api->get('posts', 1); 

[
    'title' => '',
    'body' => '',
    'author' => '["id" => 1, ...]',
]

Working with this array is difficult, as we'll always have to refer to the documentation to know what's exactly in it. This package allows you to create data transfer object definitions, classes, which will represent the data in a structured way.

We did our best to keep the syntax and overhead as little as possible:

class PostData extends DataTransferObject
{
    /** @var string */
    public $title;
    
    /** @var string */
    public $body;
    
    /** @var AnotherDataTransferObjectImplementation */
    public $author;
}

An object of PostData can from now on be constructed like so:

$postData = new PostData([
    'title' => '',
    'body' => '',
    'author' => '',
]);

Now you can use this data in a structured way:

$postData->title;
$postData->body;
$postData->author;

By adding doc blocks to our properties, their values will be validated against the given type; and a TypeError will be thrown if the value doesn't comply with the given type.

Here are the possible ways of declaring types:

use SomeNameSpace\Author;

class PostData extends DataTransferObject
{
    /**
     * Built in types: 
     *
     * @var string 
     */
    public $property;

    /**
     * You can override property data source (array key)
     *
     * @source keyWithSomeAnotherName
     */
    public $property;
    
    /**
     * Classes with their FQCN: 
     *
     * @var Author
     */
    public $property;
    
    /**
     * Lists of types: 
     *
     * @var Author[]
     */
    public $property;
    
    /**
     * Iterator of types: 
     *
     * @var iterator<Author>
     */
    public $property;
    
    /**
     * Union types: 
     *
     * @var string|int
     */
    public $property;
    
    /**
     * Nullable types: 
     *
     * @var string|null
     */
    public $property;
    
    /**
     * Mixed types: 
     *
     * @var mixed|null
     */
    public $property;
    
    /**
     * Any iterator : 
     *
     * @var iterator
     */
    public $property;
    
    /**
     * No type, which allows everything
     */
    public $property;
    
    /** 
     * PHP types declaration supported
     */
    public ?int $property;

}

PHP 7.4 typed properties are the source of types like PHP doc is. The compatibility of declared types is upon to the developer. Feel free to use one of them, both or none at all.

Source override

By default, the array key the value is taken from is the property name. If you need to specify the key of the original array the value should be taken from you should use @source doc comment:

class PostData extends DataTransferObject
{
    /** 
     * @var AuthorData
     * @source authorData
     */
    public $author;
}

Automatic casting of nested DTOs

If you've got nested DTO fields, data passed to the parent DTO will automatically be cast.

class PostData extends DataTransferObject
{
    /** @var AuthorData */
    public $author;
}

PostData can now be constructed like so:

$postData = new PostData([
    'author' => [
        'name' => 'Foo',
    ],
]);

Automatic casting of nested array DTOs

Similarly to above, nested array DTOs will automatically be cast.

class TagData extends DataTransferObject
{
    /** @var string */
   public $name;
}

class PostData extends DataTransferObject
{
    /** @var TagData[] */
   public $tags;
}

PostData will automatically construct tags like such:

$postData = new PostData([
    'tags' => [
        ['name' => 'foo'],
        ['name' => 'bar']
    ]
]);

Exception handling

Beside property type validation, you can also be certain that the data transfer object in its whole is always valid. On constructing a data transfer object, we'll validate whether all required (non-nullable) properties are set. If not, a Amorphine\DataTransferObject\Exceptions\DataTransferObjectError will be thrown.

Likewise, if you're trying to set non-defined properties, you'll get a DataTransferObjectError.

Testing

composer test

Limitations

  • Opcache: types and data sources are resolved through PHP comments so ensure opcache.save_comments equals 1

License

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

Powered by

See also

amorphine/data-transfer-object 适用场景与选型建议

amorphine/data-transfer-object 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.48k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2020 年 07 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 amorphine/data-transfer-object 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-07-01