devuri/dot-access
Composer 安装命令:
composer require devuri/dot-access
包简介
A simple PHP library for accessing nested data using dot notation.
README 文档
README
The DotAccess class provides a user-friendly wrapper around the functionality of the Dflydev\DotAccessData\Data package, allowing easy access to nested data using dot notation in PHP.
Installation
- Ensure you have Composer installed on your system.
- Run the following command to install the package:
composer require devuri/dot-access
Getting Started
- Include the
DotAccessclass in your PHP script:
use Urisoft\DotAccess;
- Create an instance of the
DotAccessclass and pass the nested data (array or object) to the constructor:
$data = [ 'user' => [ 'name' => 'John Doe', 'email' => 'john.doe@example.com', 'address' => [ 'city' => 'New York', 'country' => 'USA', ], ], ]; $dotdata = new DotAccess($data);
Accessing Data
The DotAccess class provides the following methods to access the nested data using dot notation:
Get the Value
Use the get() method to retrieve the value associated with a dot notation key:
$name = $dotdata->get('user.name'); $email = $dotdata->get('user.email'); $city = $dotdata->get('user.address.city');
Set the Value
Use the set() method to set a value for a dot notation key:
$dotdata->set('user.age', 30);
Checking for Key Existence
Use the has() method to check if a dot notation key exists in the data:
$emailExists = $dotdata->has('user.email');
Removing a Key
Use the remove() method to unset the value associated with a dot notation key:
$dotdata->remove('user.address.country');
Example
$data = [ 'user' => [ 'name' => 'John Doe', 'email' => 'john.doe@example.com', 'address' => [ 'city' => 'New York', 'country' => 'USA', ], ], ]; $dotdata = new DotAccess($data); $name = $dotdata->get('user.name'); // Output: "John Doe" $dotdata->set('user.age', 30); $emailExists = $dotdata->has('user.email'); // Output: true $dotdata->remove('user.address.country'); echo "Name: $name\n"; echo "Age: " . $dotdata->get('user.age') . "\n"; echo "Email exists: " . ($emailExists ? 'Yes' : 'No') . "\n";
Wrapper Function - DataKey:get()
In addition to the DotAccess class, we also provide a standalone wrapper function DataKey that simplifies accessing nested data using dot notation.
Usage
The DataKey:get() function allows you to quickly access nested data without having to create an instance of the DotAccess class. It takes three parameters:
- The data array or object to access.
- The dot notation key to access the data.
- An optional default value to return if the key is not found.
Here's how you can use the DataKey:get() function:
$data = [ 'user' => [ 'name' => 'John Doe', 'email' => 'john.doe@example.com', 'address' => [ 'city' => 'New York', 'country' => 'USA', ], ], ]; // Using the wrapper function $name = DataKey:get($data, 'user.name'); $email = DataKey:get($data, 'user.email'); $city = DataKey:get($data, 'user.address.city'); $zipCode = DataKey:get($data, 'user.address.zip_code', 'N/A'); // Provide a default value if the key doesn't exist echo "Name: $name\n"; echo "Email: $email\n"; echo "City: $city\n"; echo "Zip Code: $zipCode\n";
When to Use DataKey:get() vs. DotAccess
Both the DataKey:get() function and the DotAccess class serve the same purpose: accessing nested data using dot notation. The choice between them depends on your specific use case and coding preferences.
Use DataKey:get() when:
- You prefer a simple function call over creating an instance of the
DotAccessclass. - You only need to access nested data at a few specific points in your code.
- You don't need to perform multiple operations (e.g., setting, checking, or removing keys).
Use DotAccess class when:
- You need to perform multiple operations on the same nested data within your code.
- You prefer an object-oriented approach for handling nested data.
- You need better encapsulation and separation of concerns in your code.
Both approaches provide a convenient and user-friendly way to work with nested data using dot notation. Choose the one that best fits your coding style and requirements.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
The DotAccess class is a simple wrapper around the Dflydev\DotAccessData\Data package, which provides the core functionality for accessing nested data using dot notation. Special thanks to the authors of the Dflydev\DotAccessData package for their excellent work.
devuri/dot-access 适用场景与选型建议
devuri/dot-access 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.46k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 07 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dot-notation」 「dot-access」 「nested-data」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 devuri/dot-access 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 devuri/dot-access 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 devuri/dot-access 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Define configuration arrays with strict schemas and access values with dot notation
DotContainer enables deep array access using dot-notation.
A strictly typed array transformer with dot-access, fluent interface and filters.
Library that allows to access array and object with strong type support in Dot Notation
Add a flexible JSON object to any ActiveRecord model with easy dot-notation access
Strongly-typed configuration for PHP applications
统计信息
- 总下载量: 25.46k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-07-30