PHP Classes

neoshiftlab PHP Tree Data Structure: Create and manipulate trees of data values

Recommend this page to a friend!

  Author Author  
Picture of William
Name: William <contact>
Classes: 1 package by
Country: France France


  Detailed description   Download Download .zip .tar.gz  
This package can create and manipulate trees of data values.

It can create a tree of data value nodes by passing an array to the class constructor function.

The package can also perform several types of operations with tree data notes like:

- Setting each node primary and parent keys to finding nodes by key

- Return a data structure that is suitable to serialize into a JSON string

- Use objects as tree nodes

Details

<p align="center"> <img src="/art/banner.png" alt="Tree Logo"> </p>

About Tree

Run tests Latest Stable Version

This package provides a tree structure.

Installation

This package requires php:^8.0. You can install it via composer:

composer require neoshiftlab/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 Neoshiftlab\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->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 Neoshiftlab\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 Neoshiftlab\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 passe object as items with expected id and parentId public property like this :

use Tests\Artifact\Person;
use Neoshiftlab\Tree\Tree;

$items = [
    new Person(1, null, 'Chicken'),
    new Person(2, 1, 'Egg'),
];

$tree = new Tree($items);

$chicken = $tree->getNodeById(1);
$chicken->getChildren(); // Return Egg's node

  Classes of William  >  neoshiftlab PHP Tree Data Structure  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: neoshiftlab PHP Tree Data Structure
Base name: neoshiftlab-tree
Description: Create and manipulate trees of data values
Version: -
PHP version: 8
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 Data types Modeling and manipulating data types View top rated classes
Group folder image PHP 8 Classes using PHP 8 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 imageart (1 file)
Files folder imagesrc (2 files)
Files folder imagetests (2 files, 1 directory)
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file phpunit.xml 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 run_tests.yml Data Auxiliary data

  Files folder image Files  /  art  
File Role Description
  Accessible without login Image file banner.png Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
  Plain text file Node.php Class Class source
  Plain text file Tree.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageArtifact (1 file)
  Plain text file NodeTest.php Class Class source
  Plain text file TreeTest.php Class Class source

  Files folder image Files  /  tests  /  Artifact  
File Role Description
  Plain text file Person.php Class Class source

Download Download all files: neoshiftlab-tree.tar.gz neoshiftlab-tree.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.
For more information send a message to info at phpclasses dot org.