aklump/data

用于与对象和数组中的数据进行交互的通用接口。

0.0.28 2017-05-31 01:04 UTC

README

************* 已弃用 *************

建议的替代方案

// 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');

摘要

提供了一种从对象或数组获取数据的方法,具有默认选项,如Lodash的get方法。将逐步添加处理数组/对象数据的其他方法。

理由

给定一个多维数组,在原生PHP中你会这样做

print isset($multi_array['do']['re']) ? $multi_array['do']['re'] : 'default';

使用这个类,你会这样做

$data = new Data;
print $data->get($multi_array, 'do.re', 'default');

并不太令人印象深刻...但是等等...想象一下当你有复杂对象时,比如将Drupal 7字段值翻译成西班牙语。

print isset($node->field_description['es']['1']['value']) ? $node->field_description['es']['1']['value'] : '';

// vs...

print $data->get($node, 'field_description.1.value', '');

或者当你在Drupal 8上工作几天时。

print isset($node->field_description) ? $node->get('field_description')->get(1)->value : '';

vs.

print $data->get($node, 'field_description.1.value', '');

这就是一致接口方法开始变得有意义的地方。顺便说一句,有一个使用这个类不同实现的Drupal模块,可以在这里找到。

默认值

每个对::get的调用都可以有一个默认值,这样可以消除if/thens或issets的需求。

$country = $data->get($record, 'home.address.country', 'U.S.');

回调

你可以传递一个回调来处理值,例如通过id加载记录。

$url = $data->get($record, 'picture.id', 'http://mysite.com/default.jpg', function($id) {
    return load_url_by_record_id($id);
});

详细信息

<?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()

无条件地在路径处设置值。

Data::ensure()

确保路径已设置,如果键/属性存在,则不会覆盖。

Data::fill()

这是一个条件设置方法。它只有在路径为空(或基于其他测试,例如示例3)时才会填充。

示例 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');

示例 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); 

示例 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');

致谢