obernard / property-indexer
基于symfony/property-access的对象属性操作工具
Requires
- php: >=8.0
- symfony/property-access: >=5.4
Requires (Dev)
- phpunit/phpunit: ^9.3
README
-
PorpertyIndexer
在键值映射中索引对象/数组的属性。 -
PorpertyTree
从对象集合中构建树结构。
PorpertyIndexer
和 PropertyTree
都是可迭代的。
索引键和值,另一个中的节点和叶节点通过 symfony/property-access 组件从对象或数组中检索。
安装
# from packagist composer require obernard/property-indexer # or from github git clone git@github.com:pytoccaz/php-property-indexer.git
Porperty Indexer
PorpertyIndexer
在键值映射中索引对象/数组的属性。
选择
- 一个字符串|int 属性作为键
- 一个混合属性或对象|数组本身作为值
索引对象属性
假设你有具有属性 id
和 value
的对象
$obj1->id == "id1"; $obj1->value == "value1"; $obj2->id == "id2"; $obj2->value == "value2";
为 id&value 对象创建索引器
$indexer = new Obernard\PropertyIndexer\PropertyIndexer('id', 'value');
添加对象
$indexer->add($obj1)->add($obj2);
通过其键检索索引值
$index->get('id1') // returns "value1" $index->get('id2') // returns "value2"
或直接通过索引对象的引用
$index->get($obj2) // returns "value2"
索引数组属性
假设你有具有 id
和 value
键的数组映射
$array1= ["id" => "id1", "value" => "value1"]; $array2 = ["id" => "id2", "value" => "value2"];
为 id&value 数组创建索引器(注意数组属性路径的括号使用)
$indexer = new Obernard\PropertyIndexer\PropertyIndexer('[id]', '[value]');
添加数组
$indexer->add($array1)->add($array2);
通过其键检索索引值
$index->get('id1') // returns "value1" $index->get('id2') // returns "value2"
批量加载集合
使用 load
方法
$indexer = new Obernard\PropertyIndexer\PropertyIndexer('[id]', '[value]'); $collection = [ ["id" => "id1", "value" => "value1"], ["id" => "id2", "value" => "value2"] ] $indexer = $indexer->load($collection); $index->get('id1') // returns value1 $index->get('id2') // returns value2
或使用第三个参数调用 PropertyIndexer
$indexer = new Obernard\PropertyIndexer\PropertyIndexer('[id]', '[value]', $collection);
索引对象或数组(而不是它们的属性)
在调用 PropertyIndexer
时不要指定值路径或将其设置为 null
$objectIndexer = new Obernard\PropertyIndexer\PropertyIndexer('id'); $arrayIndexer = new Obernard\PropertyIndexer\PropertyIndexer('[id]', null);
Porperty Tree Builder
PorpertyTree
从对象集合中构建树结构。
从处理集合的项目中选择叶路径和值。
Porperty Tree 基础
假设你有具有属性 id
、value
和 date
的对象
$obj1->id == "id1"; $obj1->value == "value1"; $obj1->date == "today"; $obj2->id == "id2"; $obj2->value == "value2"; $obj2->date == "today"; // first arg is the collection of objects // second arg is the "leaves" value // third arg is a groupBy-like definition of the tree levels $tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], 'value', ['id', 'date']); // ["tree":"Obernard\PropertyIndexer\PropertyTree":private]=> // array(2) { // ["id1"]=> // array(1) { // ["today"]=> // string(6) "value1" // } // ["id2"]=> // array(1) { // ["today"]=> // string(6) "value2" // } // } $tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], 'value', ['date', 'id']); // ["tree":"Obernard\PropertyIndexer\PropertyTree":private]=> // array(1) { // ["today"]=> // array(2) { // ["id1"]=> // string(6) "value1" // ["id2"]=> // string(6) "value2" // } // }
Porperty Tree 模式 ARRAY_LEAF
注意具有“相同”属性路径的对象之间的可能冲突
$tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], 'value', ['date']); // ["tree":"Obernard\PropertyIndexer\PropertyTree":private]=> // array(1) { // ["today"]=> // string(6) "value2" // }
为了避免可能的冲突,切换到数组类型的叶节点
$tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], 'value', ['date'], PropertyTree::ARRAY_LEAF); // ["tree":"Obernard\PropertyIndexer\PropertyTree":private]=> // array(1) { // ["today"]=> // array(2) { // [0]=> // string(6) "value1" // [1]=> // string(6) "value2" // } // }
Porperty Tree 高级功能
闭包作为 valuePath
$valuePath
第二个参数接受一个接受一个参数的闭包(必须返回 int|string)。当迭代对象数组集合时,PorpertyTree 将将参数绑定到当前对象数组项以提取一个值。
要检索对象项本身,您可以使用 Identity Closure(或更好地将 valuePath
设置为 null
:P)
// Use an Idendity Closure to retrieve Objects themselves: $tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], function($item):string {return $item;}, ['id']); ["tree":"Obernard\PropertyIndexer\PropertyTree":private]=> // array(2) { // ["id1"]=> // object(stdClass)#106 (3) { // ["id"]=> // string(3) "id1" // ["value"]=> // string(6) "value1" // ["date"]=> // string(5) "today" // } // ["id2"]=> // object(stdClass)#83 (3) { // ["id"]=> // string(3) "id2" // ["value"]=> // string(6) "value2" // ["date"]=> // string(5) "today" // } // } // Or a simple way : $tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], null, ['id']);
闭包作为 groupByproperties
数组 $groupByproperties
第三个参数接受一个接受一个参数的闭包(必须返回 int|string)。当迭代对象数组集合时,PorpertyTree 将将参数绑定到当前对象数组项以提取树路径节点
$tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], null, [function($item):string {return $item->id;}]); // is a complicated way to get the same resulting tree as : $tree = new Obernard\PropertyIndexer\PropertyTree([$obj1, $obj2], null, ['id']);
这两个示例很简单。闭包用于执行对象属性无法原生提供的复杂任务(检索路径或叶节点)。
测试
运行 composer test
。
贡献
请随意提交拉取请求。
许可证
MIT
版权(c)2022 Olivier BERNARD