robertmarney/lara-hierarchial-collections

将平面集合转换为嵌套层次结构

1.1.0 2024-05-18 14:42 UTC

This package is auto-updated.

Last update: 2024-09-08 05:37:07 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

该软件包可以将层次数据集合扩展为节点,以层次结构组织数据。

该软件包支持无限起始节点,并已测试到10层深度。

用例

  • 组织结构图
  • 账户图表

需求

  • Illuminate Collections 8+/9+/10+/11+ (包含在 Laravel 8+/9+/10+/11+ 中)
  • PHP 8.0 / 8.1 / 8.2 / 8.3

安装

您可以通过 composer 安装该软件包

composer require robertmarney/lara-hierarchial-collections

基本用法

该工具接受支持集合或 Eloquent 集合,在集合中我们期望 Eloquent 模型或 StdClass 对象。

假设主键为 id 和父标识符为 parent_id

$collection = User::select(['id', 'parent_id', 'name'])->get();

$hierarchy = Hierarchical::make($collection);    // or new Hierarchical($collection);

$result = $hierarchy->toArray();

// Result:

[
    'id' => 1,
    'parent_id' => null,
    'name' => 'John Doe'
    'children' => [
        [
            'id' => 1000,
            'parent_id' => 1,
            'name' => 'Sue Smith'
            'children' => [//...]
        ],
        //...
    ]               
]

自定义本地键

如果您不使用 id(例如 uuid),可以覆盖本地比较值

$hierarchy = new Hierarchical($collection, localIdentifier: 'custom_primary_key')

自定义父键

同样,如果本地关系不是基于默认的 parent_id,您也可以更改父键

$hierarchy = new Hierarchical($collection, parentIdentifier: 'custom_parent_id')

提供 relationName 属性将更改放置子项的集合名称

$hierarchy = (new Hierarchical($collection, relationName: 'descendants'))->toArray();

// Result:

[
    'id' => 1,
    'parent_id' => null,
    'name' => 'John Doe'
    'descendants' => [
        [
            'id' => 1000,
            'parent_id' => 1,
            'name' => 'Sue Smith'
            'descendants' => [//...]
        ],
        //...
    ]               
]

集合宏(仅 Laravel)

该软件包还提供了一个集合宏,可以轻松地将集合转换为层次结构

$collection = User::select(['id', 'parent_id', 'name'])->get();
$result = $collection->toHierarchical();

辅助方法

祖先

Hierarchical::make($collection)->ancestorsOf($id); // Will resolve all ancestors of the given id

Hierarchical::make($collection)->ancestorsOf($item); // Will resolve all ancestors of the given item

后代

Hierarchical::make($collection)->descendantsOf($id); // Will resolve all descendants of the given id`

Hierarchical::make($collection)->descendantsOf($item); // Will resolve all descendants of the given item

兄弟

Hierarchical::make($collection)->siblingsOf($id); // Will resolve all siblings of the given id

Hierarchical::make($collection)->siblingsOf($item); // Will resolve all siblings of the given item

深度

Hierarchical::make($collection)->depthOf($id); // Will resolve the depth of the given id (eg 0, 1, 2, 3, ...)

Hierarchical::make($collection)->depthOf($item); // Will resolve the depth of the given item

流畅比较

Hierarchical::make($collection)->is($id)->siblingOf($id); // boolean

Hierarchical::make($collection)->is($item)->siblingOf($item); // boolean

Hierarchical::make($collection)->is($id)->childOf($id); // boolean

Hierarchical::make($collection)->is($item)->childOf($item); // boolean

Hierarchical::make($collection)->is($id)->ancestorOf($id); // boolean

Hierarchical::make($collection)->is($item)->ancestorOf($item); // boolean

旧版用法(已弃用)

$laraHierarchy = new RCM\LaraHierarchy\LaraHierarchy();

$collection = User::select(['id', 'parent_id', 'name'])->get();

$hierarchy = $laraHierarchy->collectionToHierarchy($collection)->toArray();

// Result:

[
    'id' => 1,
    'parent_id' => null,
    'name' => 'John Doe'
    'children' => [
        [
            'id' => 1000,
            'parent_id' => 1,
            'name' => 'Sue Smith'
            'children' => [//...]
        ],
        //...
    ]               
]

自定义本地键

如果您不使用 id(例如 uuid),可以覆盖本地比较值

$hierarchy = $laraHierarchy->collectionToHierarchy($collection, localIdentifier: 'custom_primary_key')

自定义父键

同样,如果本地关系不是基于默认的 parent_id,您也可以更改父键

$hierarchy = $laraHierarchy->collectionToHierarchy($collection, parentIdentifier: 'custom_parent_id')

提供 relationName 属性将更改放置子项的集合名称

$hierarchy = $laraHierarchy->collectionToHierarchy($collection, relationName: 'descendants')->toArray();

// Result:

[
    'id' => 1,
    'parent_id' => null,
    'name' => 'John Doe'
    'descendants' => [
        [
            'id' => 1000,
            'parent_id' => 1,
            'name' => 'Sue Smith'
            'descendants' => [//...]
        ],
        //...
    ]               
]

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近更改的更多信息。

鸣谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件