承接 lemax10/dto-helpers 相关项目开发

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

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

lemax10/dto-helpers

Composer 安装命令:

composer require lemax10/dto-helpers

包简介

Laravel DTO Helpers

README 文档

README

This package offers a set of traits to simplify working with DTOs (Data Transfer Objects) in Laravel. It allows you to:

  • Convert a DTO to an array using the AsArray trait.
  • Transform a DTO into a JSON string via the AsJson trait.
  • Seamlessly serialize a DTO with json_encode() using the AsJsonSerialize trait.
  • Clone a DTO with modified properties through the AsCloneable trait.
  • Make a DTO with static method make(), using AsMake trait
  • Use DTOs as custom casts in Eloquent models.
  • Use Array helpers only() or except() from DTO with using AsArray trait.

Installation

Install the library using Composer:

composer require lemax10/dto-helpers

AsArray

Converts your DTO to an array.
Requires: Implements Illuminate\Contracts\Support\Arrayable.

use LeMaX10\DtoHelpers\Traits\AsArray;
use Illuminate\Contracts\Support\Arrayable;

class MyData implements Arrayable
{
    use AsArray;

    public function __construct(
        public string $key,
        public string $value
    ) {}
}

$dto = new MyData(key: 'test1', value: 'value1');

dump($dto->toArray());
// Output: ['key' => 'test1', 'value' => 'value1']

dump($dto->only('key'));
// Output: ['key' => 'test1']

dump($dto->except('key'));
// Output: ['value' => 'value1']

AsJson

Converts your DTO to a JSON string.
Requires: Implements Illuminate\Contracts\Support\Jsonable.

use LeMaX10\DtoHelpers\Traits\AsJson;
use Illuminate\Contracts\Support\Jsonable;

class MyData implements Jsonable
{
    use AsJson;
    // ...
}

$dto = new MyData(key: 'test1', value: 'value1');

dump($dto->toJson());
// Output: '{"key":"test1","value":"value1"}'

AsJsonSerialize

Enables JSON serialization with json_encode().
Requires: Implements JsonSerializable.

use LeMaX10\DtoHelpers\Contracts\Makeable;
use JsonSerializable;

class MyData implements JsonSerializable
{
    use AsJsonSerialize;

    // ...
}

$dto = MyData::make(key: 'test1', value: 'value1');
dump(json_encode($dto));
// Output: {"key":"test1","value":"value1"}

AsMake

Enables JSON serialization with json_encode().
Requires: Implements LeMaX10\DtoHelpers\Contracts\Makeable.

use LeMaX10\DtoHelpers\Contracts\Makeable;
use JsonSerializable;

class MyData implements Makeable
{
    use AsMake;

    // ...
}

class MyDataCustomMake implements Makeable
{
    // ...
    public static function make(...$arguments): static
    {
        // Example so-so
        if (Arr::get($arguments, 'key') === 1) {
            $arguments['value'] = 'valueExample';
        }
        
        return new static(...$arguments);
    }
}

$dto = MyData::make(key: 'test1', value: 'value1');
dump($dto);
// Output: instance of MyData

$dtoCustom = MyDataCustomMake::make(key: 1, value: 'value1');
dump($dto);
// Output: instance of MyDataCustomMake and value equals "valueExample"

AsCloneable

Clones a DTO while allowing property overrides.
Requires: Implements LeMaX10\DtoHelpers\Contracts\Cloneable

use LeMaX10\DtoHelpers\Traits\AsArray;
use LeMaX10\DtoHelpers\Traits\AsCloneable;
use LeMaX10\DtoHelpers\Contracts\Cloneable;
use Illuminate\Contracts\Support\Arrayable;

class MyData implements Cloneable, Arrayable
{
    use AsCloneable, AsArray;

    // ...
}

$dto = new MyData(key: 'test1', value: 'value1');

dump($dto->toArray());
// Output: ['key' => 'test1', 'value' => 'value1']

$clone = $dto->clone(['key' => 'test2']);

dump($clone->toArray());
// Output: ['key' => 'test2', 'value' => 'value1']

AsMake

Converts your DTO to a JSON string.
Requires: Implements Illuminate\Contracts\Support\Jsonable.

use LeMaX10\DtoHelpers\Traits\AsJson;
use Illuminate\Contracts\Support\Jsonable;

class MyData implements Jsonable
{
    use AsJson;
    // ...
}

$dto = new MyData(key: 'test1', value: 'value1');

dump($dto->toJson());
// Output: '{"key":"test1","value":"value1"}'

Eloquent Model Casts

Use your DTO as a custom cast in an Eloquent model.

use LeMaX10\DtoHelpers\Casts\AsDto;
use Illuminate\Database\Eloquent\Model;

class ExampleModel extends Model
{
    protected $casts = [
        'dto' => AsDto::class . ':' . MyData::class,
    ];
    
    // OR
    
    public function casts()
    {
        return [
           'dto' => AsDto::cast(MyData::class),
        ];
    }
}

$model = ExampleModel::find(1);

dump($model->dto);
// Output: instance of MyData

Contributing

Thank you for considering contributing to the DTO Helpers package!

License

The DTO Helpers package is open-sourced software licensed under the MIT license.

lemax10/dto-helpers 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

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