jdz/data
Composer 安装命令:
composer require jdz/data
包简介
Data utility class for nested array manipulation
关键字:
README 文档
README
A PHP utility class for manipulating nested arrays using dot notation.
Features
- 🎯 Dot Notation: Access nested array values with simple dot notation (
user.profile.name) - 🔢 Numeric Keys: Work seamlessly with array indices
- 🔄 Bulk Operations: Set multiple values at once with merge support
- 🎨 Typed Getters: Type-safe retrieval with
getBool(),getInt(),getArray() - 🔗 Method Chaining: Fluent interface for elegant code
- 🎭 Null Handling: Optional null value preservation
- ✅ Type Safe: Full PHP 8.0+ type declarations
Installation
composer require jdz/data
Requirements
- PHP 8.0 or higher
Quick Start
use JDZ\Utils\Data; $data = new Data(); // Set values using dot notation $data->set('user.name', 'John Doe'); $data->set('user.email', 'john@example.com'); $data->set('user.settings.theme', 'dark'); // Get values echo $data->get('user.name'); // "John Doe" echo $data->get('user.age', 30); // 30 (default value) // Check existence if ($data->has('user.email')) { echo "Email exists!"; } // Work with arrays $data->set('users.0.name', 'John'); $data->set('users.1.name', 'Jane'); // Get typed values $isActive = $data->getBool('user.active', true); $count = $data->getInt('user.login_count', 0); $tags = $data->getArray('user.tags', []);
Documentation
Setting Values
set(string $path, mixed $value): self
Set a single value using dot notation.
$data->set('app.name', 'MyApp'); $data->set('app.version', '1.0.0'); $data->set('config.database.host', 'localhost');
sets(array $data, bool $merge = true): self
Set multiple values at once.
// Merge with existing data $data->sets([ 'api.endpoint' => 'https://api.example.com', 'api.timeout' => 30, ], true); // Set without merging $data->sets([ 'config.debug' => true, ], false);
Getting Values
get(string $path, mixed $default = null): mixed
Get a value with an optional default.
$name = $data->get('user.name'); $country = $data->get('user.country', 'USA');
getBool(string $path, bool $default = false): bool
Get a boolean value. Converts 1, '1', and true to true.
$isEnabled = $data->getBool('features.api'); $hasAccess = $data->getBool('user.premium', false);
getInt(string $path, int $default = 0): int
Get an integer value with automatic type conversion.
$timeout = $data->getInt('config.timeout'); $retries = $data->getInt('config.retries', 3);
getArray(string $path, array $default = []): array
Get an array value. Non-array values are cast to arrays.
$tags = $data->getArray('post.tags'); $items = $data->getArray('list.items', ['default']);
Checking and Removing
has(string $path): bool
Check if a key exists.
if ($data->has('user.email')) { // Email is set }
erase(string $path): self
Remove a value.
$data->erase('user.temporary_token'); $data->erase('cache.expired');
def(string $path, mixed $default = ''): self
Set a value only if it doesn't already exist.
$data->def('config.timeout', 30); // Sets if not exists $data->def('config.retries', 3); // Sets if not exists
Utility Methods
all(): array
Get all data as an array.
$allData = $data->all();
preserveNulls(bool $preserve = true): self
Enable or disable null value preservation.
$data->preserveNulls(true); $data->set('user.optional', null); // Null is preserved
Working with Arrays
Access array elements using numeric keys:
$data->set('items.0', 'First'); $data->set('items.1', 'Second'); $data->set('items.2', 'Third'); echo $data->get('items.0'); // "First"
Build complex structures:
$data->set('users.0.name', 'John'); $data->set('users.0.email', 'john@example.com'); $data->set('users.1.name', 'Jane'); $data->set('users.1.email', 'jane@example.com'); $users = $data->getArray('users'); // [ // ['name' => 'John', 'email' => 'john@example.com'], // ['name' => 'Jane', 'email' => 'jane@example.com'] // ]
Method Chaining
All mutating methods return $this for fluent chaining:
$config = (new Data()) ->set('app.name', 'MyApp') ->set('app.version', '1.0.0') ->def('app.debug', false) ->def('app.timeout', 30) ->erase('app.temporary');
Examples
See the examples directory for detailed examples:
01-basic-usage.php- Basic operations02-typed-getters.php- Typed getter methods03-array-keys.php- Working with arrays04-bulk-operations.php- Bulk setting operations05-erase-and-def.php- Removing and defaulting values06-null-handling.php- Null value handling07-method-chaining.php- Method chaining examples
Run example:
php examples/01-basic-usage.php
Testing
Run all tests:
composer test # or vendor/bin/phpunit
Run tests with coverage report (HTML):
composer test-coverage
# Coverage report will be generated in the coverage/ directory
License
This project is licensed under the MIT License - see the LICENSE file for details.
jdz/data 适用场景与选型建议
jdz/data 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 128 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 12 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「config」 「utilities」 「core」 「dataset」 「JDZ」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jdz/data 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jdz/data 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jdz/data 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Contao Open Source CMS
A Zend Framework module to quickly and easily set PHP settings.
A collection of enhancements and utilities for the Silverstripe UserForms module
DiZed Team Core module for the Magento 2 project
Server environment detection based on config array-file
Core library that defines common interfaces used by the rest of the intahwebz..
统计信息
- 总下载量: 128
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 9
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-12-20