abather/custom-data-helpers 问题修复 & 功能扩展

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

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

abather/custom-data-helpers

Composer 安装命令:

composer require abather/custom-data-helpers

包简介

extend data helpers to include custom seperators.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Description

This Laravel package extends the default data helper functions (data_get, data_set, data_forget, data_has) to support custom separators instead of the hardcoded dot (.) notation.

Laravel's built-in data helpers are fantastic, but they use a fixed dot separator which can cause issues when your data keys contain actual dots. This package solves that problem by allowing you to specify any separator you want.

Installation

You can install the package via composer:

composer require abather/custom-data-helpers

That's it! The package will auto-register itself in Laravel and the helper functions will be available globally.

Usage

The package provides three ways to use custom data helpers:

1. Using the DataHelper Class (Recommended)

use Abather\CustomDataHelpers\DataHelper;

$data = ['users' => ['profile' => ['name' => 'John']]];

// Get value with custom separator
DataHelper::get($data, 'users/profile/name', '/'); // 'John'

// Get with default value
DataHelper::get($data, 'users/profile/age', '/', 25); // 25 (if key doesn't exist)

// Set value
DataHelper::set($data, 'users/profile/email', 'john@example.com', true, '/');

// Check existence
DataHelper::has($data, 'users/profile/name', '/'); // true

// Remove key
DataHelper::forget($data, 'users/profile/email', '/');

2. Using Global Helper Functions

// All DataHelper methods are also available as global helper functions with _ prefix
_data_get($data, 'users/profile/name', '/'); // 'John'
_data_set($data, 'users/profile/email', 'john@example.com', true, '/');
_data_has($data, 'users/profile/name', '/'); // true
_data_forget($data, 'users/profile/email', '/');

DataHelper::get() - Retrieve Values

Retrieve a value from a nested array or object using a custom separator.

use Abather\CustomDataHelpers\DataHelper;

// Basic usage with default dot separator (backward compatible)
$data = ['products' => ['desk' => ['price' => 100]]];
DataHelper::get($data, 'products.desk.price'); // 100

// Using custom separator
$data = ['products' => ['desk' => ['price' => 100]]];
DataHelper::get($data, 'products/desk/price', '/'); // 100

// With default value
DataHelper::get($data, 'products/desk/discount', '/', 0); // 0

// Using wildcards to get multiple values
$data = [
    'products' => [
        ['name' => 'Desk', 'price' => 100],
        ['name' => 'Chair', 'price' => 50]
    ]
];
DataHelper::get($data, 'products/*/price', '/'); // [100, 50]

// Using placeholders
$flight = [
    'segments' => [
        ['from' => 'LHR', 'to' => 'IST'],
        ['from' => 'IST', 'to' => 'PKX'],
    ]
];
DataHelper::get($flight, 'segments->{first}->from', '->'); // 'LHR'
DataHelper::get($flight, 'segments->{last}->to', '->'); // 'PKX'

// Using different separators for different data structures
$config = ['app' => ['database' => ['host' => 'localhost']]];
DataHelper::get($config, 'app|database|host', '|'); // 'localhost'
DataHelper::get($config, 'app->database->host', '->'); // 'localhost'
DataHelper::get($config, 'app/database/host', '/'); // 'localhost'

DataHelper::set() - Set Values

Set a value within a nested array or object using a custom separator.

use Abather\CustomDataHelpers\DataHelper;

$data = [];

// Basic usage
DataHelper::set($data, 'products|desk|price', 200, true, '|');
// Result: ['products' => ['desk' => ['price' => 200]]]

// Using wildcards for bulk updates
$products = [
    ['name' => 'Desk', 'price' => 100],
    ['name' => 'Chair', 'price' => 50]
];
DataHelper::set($products, '*/price', 0, true, '/');
// Sets all product prices to 0

// Conditional setting (don't overwrite existing values)
$data = ['products' => ['desk' => ['price' => 100]]];
DataHelper::set($data, 'products.desk.price', 200, false);
// Price remains 100

// Overwrite existing values (default behavior)
DataHelper::set($data, 'products.desk.price', 200, true);
// Price is now 200

// Create nested structures automatically
$data = [];
DataHelper::set($data, 'api/v1/endpoints/users', '/api/v1/users', true, '/');
// Result: ['api' => ['v1' => ['endpoints' => ['users' => '/api/v1/users']]]]

// Using object notation separator
$settings = [];
DataHelper::set($settings, 'app->theme->colors->primary', '#007bff', true, '->');
// Result: ['app' => ['theme' => ['colors' => ['primary' => '#007bff']]]]

DataHelper::has() - Check if Key Exists

