antwerpes/data-transfer-object
Composer 安装命令:
composer require antwerpes/data-transfer-object
包简介
Simple json encoding and decoding for PHP data objects
关键字:
README 文档
README
Simple library for encoding and decoding JSON structures into PHP objects, e.g. to work with API responses in a strongly typed way.
Installation
You can install the package via composer:
composer require antwerpes/data-transfer-object
Usage
Define a class that extends Antwerpes\DataTransferObject\DataTransferObject and define the structure of the object:
use Antwerpes\DataTransferObject\Attributes\Cast; use Antwerpes\DataTransferObject\Attributes\Map; use Antwerpes\DataTransferObject\Casts\ArrayCaster; use Antwerpes\DataTransferObject\DataTransferObject; class User extends DataTransferObject { public function __construct( public string $name, #[Cast(CustomDateCaster::class)] public DateTimeInterface $birthday, #[Map(from: 'address.city')] public string $city, #[Cast(ArrayCaster::class, itemType: Interest:class)] public array $interests, ) {} }
Then you can use the class to decode JSON strings into PHP objects:
$json = '{ "name": "John Doe", "birthday": "1990-01-01", "address": { "city": "New York" }, "interests": [ { "name": "Music" }, { "name": "Programming" } ] }'; $user = User::decode(json_decode($json, true)); $encoded = $user->encode();
Custom Casters
You can define custom casters by implementing the Antwerpes\DataTransferObject\CastsProperty interface:
use Antwerpes\DataTransferObject\CastsProperty; class CustomDateCaster implements CastsProperty { public function unserialize(mixed $value): DateTimeInterface { return new DateTime($value); } public function serialize(mixed $value): string { return $value->format('Y-m-d'); } }
Mapping
You can map nested properties to a flat structure using the Map attribute:
use Antwerpes\DataTransferObject\Attributes\Map; class User extends DataTransferObject { public function __construct( #[Map(from: 'address.city', to: 'address.city')] public string $city, ) {} }
Validation
Validation is out of scope for this package, use JSON schemas or other libraries like symfony/validator
to validate the object.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Contributions are welcome! Leave an issue on GitHub, or create a Pull Request.
Credits
License
The MIT License (MIT). Please see License File for more information.
antwerpes/data-transfer-object 适用场景与选型建议
antwerpes/data-transfer-object 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 157.57k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 09 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「chiiya」 「json-encoding」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 antwerpes/data-transfer-object 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 antwerpes/data-transfer-object 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 antwerpes/data-transfer-object 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP library for creating iOS and Android Wallet Passes
Laravel library for creating iOS and Android Wallet Passes
Admin user, role and permission management for Laravel Filament
Code style and quality configurations for Laravel projects
PHP SDK for the TMDB API
Laravel SDK for the TMDB API
统计信息
- 总下载量: 157.57k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 29
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-12