定制 jbizzay/php-dot 二次开发

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

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

jbizzay/php-dot

Composer 安装命令:

composer require jbizzay/php-dot

包简介

Structured array helper with dot notation

README 文档

README

This repo is unmaintained. Check out this if you need something simliar: https://github.com/adbario/php-dot-notation

Helps manage arrays in PHP with dot notation. Useful for configs, meta data, or just working with large associative arrays.

Often, working with large arrays is cumbersome and prone to errors. Having to drill down into multiple levels of an array, checking isset all the way, is not fun.

if (isset($data['level1']) && isset($data['level1']['level2'])) {
    $value = $data['level1']['level2']['key'];
} else {
    $value = null;
}

Instead, you can do this:

$dot = new Dot($data);
$value = $dot->get('level1.level2.key');

It will simply return null, instead of throwing an undefined index error, if any part of the dot path doesn't exist. Also, working with dot notation makes code more readable and easier to write.

Usage

Create a Dot

use Jbizzay\Dot;

// Create empty dot
$dot = new Dot;

// Or, initialize with an array of data
$data = [
  'stats' => [
    'web' => [
      'hits' => 99
    ],
    'mobile' => [

    ]
  ]
];

$dot = new Dot($data);

Get

With no argument, get returns the entire data array. Pass a dot notation string to access parts of the data array.

$dot->get(); // Returns full data array

$dot->get('stats.web.hits'); // Returns 99

$dot->get('stats.mobile.hits'); // Returns null

$dot->get('some.random.undefined.key'); // Returns null

 

Set

You can set any data type, including callable functions. Any levels that don't already exist, will be created as associative arrays. Set returns the same instance of Dot allowing for method chaining. Using a callable type will recieve the currently set value (if it exists) as an argument.

$dot
  ->set('stats.web.last_updated', new DateTime)
  ->set('stats.web.allow_tracking', true)
  ->set('stats.web.hits', function ($hits) {
    $hits++;
    return $hits;
  });

$dot->get('stats.web');

/* Returns:
Array
(
    [hits] => 100
    [last_updated] => DateTime Object
        (
            [date] => 2017-07-21 14:50:34.000000
            [timezone_type] => 3
            [timezone] => America/Los_Angeles
        )

    [allow_tracking] => 1
)
*/

Unset

Unset a value, returns the dot instance

$dot
  ->unset('path.to.value')
  ->unset('some.other.value');

Has

Determines if a key is set

$dot->has('stats.web'); // true
$dot->has('some.random.key'); // false

Define

Gets a value, but if the key is not set, initialize the key with a value. You can also use a callable type. By default, initializes with array. Returns the dot path value

$dot->define('leads.emails'); // Sets to an array
$hits = $dot->define('stats.mobile.hits', 0);
$dot->define('stats.console.hits', function () {
  // This function is called if this key is not set yet
  return 0;
});

Merge

Recursively merges an array into the dot array. First argument can be a dot path, an array, or a function that returns an array. The second argument can be an array or a function, but should only be used if first argument is a key. Returns the dot instance

// Merge into whole data array
$dot->merge([
  'stats' => [
    'web' => [
      'hits' => 123, 
      'leads' => 321
    ]
  ]
]);

$dot->get();

/* Returns:
Array
(
  [stats] => Array
    (
      [web] => Array
        (
          [hits] => 123
          [last_updated] => DateTime Object
            (
              [date] => 2017-07-25 13:34:49.000000
              [timezone_type] => 3
              [timezone] => America/Los_Angeles
            )

          [allow_tracking] => 1
          [leads] => 321
        )

      [mobile] => Array
        (
        )

    )
)
*/

// Merge array into a dot path
$dot->merge('stats.mobile', [
  'issues' => 33
]);

// Merge using function
$dot->merge('stats.mobile', function ($mobile) {
  return ['updated' => new DateTime];
});

// Merge into whole data array with function
$dot->merge(function ($data) {
  return ['new' => 123];
});

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固