Check if a key exists in an array or object using a custom separator.

use Abather\CustomDataHelpers\DataHelper;

$data = ['products' => ['desk' => ['price' => 100]]];

// Basic usage
DataHelper::has($data, 'products.desk.price'); // true
DataHelper::has($data, 'products.desk.discount'); // false

// Using custom separator
DataHelper::has($data, 'products->desk->price', '->'); // true
DataHelper::has($data, 'products/desk/price', '/'); // true
DataHelper::has($data, 'products|desk|stock', '|'); // false

// Works with nested objects
$object = (object) [
    'user' => (object) [
        'profile' => (object) ['name' => 'John']
    ]
];
DataHelper::has($object, 'user/profile/name', '/'); // true
DataHelper::has($object, 'user/profile/email', '/'); // false

// Using with different separators
$routes = [
    'api' => [
        'v1' => ['users' => 'UsersController']
    ]
];
DataHelper::has($routes, 'api/v1/users', '/'); // true
DataHelper::has($routes, 'api->v1->users', '->'); // true
DataHelper::has($routes, 'api.v1.posts', '.'); // false

DataHelper::forget() - Remove Values

Remove a value from a nested array or object using a custom separator.

use Abather\CustomDataHelpers\DataHelper;

$data = ['products' => ['desk' => ['price' => 100, 'stock' => 50]]];

// Basic usage
DataHelper::forget($data, 'products.desk.price');
// Result: ['products' => ['desk' => ['stock' => 50]]]

// Using custom separator
DataHelper::forget($data, 'products|desk|stock', '|');
// Result: ['products' => ['desk' => []]]

// Using wildcards to remove from multiple items
$products = [
    ['name' => 'Desk', 'price' => 100, 'discount' => 10],
    ['name' => 'Chair', 'price' => 50, 'discount' => 5]
];
DataHelper::forget($products, '*/discount', '/');
// Removes 'discount' from all products

// Using different separators
$config = [
    'app' => [
        'debug' => true,
        'cache' => ['enabled' => true, 'ttl' => 3600]
    ]
];
DataHelper::forget($config, 'app/cache/ttl', '/');
// Result: ['app' => ['debug' => true, 'cache' => ['enabled' => true]]]

DataHelper::forget($config, 'app->cache->enabled', '->');
// Result: ['app' => ['debug' => true, 'cache' => []]]

Real-World Use Cases

1. Working with Keys Containing Dots

// API response with dotted keys
$config = [
    'app.name' => 'My Application',
    'app.env' => 'production',
    'database.host' => 'localhost'
];

// Access them safely using a different separator
$appName = _data_get($config, 'app.name', '|'); // 'My Application'

2. URL-Style Paths

$routes = [
    'api' => [
        'v1' => [
            'users' => 'UsersController@index'
        ]
    ]
];

$controller = _data_get($routes, 'api/v1/users', '/');
// 'UsersController@index'

3. Object Notation Style

$data = [
    'user' => [
        'profile' => [
            'settings' => [
                'theme' => 'dark'
            ]
        ]
    ]
];

$theme = _data_get($data, 'user->profile->settings->theme', '->');
// 'dark'

Backward Compatibility

The DataHelper class and helper functions use dot notation by default, making them compatible with Laravel's conventions. The package uses underscore-prefixed global functions (_data_*) to avoid conflicts with Laravel's built-in helpers.

// Using default dot separator (no conflicts with Laravel)
DataHelper::get($array, 'user.name');
DataHelper::set($array, 'user.email', 'john@example.com');
DataHelper::has($array, 'user.profile');
DataHelper::forget($array, 'user.settings');

// Or using global helpers
_data_get($array, 'user.name');
_data_set($array, 'user.email', 'john@example.com');
_data_has($array, 'user.profile');
_data_forget($array, 'user.settings');

Features

  • ✅ Custom separator support for all data helpers
  • ✅ Wildcard operations (*)
  • ✅ Placeholder support ({first}, {last})
  • ✅ Works with arrays and objects
  • ✅ Laravel Collections support
  • ✅ 100% backward compatible with Laravel's default helpers
  • ✅ No configuration needed
  • ✅ Auto-discovery enabled
  • ✅ Fully tested

Requirements

  • PHP 8.4 or higher
  • Laravel 11.x or 12.x

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

abather/custom-data-helpers 适用场景与选型建议

abather/custom-data-helpers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「data_set」 「Abather」 「custom-data-helpers」 「data_get」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 abather/custom-data-helpers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 abather/custom-data-helpers 我们能提供哪些服务?
定制开发 / 二次开发

基于 abather/custom-data-helpers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-13