PHP Classes

PHP Tree tools: Build and traverse structures organized as trees

Recommend this page to a friend!
  Info   View files Documentation   View files View files (23)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 66 This week: 1All time: 10,322 This week: 560Up
Version License PHP version Categories
tree-tools-php 1.0MIT/X Consortium ...7.4Algorithms, Libraries, Data types, PHP 7
Description 

Author

This package can build and traverse structures organized as trees.

It provides one class that can build a tree structure from a collection of data structures (objects or arrays) and several other parameters of each collection element like:

- The name of the key of the primary identifier

- The name of the key of the parent element

- The name of the key of children's elements

- The name of the item container


The package also provides another class to traverse a tree defined by the tree builder class. The tree can be traversed in two ways: depth-first and breadth-first.

Picture of Smoren  Freelight
  Performance   Level  
Name: Smoren Freelight <contact>
Classes: 36 packages by
Country: Russian Federation Russian Federation
Age: 34
All time rank: 290379 in Russian Federation Russian Federation
Week rank: 106 Up7 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 15x

Documentation

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

| Reducer | Description | Code Snippet | |---------------------------------------------------|--------------------------------------------|------------------------------------------------------------------| | traverseDepthFirst | Iterates a tree using depth-first search | TreeWalker::traverseDepthFirst($data, $childrenContainerKey) | | traverseBreadthFirst | Iterates a tree using breadth-first search | TreeWalker::traverseBreadthFirst($data, $childrenContainerKey) |

Tree Builder

| Reducer | Description | Code Snippet | |-------------------|------------------------------------------|-----------------------------------------------------------------------------------------------------------| | build | Builds a tree from given flat collection | TreeBuilder::build($collection, $idField, $parentIdField, $childrenContainerField, $itemContainerField) |

Usage

Tree Walker

Traverse Depth First

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


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.

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.


  Files folder image Files  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (2 files)
Files folder imagetests (3 files, 2 directories)
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file codeception.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.xml Data Auxiliary data
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file test_master.yml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
  Plain text file TreeBuilder.php Class Class source
  Plain text file TreeWalker.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageunit (3 directories)
Files folder image_support (1 file)
  Accessible without login Plain text file coding_standard.xml Data Auxiliary data
  Accessible without login Plain text file unit.suite.yml Data Auxiliary data
  Accessible without login Plain text file _bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  tests  /  unit  
File Role Description
Files folder imageFixture (4 files)
Files folder imageTreeBuilder (3 files)
Files folder imageTreeWalker (2 files)

  Files folder image Files  /  tests  /  unit  /  Fixture  
File Role Description
  Plain text file ArrayIteratorFixture.php Class Class source
  Plain text file GeneratorFixture.php Class Class source
  Plain text file IteratorAggregateFixture.php Class Class source
  Plain text file TreeItemFixture.php Class Class source

  Files folder image Files  /  tests  /  unit  /  TreeBuilder  
File Role Description
  Plain text file ArrayTest.php Class Class source
  Plain text file ClassObjectTest.php Class Class source
  Plain text file StdObjectTest.php Class Class source

  Files folder image Files  /  tests  /  unit  /  TreeWalker  
File Role Description
  Plain text file TraverseBreadthFirstTest.php Class Class source
  Plain text file TraverseDepthFirstTest.php Class Class source

  Files folder image Files  /  tests  /  _support  
File Role Description
  Plain text file UnitTester.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:66
This week:1
All time:10,322
This week:560Up