aklump/data
Composer 安装命令:
composer require aklump/data
包简介
Common interface for interacting with data in objects and arrays.
README 文档
README
************* ABANDONED *************
Suggested replacement:
// Setter $setter = (new \Dflydev\DotAccessData\Data($multi_array)); $setter->set('do.re.mi', $value); $multi_array = $setter->export(); // Getter print (new \Dflydev\DotAccessData\Data($multi_array)) ->get('do.re.mi', 'default');
Summary
Provides a common means for getting data from objects or arrays with default option such as Lodash's get method. Other methods for working with array/object data will be added in time.
Rationale
Given a multidimensional array, in vanilla PHP you will do this:
print isset($multi_array['do']['re']) ? $multi_array['do']['re'] : 'default';
Using this class you would do this:
$data = new Data;
print $data->get($multi_array, 'do.re', 'default');
Not too impressive... but wait... imagine when you have complex objects, like say a Drupal 7 field value translated into spanish.
print isset($node->field_description['es']['1']['value']) ? $node->field_description['es']['1']['value'] : '';
// vs...
print $data->get($node, 'field_description.1.value', '');
Or when you need to work in Drupal 8 for a few days.
print isset($node->field_description) ? $node->get('field_description')->get(1)->value : '';
vs.
print $data->get($node, 'field_description.1.value', '');
This is where a consistent interface approach starts to make sense. By the way, there is a Drupal module that uses a different implementation of this class which can be found here.
Default value
Every call to ::get can have a default value, which removes the need for if/thens or issets.
$country = $data->get($record, 'home.address.country', 'U.S.');
Callback
You can pass a callback to process the value such as loading a record by id.
$url = $data->get($record, 'picture.id', 'http://mysite.com/default.jpg', function($id) {
return load_url_by_record_id($id);
});
Details
<?php
use AKlump\Data\Data;
$data = new Data;
// Let's create a data subject, from which we want to pull data.
$a = array('b' => array('c' => 'd'));
// First let's pull data that exits.
// Because $a['b']['c'] has a value 'd', it will be returned. Default is ignored.
$value = $data->get($a, 'b.c', 'e');
$value === 'c';
// Because $a['b']['z'] is not set then the default value comes back, 'e'.
$value = $data->get($a, 'b.z', 'e');
$value === 'e';
// This interface works on objects or arrays, regardless. Let's convert this to a nested object using json functions.
$a_object = json_decode(json_encode($a));
// Make the same call and you'll get the same answer.
$value = $data->get($a, 'b.c', 'e');
$value === 'c';
Data::set()
Unconditionally sets a value at path.
Data::ensure()
Ensures that a path is set, does not overwrite if the key/property exists.
Data::fill()
This is a conditional setter method. It will fill in only if a path is empty (or based on some other test see example 3).
Example 1
<?php
// In this case the value is filled in.
$array = array('do' => '');
$data->fill($array, 'do', 're');
// The value is filled in because the current value is empty.
$array === array('do' => 're');
Example 2
<?php
// In this case the value is NOT.
$array = array('do' => null);
$data->fill($array, 'do', 're', 'strict');
// The old value of null remains because 're' is a string and the current value not a string; even though it's empty, it will not be replaced because we've used the 'strict' test.
$array === array('do' => null);
Example 3
<?php
// In this case the value is replaced based on a callback.
$array = array('do' => 45);
$data->fill($array, 'do', 're', function ($current, $exists) {
return $exists && is_numeric($current);
});
// The value is replaced because our custom callable tested it as a number and returned true.
$array === array('do' => 're');
Acknowledgments
- Thank you Aaron Jensen (https://github.com/aaronjensen) for introducing me to this concept.
- https://lodash.com/docs/4.16.2#get
aklump/data 适用场景与选型建议
aklump/data 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 652 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 10 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「data」 「array」 「objects」 「getters」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aklump/data 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aklump/data 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aklump/data 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
A set of useful PHP classes.
Adds the EDTF data type to Wikibase
A simple library that allows transform any kind of data to native php data or whatever
Trait providing methods to set class properties with an array.
Traits to build collections of specific objects
统计信息
- 总下载量: 652
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2016-10-03