定制 jasny/dotkey 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

jasny/dotkey

Composer 安装命令:

composer require jasny/dotkey

包简介

Dot notation access for objects and arrays

README 文档

README

jasny-banner

DotKey

PHP Scrutinizer Code Quality Code Coverage Packagist Stable Version Packagist License

Dot notation access for objects and arrays.

Inspired by the node.js Dotty library.

Installation

composer require jasny/dotkey

Usage

use Jasny\DotKey\DotKey;

$subject = [
  "a" => [
    "b" => [
      "x" => "y"
    ]
  ]
];

DotKey::on($subject)->exists("a.b.x");        // true
DotKey::on($subject)->exists("a.b.z");        // false
DotKey::on($subject)->exists("a.b.x.o");      // false

DotKey::on($subject)->get("a.b.x");           // "y"
DotKey::on($subject)->get("a.b");             // ["x" => "y"]
DotKey::on($subject)->get("a.b.z");           // null
DotKey::on($subject)->get("a.b.x.o");         // Throws ResolveException because a.b.x is a string

DotKey::on($subject)->set("a.b.q", "foo");    // $subject = ["a" => ["b" => ["x" => "y", "q" => "foo"]]]
DotKey::on($subject)->set("a.d", ['p' => 1]); // $subject = ["a" => ["b" => ["x" => "y"]], "d" => ["p" => 1]]
DotKey::on($subject)->set("a.c.x", "bar");    // Throws ResolveException because a.c doesn't exist
DotKey::on($subject)->set("a.b.x.o", "qux");  // Throws ResolveException because a.b.x is a string

DotKey::on($subject)->put("a.b.q", "foo");    // $subject = ["a" => ["b" => ["x" => "y", "q" => "foo"]]]
DotKey::on($subject)->put("a.c.x", "bar");    // $subject = ["a" => ["b" => ["x" => "y"]], "c" => ["x" => "bar"]]

DotKey::on($subject)->remove("a.b.x");        // $subject = ["a" => ["b" => []]]
DotKey::on($subject)->remove("a.c.z");        // $subject isn't modified
DotKey::on($subject)->remove("a.b.x.o");      // Throws ResolveException because a.b.x is a string
DotKey::on($subject)->remove("a.b");          // $subject = ["a" => []]

DotKey::on($subject)->update("a.b", fn($value) => array_map('strtoupper', $value)); // $subject = ["a" => ["b" => ["x" => "Y"]]]

The subject may be an array or object. If an object implements ArrayAccess it will be treated as array.

use Jasny\DotKey\DotKey;

$obj = (object)["a" => (object)["b" => (object)["x" => "y"]]];

DotKey::on($obj)->exists("a.b.x");
DotKey::on($obj)->set("a.b.q", "foo");

exists() will return false when trying access a private or static property. All other methods will throw a ResolveException.

Copy

By default, the target is modified in place. With the onCopy() factory method, a copy will be made instead.

use Jasny\DotKey\DotKey;

$source = ["a" => ["b" => ["x" => "y"]]];

DotKey::onCopy($source, $copy)->set("a.b.q", "foo");  // $copy = ["a" => ["b" => ["x" => "y", "q" => "foo"]]]

With onCopy(), objects will be cloned if they're modified.

use Jasny\DotKey\DotKey;

$source = (object)["f" => (object)["x" => "y"], "g" => (object)["x" => "z"]]]];

DotKey::onCopy($source, $copy)->set("f.q", "foo");

$copy === $source;       // false, source is cloned
$copy->f === $source->f; // false, `f` is cloned and modified
$copy->g === $source->g; // true, `g` is not cloned

Delimiter

The default delimiter is a dot, but any string can be used as delimiter. Leading and trailing delimiters are stripped.

use Jasny\DotKey\DotKey;

DotKey::on($subject)->exists('/a/b/x', '/');
DotKey::on($subject)->get("/a/b/x", '/');
DotKey::on($subject)->set("/a/b/q", "foo", '/');
DotKey::on($subject)->put("/a/b/q", "foo", '/');
DotKey::on($subject)->remove("/a/b/q", '/');

DotKey::on($subject)->exists('a::b::c', '::');

Set vs Put

  • set('a.b.c', 1) requires 'a.b' to exist and be an object or array. If that's not the case, a ResolveException is thrown.
  • put('a.b.c', 1) will always result in $result["a"]["b"]["c"] === 1. If 'a.b' doesn't exist, it will be created. If it already exists and isn't an array or object, it will be overwritten.

The structure put() creates is either an associative array or an \stdClass object, depending on the type of the subject. Passing true as fourth argument forces this to an array, while passing false forces it to create an object.

use Jasny\DotKey\DotKey;

$subject = ["a" => null];
$obj = (object)["a" => null];

DotKey::on($subject)->put("a.b.o", 1, '.');        // ["a" => ["b" => ["o" => 1]]]
DotKey::on($subject)->put("a.b.o", 1, '.', true);  // ["a" => ["b" => ["o" => 1]]]
DotKey::on($subject)->put("a.b.o", 1, '.', false); // ["a" => (object)["b" => (object)["o" => 1]]]

DotKey::on($obj)->put("a.b.o", 1, '.');            // (object)["a" => (object)["b" => (object)["o" => 1]]]
DotKey::on($obj)->put("a.b.o", 1, '.', true);      // (object)["a" => ["b" => ["o" => 1]]]
DotKey::on($obj)->put("a.b.o", 1, '.', false);     // (object)["a" => (object)["b" => (object)["o" => 1]]]

jasny/dotkey 适用场景与选型建议

jasny/dotkey 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 223.52k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2015 年 08 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 jasny/dotkey 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 14
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-08-12