diephp/data-object 问题修复 & 功能扩展

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

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

diephp/data-object

最新稳定版本:v1.0.0

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 \JsonSerializable and ArrayAccess interfaces
  • 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 toArray method
  • 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.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-24

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固