diephp/data-object
Composer 安装命令:
composer require diephp/data-object
包简介
Simple object to be used as a DTO/MODEL/ValueObject.
关键字:
README 文档
README
Simple object to be used as a DTO/MODEL/ValueObject.
Overview
The DataObject class is a versatile data container designed for use as a Data Transfer Object (DTO), Model, or ValueObject. It can be initialized with an associative array and provides various methods for manipulating and accessing data. This class implements both the \JsonSerializable and ArrayAccess interfaces.
Features
- Initialize with an associative array
- Implements
\JsonSerializableandArrayAccessinterfaces - Supports dot notation for nested data access
- Provides methods for merging, filtering, mapping, and transforming data
- Automatically converts objects to arrays if they implement a
toArraymethod - Various utility methods like
hash,flatten,collapse,clone, etc.
Installation
Install the package via Composer:
composer require diephp/dataobject
Or manually add it to your composer.json:
"require": { "diephp/dataobject": "^1.0.0" }
Usage
Initialization
Create a new instance of DataObject:
use DiePHP\DataObject; $data = new DataObject([ 'name' => 'John Doe', 'email' => 'john.doe@example.com' ]);
Or use the static of method:
$data = DataObject::of([ 'name' => 'John Doe', 'email' => 'john.doe@example.com' ]);
Accessing Data
Access data using property notation or array access:
echo $data->name; // John Doe echo $data['email']; // john.doe@example.com
Check if a key exists:
if ($data->has('name')) { // Key exists }
Manipulating Data
Set a value:
$data->set('name', 'Jane Doe');
Get a value with a default:
$email = $data->get('email', 'default@example.com');
Merge new data:
$data->merge([ 'address' => '123 Main St', 'phone' => '123-456-7890' ]);
Filter data:
$data->filter(function ($value, $key) { return !empty($value); });
Map data:
$mappedData = $data->map('*', function ($value) { return strtoupper($value); });
Utility Methods
Convert to array:
$array = $data->toArray();
Convert to JSON:
$json = json_encode($data);
Get the MD5 hash of the data:
$hash = $data->hash();
Clone the object:
$clone = $data->clone();
Example
use DiePHP\DataObject; $data = new DataObject([ 'user' => [ 'name' => 'John Doe', 'email' => 'john.doe@example.com' ] ]); echo $data->get('user.name'); // John Doe $data->set('user.address', '123 Main St'); echo $data->get('user.address'); // 123 Main St $data->merge(['user.phone' => '123-456-7890']); echo $data->get('user.phone'); // 123-456-7890 $data->filter(function ($value, $key) { return !empty($value); }); echo $data->hash(); // MD5 hash of the data echo json_encode($data); // JSON representation
License
This project is licensed under the MIT License. See the LICENSE file for details.
diephp/data-object 适用场景与选型建议
diephp/data-object 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dto」 「dataobject」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 diephp/data-object 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 diephp/data-object 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 diephp/data-object 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Pimcore 6.x Relation-Fields for DataObjects and Documents
PHP library for the MUMSYS project
DataExtension that easily allows a DataObject to be viewed like a Page
A simple library that allows transform any kind of data to native php data or whatever
Silverstripe Translatable extension for DataObjects
Allows DataObjects to self-populate intelligently using fake data
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 25
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-24