firstred/php-dot-notation 问题修复 & 功能扩展

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

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

firstred/php-dot-notation

Composer 安装命令:

composer require firstred/php-dot-notation

包简介

PHP dot notation access to arrays

README 文档

README

Build Status Coverage Status Total Downloads License

Dot provides an easy access to arrays of data with dot notation in a lightweight and fast way. Inspired by Laravel Collection.

Dot implements PHP's ArrayAccess interface and Dot object can also be used the same way as normal arrays with additional dot notation.

Examples

With Dot you can chage this regular array syntax:

$array['info']['home']['address'] = 'Kings Square';

echo $array['info']['home']['address'];

// Kings Square

to this (Dot object):

$dot->set('info.home.address', 'Kings Square');

echo $dot->get('info.home.address');

or even this (ArrayAccess):

$dot['info.home.address'] = 'Kings Square';

echo $dot['info.home.address'];

Install

Install the latest version using Composer:

$ composer require adbario/php-dot-notation

Usage

Create a new Dot object:

$dot = new \Adbar\Dot;

// With existing array
$dot = new \Adbar\Dot($array);

You can also use a helper function to create the object:

$dot = dot();

// With existing array
$dot = dot($array);

Methods

Dot has the following methods:

add()

Sets a given key / value pair if the key doesn't exist already:

$dot->add('user.name', 'John');

// Equivalent vanilla PHP
if (!isset($array['user']['name'])) {
    $array['user']['name'] = 'John';
}

Multiple key / value pairs:

$dot->add([
    'user.name' => 'John',
    'page.title' => 'Home'
]);

all()

Returns all the stored items as an array:

$values = $dot->all();

clear()

Deletes the contents of a given key (sets an empty array):

$dot->clear('user.settings');

// Equivalent vanilla PHP
$array['user']['settings'] = [];

Multiple keys:

$dot->clear(['user.settings', 'app.config']);

All the stored items:

$dot->clear();

// Equivalent vanilla PHP
$array = [];

count()

Returns the number of items in a given key:

$dot->count('user.siblings');

Items in the root of Dot object:

$dot->count();

// Or use coun() function as Dot implements Countable
count($dot);

delete()

Deletes the given key:

$dot->delete('user.name');

// ArrayAccess
unset($dot['user.name']);

// Equivalent vanilla PHP
unset($array['user']['name']);

Multiple keys:

$dot->delete([
    'user.name',
    'page.title'
]);

get()

Returns the value of a given key:

echo $dot->get('user.name');

// ArrayAccess
echo $dot['user.name'];

// Equivalent vanilla PHP < 7.0
echo isset($array['user']['name']) ? $array['user']['name'] : null;

// Equivalent vanilla PHP >= 7.0
echo $array['user']['name'] ?? null;

Returns a given default value, if the given key doesn't exist:

echo $dot->get('user.name', 'some default value');

has()

Checks if a given key exists (returns boolean true or false):

$dot->has('user.name');

// ArrayAccess
isset($dot['user.name']);

Multiple keys:

$dot->has([
    'user.name',
    'page.title'
]);

isEmpty()

Checks if a given key is empty (returns boolean true or false):

$dot->isEmpty('user.name');

// ArrayAccess
empty($dot['user.name']);

// Equivalent vanilla PHP
empty($array['user']['name']);

Multiple keys:

$dot->isEmpty([
    'user.name',
    'page.title'
]);

Checks the whole Dot object:

$dot->isEmpty();

merge()

Merges a given array or another Dot object:

$dot->merge($array);

// Equivalent vanilla PHP
array_merge($originalArray, $array);

Merges a given array or another Dot object with the given key:

$dot->merge('user', $array);

// Equivalent vanilla PHP
array_merge($originalArray['user'], $array);

pull()

Returns the value of a given key and deletes the key:

echo $dot->pull('user.name');

// Equivalent vanilla PHP < 7.0
echo isset($array['user']['name']) ? $array['user']['name'] : null;
unset($array['user']['name']);

// Equivalent vanilla PHP >= 7.0
echo $array['user']['name'] ?? null;
unset($array['user']['name']);

Returns a given default value, if the given key doesn't exist:

echo $dot->pull('user.name', 'some default value');

Returns all the stored items as an array and clears the Dot object:

$items = $dot->pull();

push()

Pushes a given value to the end of the array in a given key:

$dot->push('users', 'John');

// Equivalent vanilla PHP
$array['users'][] = 'John';

Pushes a given value to the end of the array:

$dot->push('John');

// Equivalent vanilla PHP
$array[] = 'John';

set()

Sets a given key / value pair:

$dot->set('user.name', 'John');

// ArrayAccess
$dot['user.name'] = 'John';

// Equivalent vanilla PHP
$array['user']['name'] = 'John';

Multiple key / value pairs:

$dot->set([
    'user.name' => 'John',
    'page.title'     => 'Home'
]);

setArray()

Replaces all items in Dot object with a given array:

$dot->setArray($array);

setReference()

Replaces all items in Dot object with a given array as a reference and all future changes to Dot will be made directly to the original array:

$dot->setReference($array);

toJson()

Returns the value of a given key as JSON:

echo $dot->toJson('user');

Returns all the stored items as JSON:

echo $dot->toJson();

License

MIT license

firstred/php-dot-notation 适用场景与选型建议

firstred/php-dot-notation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 129 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 firstred/php-dot-notation 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-03-06