bluestone/tree
最新稳定版本:v1.0.2
Composer 安装命令:
composer require bluestone/tree
包简介
Build tree data structure
README 文档
README
Installation
This package requires php:^8.1.
You can install it via composer:
composer require bluestone/tree
Usage
Tree instance need items to forge the expected data structure.
How to forge a tree with array as items ?
By default, a tree expect id and parentId fields in give items like this :
use Bluestone\Tree\Tree; $items = [ ['id' => 1, 'parentId' => null, 'username' => 'Robert'], ['id' => 2, 'parentId' => 1, 'username' => 'John'], ['id' => 3, 'parentId' => 1, 'username' => 'Jane'], ]; $tree = new Tree($items); // Get Robert's children $robert = $tree->getNodeById(1); // Return the node representing Robert $robert->username; // Robert $robert->getChildren(); // Return an array containing John & Jane // Get Jane's parent $jane = $tree->getNodeById(3); $jane->getParent(); // Return Robert's node
How to forge a tree with custom fields ?
You can customize the parentKey and the primaryKey like this :
use Bluestone\Tree\Tree; $items = [ [ 'uuid' => 'b08f82b9-e8cc-44fb-b99a-4929bfcf02a4', 'parentUuid' => null, 'username' => 'Robert', ], [ 'uuid' => 'eaaf3215-17ba-4779-b444-d4a8203f1096', 'parentUuid' => 'b08f82b9-e8cc-44fb-b99a-4929bfcf02a4', 'username' => 'John', ], ]; $tree = new Tree($items, 'parentUuid', 'uuid');
How to serialize a tree to json ?
use Bluestone\Tree\Tree; $items = [ ['id' => 1, 'parentId' => null, 'name' => 'Chicken'], ['id' => 2, 'parentId' => 1, 'name' => 'Egg'], ]; $tree = new Tree($items); $json = json_encode($tree);
The result of the json_encode is a string with contains :
{
"primaryKey": "id",
"parentKey": "parentId",
"nodes": [
{"id": 1, "parentId": null, "name": "Chicken"},
{"id": 2, "parentId": 1, "name": "Egg"}
]
}
How to forge a tree with object as items ?
You can pass object as items with expected id and parentId public property like this :
use Tests\Artifacts\Person; use Bluestone\Tree\Tree; $items = [ new Person(1, null, 'Chicken'), new Person(2, 1, 'Egg'), ]; $tree = new Tree($items); $chicken = $tree->getNodeById(1); $chicken->name; // Chicken $chicken->getChildren(); // Return Egg's node
Contributing
Tree is an open source project under MIT License and is open for contributions.
统计信息
- 总下载量: 4.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-23