承接 mohammedmanssour/super-simple-dto 相关项目开发

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

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

mohammedmanssour/super-simple-dto

Composer 安装命令:

composer require mohammedmanssour/super-simple-dto

包简介

Creating data transfer objects with the power of php objects

README 文档

README

Latest Version on Packagist Tests Total Downloads

Creating data transfer objects with the power of php objects. Simple, lightweight, and efficient DTO conversion with automatic type handling.

This is a laravel package and laravel/framework is part of the package dependencies. Please, make sure you have no problem with that before using.

Why Bother.

The spatie team has already created an awesome package that serves as a great solution for DTO objects. But, for me, it's full of features that I don't use and it seems like an overkill for me when I just wanted simple solution for DTO work.

Installation

You can install the package via composer:

composer require mohammedmanssour/super-simple-dto

Usage

  1. Apply the AsDTO trait to your data object
use MohammedManssour\DTO\Concerns\AsDTO;

class UserData
{
    use AsDTO;

    public string $name;
    public string $email;
    public BalanceData $balance;
    public Status $status;
}
  1. Use one of these static methods to convert data into DTO:
    • fromCollection: converts collections to DTO objects.
    • fromArray: converts array to DTO objects.
    • fromModel: converts model to DTO objects. It works with the data available with $model->getAttributes() method.
    • fromRequest: converts laravel requests to DTO objects. It works with the data available with validated(). In case validated method is not available, it'll use the all() method. You can also force using request's all method by passing true as a second parameter.
UserData::fromCollection(collect([]));
UserData::fromArray([]);
UserData::fromModel($model);
UserData::fromRequest($request);

Features

Automatic Type Conversion

The package automatically handles type conversion for:

  • Enums: Automatically converts values to enum instances
  • DTOs: Automatically converts arrays/objects to other DTO instances
  • Built-in types: Handles all PHP built-in types

Array to DTO Collection Mapping with MapInto

The MapInto attribute allows you to automatically convert arrays of data into arrays of DTO objects. This is particularly useful when working with collections of related data.

use MohammedManssour\DTO\Concerns\AsDTO;
use MohammedManssour\DTO\Support\MapInto;

class WalletData
{
    use AsDTO;
    
    public string $type;
    public int $balance;
}

class UserData
{
    use AsDTO;
    
    public string $name;
    public string $email;
    
    #[MapInto(WalletData::class)]
    public array $wallets;
}

$data = [
    'name' => 'Mohammed Manssour',
    'email' => 'hello@mohammedmanssour.me',
    'wallets' => [
        ['type' => 'bitcoin', 'balance' => 1000],
        ['type' => 'ethereum', 'balance' => 500],
    ]
];

$user = UserData::fromArray($data);

// $user->wallets will contain an array of WalletData objects
foreach ($user->wallets as $wallet) {
    echo $wallet->type . ': ' . $wallet->balance; // Each wallet is a WalletData instance
}

Custom Property Setters

You can define custom setter methods for properties that need special handling:

class UserData
{
    use AsDTO;
    
    public Carbon $created_at;
    
    public function setCreatedAt($value)
    {
        $this->created_at = Carbon::parse($value);
    }
}

Type Safety

The package respects PHP type declarations and automatically converts:

class BalanceData
{
    use AsDTO;
    
    public float $bitcoin;
    public int $usdollar;
}

enum Status: string
{
    case Active = 'active';
    case Suspended = 'suspended';
}

class UserData
{
    use AsDTO;
    
    public BalanceData $balance;  // Automatically converted from array
    public Status $status;        // Automatically converted from string
}

$data = [
    'balance' => [
        'bitcoin' => 10.5,
        'usdollar' => 1000
    ],
    'status' => 'active'
];

$dto = UserData::fromArray($data);

// $dto->balance is a BalanceData instance
// $dto->status is a Status enum instance

Property Initialization Check

The package provides a helpful isset() method that checks if a property was actually initialized (different from PHP's native isset() which returns false for null values):

$dto = UserData::fromArray([
    'name' => 'Mohammed',
    'email' => null  // explicitly set to null
]);

// Native PHP isset
isset($dto->name);     // true
isset($dto->email);    // false (because it's null)
isset($dto->balance);  // false (not initialized)

// DTO isset method
$dto->isset('name');     // true
$dto->isset('email');    // true (was explicitly set, even though null)
$dto->isset('balance');  // false (not initialized)

Converting DTO to Array

You can convert any DTO back to an array:

$dto = UserData::fromArray($data);
$array = $dto->toArray();

The toArray() method handles:

  • Nested DTOs (converts them to arrays recursively)
  • Enums (converts to their scalar values)
  • Carbon instances (keeps as Carbon objects)
  • Arrays of DTOs (converts each DTO to array)

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

mohammedmanssour/super-simple-dto 适用场景与选型建议

mohammedmanssour/super-simple-dto 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.2k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2023 年 05 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-05-28