tree-tools-php

Форк
0

2 года назад
2 года назад
2 года назад
2 года назад
2 года назад
2 года назад
2 года назад
2 года назад
2 года назад
README.md

PHP Tree Tools

Packagist PHP Version Support Scrutinizer Code Quality Coverage Status Build and test License: MIT

Library for working with trees.

How to install to your project

composer require smoren/tree-tools

Quick reference

Tree Walker

ReducerDescriptionCode Snippet
traverseDepthFirstIterates a tree using depth-first searchTreeWalker::traverseDepthFirst($data, $childrenContainerKey)
traverseBreadthFirstIterates a tree using breadth-first searchTreeWalker::traverseBreadthFirst($data, $childrenContainerKey)

Tree Builder

ReducerDescriptionCode Snippet
buildBuilds a tree from given flat collectionTreeBuilder::build($collection, $idField, $parentIdField, $childrenContainerField, $itemContainerField)

Usage

Tree Walker

Traverse Depth First

Iterates a tree like a flat collection using depth-first traversal.

TreeWalker::traverseDepthFirst(iterable $data, ?string $childrenContainerKey = null): Generator

If $childrenContainerKey is not null looks for children items using by this key only.

Otherwise, considers any subarray to contain children.

use Smoren\TreeTools\TreeWalker;
$tree = [
[
'id' => 1,
'children' => [
['id' => 11],
[
'id' => 12,
'children' => [
['id' => 121],
['id' => 122],
],
],
],
],
[
'id' => 2,
'children' => [
['id' => 21],
],
],
['id' => 3],
];
$result = [];
foreach(TreeWalker::traverseDepthFirst($tree) as $item) {
$result[] = $item['id'];
}
var_dump($result);
// [1, 11, 12, 121, 122, 2, 21, 3]

Traverse Breadth First

Iterates a tree like a flat collection using depth-breadth traversal.

TreeWalker::traverseBreadthFirst(iterable $data, ?string $childrenContainerKey = null): Generator

If $childrenContainerKey is not null looks for children items using by this key only.

Otherwise, considers any subarray to contain children.

use Smoren\TreeTools\TreeWalker;
$tree = [
[
'id' => 1,
'children' => [
['id' => 11],
[
'id' => 12,
'children' => [
['id' => 121],
['id' => 122],
],
],
],
],
[
'id' => 2,
'children' => [
['id' => 21],
],
],
['id' => 3],
];
$result = [];
foreach(TreeWalker::traverseBreadthFirst($tree) as $item) {
$result[] = $item['id'];
}
var_dump($result);
// [1, 2, 3, 11, 12, 21, 121, 122]

Tree Builder

Build

Builds a tree from given flat collection of items with relations.

TreeBuilder::build(
    iterable $collection,
    string $idField = 'id',
    string $parentIdField = 'parent_id',
    string $childrenContainerField = 'children',
    string $itemContainerField = 'item'
): array
use Smoren\TreeTools\TreeBuilder;
$input = [
['id' => 1, 'name' => 'Item 1', 'parent_id' => null],
['id' => 2, 'name' => 'Item 1.1', 'parent_id' => 1],
['id' => 3, 'name' => 'Item 1.2', 'parent_id' => 1],
['id' => 4, 'name' => 'Item 1.1.1', 'parent_id' => 2],
['id' => 5, 'name' => 'Item 2', 'parent_id' => null],
['id' => 6, 'name' => 'Item 3', 'parent_id' => null],
['id' => 7, 'name' => 'Item 3.1', 'parent_id' => 6],
['id' => 8, 'name' => 'Item 3.2', 'parent_id' => 6],
];
$tree = TreeBuilder::build($input);
print_r($tree);
/*
[
[
'id' => 1,
'name' => 'Item 1',
'parent_id' => null,
'children' => [
[
'id' => 2,
'name' => 'Item 1.1',
'parent_id' => 1,
'children' => [
[
'id' => 4,
'name' => 'Item 1.1.1',
'parent_id' => 2,
'children' => [],
]
],
],
[
'id' => 3,
'name' => 'Item 1.2',
'parent_id' => 1,
'children' => [],
],
],
],
[
'id' => 5,
'name' => 'Item 2',
'parent_id' => null,
'children' => [],
],
[
'id' => 6,
'name' => 'Item 3',
'parent_id' => null,
'children' => [
[
'id' => 7,
'name' => 'Item 3.1',
'parent_id' => 6,
'children' => [],
],
[
'id' => 8,
'name' => 'Item 3.2',
'parent_id' => 6,
'children' => [],
],
]
],
]
*/

Unit testing

composer install
composer test-init
composer test

License

PHP Tree Tools is licensed under the MIT License.

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.