PHP Classes

PHP Tree tools: Build and traverse structures organized as trees

Recommend this page to a friend!

  Author Author  
Picture of Smoren  Freelight
Name: Smoren Freelight <contact>
Classes: 20 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 9x


  Detailed description   Download Download .zip .tar.gz  
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.

Details

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.


  Classes of Smoren Freelight  >  PHP Tree tools  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: PHP Tree tools
Base name: tree-tools-php
Description: Build and traverse structures organized as trees
Version: -
PHP version: 7.4
License: MIT/X Consortium License
 
  Groups   Applications   Files Files  

  Groups  
Group folder image Algorithms Numerical and statistical algorithms View top rated classes
Group folder image Libraries Frameworks and libraries of cooperating classes View top rated classes
Group folder image Data types Modeling and manipulating data types View top rated classes
Group folder image PHP 7 Classes using PHP 7 specific features View top rated classes


  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  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

Download Download all files: tree-tools-php.tar.gz tree-tools-php.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
  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

Download Download all files: tree-tools-php.tar.gz tree-tools-php.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.