nuxtifyts/php-dto 问题修复 & 功能扩展

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

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

nuxtifyts/php-dto

最新稳定版本:v0.5.0

Composer 安装命令:

composer require nuxtifyts/php-dto

包简介

PHP DataTransferObject

README 文档

README

GitHub License Packagist Version PhpStan Level PHPStan Checks CI Tests Test Coverage

This package enabled the creation of data objects which can be used in various ways. Using modern PHP syntax, it allows you to hydrate data from arrays, objects, and other data sources. As well as carrying out the data, type validation and serialize the data for any purpose.

To create a data class, you will need to declare a readonly class that extends Data class. Then you can define the properties of the class and their types.

use Nuxtifyts\PhpDto\Data;
use Nuxtifyts\PhpDto\Attributes\Property\Aliases;
use Nuxtifyts\PhpDto\Attributes\Property\Computed;

final readonly class UserData extends Data
{
    #[Computed]
    public string $fullName;

    public function __construct(
        public string $firstName,
        #[Aliases('familyName')]
        public string $lastName
    ) {
        $this->fullName = "$this->firstName $this->lastName";
    }
}

You can then create an instance of the class from a mixed value. The DTO will then attempt to hydrate the object with the given data.

$data = [
    'firstName' => 'John',
    'lastName' => 'Doe',
];

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

DTOs can also be serialized to an array:

$user = new UserData('John', 'Doe');

$userData = $user->toArray();

// Or transform to a JSON string

$userData = $user->toJson();

Check out the Quick start guide for more information.

Note

This package was inspired from the spatie/data-transfer-object package. The main thing that I tried to focus on when creating this package is to make it outside of Laravel ecosystem, meaning: no dependency on illuminate/support.

Requirements

  • PHP 8.4 or higher
  • That's it!

Installation

You can install the package via composer:

composer require nuxtifyts/php-dto

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-12-22