zendrop/data
Composer 安装命令:
composer require zendrop/data
包简介
This package allows you to created DataTransferObjects without pain
README 文档
README
Installation
To install the package via Composer, run the following command:
composer require zendrop/data
How To Use
Creating Data Models
- Implement Interfaces: Your models should implement
DataInterfaceandToArrayInterface. - Use Traits: Incorporate
DataTraitandToArrayTraitfor functionality. - Handle Arrays: For validating arrays, use the
ArrayOfattribute. Mark fields that can be skipped with Skippable. - Nested Structures: You can create models within models.
- Enums: Use BackedEnum for enum types.
Code Example
<?php use Zendrop\Data\DataInterface; use Zendrop\Data\DataTrait; use Zendrop\Data\Attributes\ArrayOf; use Zendrop\Data\Skippable; use Zendrop\Data\ToArrayInterface; use Zendrop\Data\ToArrayTrait; class Tag implements DataInterface, ToArrayInterface { use DataTrait; use ToArrayTrait; public function __construct( public readonly string $name ) { } } enum Occupation: string { case Manager: 'manager'; case Developer: 'developer'; } class User implements DataInterface, ToArrayInterface { use DataTrait; use ToArrayTrait; public function __construct( public readonly int $id, public readonly string $userName, /** @var Tag[] */ #[ArrayOf(Tag::class)] public readonly array $tags, public readonly Occupation $occupation, public readonly string|Skippable $bio = Skippable::Skipped ) { } }
Instantiation and Serialization
Create objects from arrays with automatic type conversion and key normalization. Serialize objects back to arrays with flexible key formatting.
// Create a User object from an array $user = User::from([ 'id' => '42', // will be converted to int 'user_name' => 'John Doe', 'tags' => [ ['name' => 'friend'], // will be converted to Tag class ['name' => 'zendrop'] ], 'occupation' => 'developer' // will be converted to Occupation enum // 'bio' is optional and skipped here ]); // Serialize the User object to an array $arraySnakeCase = $user->toArray(); // Default snake_case $arrayCamelCase = $user->toArray(ToArrayCase::Camel); $arrayKebabCase = $user->toArray(ToArrayCase::Kebab);
zendrop/data 适用场景与选型建议
zendrop/data 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 98.31k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2024 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 zendrop/data 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zendrop/data 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 98.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 30
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-02-19