damianulan/php-dtos 问题修复 & 功能扩展

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

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

damianulan/php-dtos

最新稳定版本:1.0.0

Composer 安装命令:

composer require damianulan/php-dtos

包简介

Provides a template for creating robust and efficient data-transfer-objects (DTOs) in your PHP projects.

README 文档

README

Licence   Static Badge

Description

Provides a template for creating robust and efficient data-transfer-objects (DTOs) in your PHP projects. Initialization steps look very similar to the ones in Laravel's Eloquent models, regarding objects are based on attributes, which are accessible as propertiesm and DTO objects are bootable.

Installation

You can install the package via composer in your laravel project:

composer require damianulan/php-dtos

Usage

Standard DTO object looks like this:

use DTOs\Dto;

class UserDto extends Dto
{
    // here you can define default values to your attributes
    protected $attributes = [
        'name' => 'Damian',
        'email' => 'damian.ulan@protonmail.com',

    ];

    // here you can define attributes that are assignable outside of constructor
    // then assigning anything outside of this list will throw an exception
    protected $fillable = ['name', 'email'];


    /**
     * Here contain all the logic you need.
    **/
}

Creating and basics on DTO instance:

$dto = new UserDto();

// or with attributes assigned.
// these will be assigned to the object before its initialization
$dto = new UserDto([
    'name' => 'Damian',
    'email' => 'damian.ulan@protonmail.com',
]);

// you can retrieve attributes changes as:
$dto->name = 'Alexander';
$dto->getDirty('name'); // returns 'Alexander'
$dto->getOriginal('name'); // returns 'Damian'

// those will return all attributes as an assoc array
$dto->all();
$dto->toArray();

// check if attributes are empty/filled
$dto->isEmpty();
$dto->isFilled();

// or check if a specific attribute is assigned
$dto->hasAttribute('name');
$dto->hasAttribute('email');

Additional Options

In your DTO class you can implement additional options, that will add custom behavior to your object.

use DTOs\Workshop\ReadOnlyAttributes;

/**
 * This interface will prevent overriding attributes over those previously assigned.
 */
class UserDto extends Dto implements ReadOnlyAttributes
{
    //
}

Available options:

  • ReadOnlyAttributes - prevents reassignment of attributes after object construction.
  • ForbidOverrides - prevents overriding attributes over those already assigned.
  • PreventAccessingMissingAttributes - throws exception on accessing uninitialized attributes.

Contact & Contributing

Any question You can submit to damian.ulan@protonmail.com.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-16