PHP code example of tobento / service-treeable

1. Go to this page and download the library: Download tobento/service-treeable library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tobento / service-treeable example snippets


use Tobento\Service\Treeable\ArrayTree;

$items = [
    [
        'name' => 'cars',
    ],
    [
        'name' => 'VW',
        'parent' => 'cars',
    ],        
];

$items = (new ArrayTree($items, 'name', 'parent'))->create();

Array
(
    [cars] => Array
        (
            [name] => cars
            [parent] => 
            [level] => 0
            [parentItem] => 
            [children] => Array
                (
                    [VW] => Array
                        (
                            [name] => VW
                            [parent] => cars
                            [level] => 1
                            [parentItem] => Array
                                (
                                    [name] => cars
                                    [parent] => 
                                    [level] => 0
                                    [parentItem] => 
                                )

                        )

                )

        )

)

use Tobento\Service\Treeable\ArrayTree;

$tree = new ArrayTree($items, 'id', 'parent_id', 'level', 'children', 'parentItem');

use Tobento\Service\Treeable\ArrayTree;

// sort by array key.
$items = (new ArrayTree($items, 'name', 'parent'))->sort('name')->create();

// sort by callable.
$items = (new ArrayTree($items, 'name', 'parent'))
    ->sort(fn ($a, $b) => $a['name'] > $b['name'])
    ->create();

use Tobento\Service\Treeable\ArrayTree;

$items = (new ArrayTree($items, 'name', 'parent'))
    ->filter(fn($i) => $i['name'] === 'cars')
    ->create();

use Tobento\Service\Treeable\ArrayTree;

$items = (new ArrayTree($items, 'name', 'parent'))
    ->each(function($item, $level) {

        if ($level >= 1) {
            return null;
        }
        
        $parentItem = $item['parentItem'];

        return $item;
    })
    ->create();

use Tobento\Service\Treeable\ArrayTree;

$items = (new ArrayTree($items, 'name', 'parent'))
    ->parents('BMW', function($item) {
        $item['active'] = true;
        return $item;
    })
    ->create();

use Tobento\Service\Treeable\Tree;
use Tobento\Service\Treeable\Treeable;
use Tobento\Service\Treeable\TreeableAware;

/**
 * Item
 */
class Item implements Treeable
{
    use TreeableAware;
            
    /**
     * Create a new Item
     *
     * @param string
     * @param null|string
     */
    public function __construct(
        protected string $text,
        protected ?string $parent = null,
    ) {}
    
    /**
     * Get the tree id
     *
     * @return string|int
     */
    public function getTreeId(): string|int
    {
        return $this->text;
    }
    
    /**
     * Get the tree parent
     *
     * @return null|string|int
     */
    public function getTreeParent(): null|string|int
    {
        return $this->parent;
    }

    public function text(): string
    {
        return $this->text;
    }
}

$tree = new Tree([
    new Item('cars'),
    new Item('VW', 'cars'),
    new Item('BMW', 'cars'),
]);

$items = $tree->create();

use Tobento\Service\Treeable\Tree;

$tree = new Tree([
    new Item('cars'),
    new Item('VW', 'cars'),
    new Item('BMW', 'cars'),
]);

$tree->sort(fn ($a, $b) => $a->text() <=> $b->text());

$items = $tree->create();

use Tobento\Service\Treeable\Tree;

$tree = new Tree([
    new Item('cars'),
    new Item('VW', 'cars'),
    new Item('BMW', 'cars'),
]);

$tree->filter(fn($i) => $i->text() === 'cars');

$items = $tree->create();

use Tobento\Service\Treeable\Tree;

$tree = new Tree([
    new Item('cars'),
    new Item('VW', 'cars'),
    new Item('BMW', 'cars'),
]);

$tree->each(function($item, $level) {
    if ($level >= 1) {
        return null;
    }
    
    //$item->getTreeLevel();
    
    return $item;
});

$items = $tree->create();

use Tobento\Service\Treeable\Tree;

$tree = new Tree([
    new Item('cars'),
    new Item('VW', 'cars'),
    new Item('BMW', 'cars'),
]);

$tree->parents('VW', function($item) {
    $item->active(true);
    return $item;
});

$items = $tree->create();

use Tobento\Service\Treeable\Traverser;
use Tobento\Service\Treeable\ArrayTree;

$items = [        
    [
        'name' => 'cars',
    ],
    [
        'name' => 'VW',
        'parent' => 'cars',
    ],
    [
        'name' => 'BMW',
        'parent' => 'cars',
    ],    
];

$tree = new ArrayTree($items, 'name', 'parent');

// change the children key if needed.
$html = (new Traverser($tree->create(), 'children'))
    ->before(function($level) {
        return '<ul>';
    })
    ->item(function($item, $childrenHtml, $level) {
        
        if (!empty($childrenHtml)) {
            return '<li>'.$item['name'].$childrenHtml.'</li>';
        }
        
        return '<li>'.$item['name'].'</li>';
    })
    ->after(function($level) {
        return '</ul>';
    })
    ->render();

use Tobento\Service\Treeable\Traverser;
use Tobento\Service\Treeable\Tree;

$tree = new Tree([
    new Item('cars'),
    new Item('VW', 'cars'),
    new Item('BMW', 'cars'),
]);

$html = (new Traverser($tree->create()))
    ->before(function($level) {
        return '<ul>';
    })
    ->item(function($item, $childrenHtml, $level) {
        
        if (!empty($childrenHtml)) {
            return '<li>'.$item->text().$childrenHtml.'</li>';
        }
        
        return '<li>'.$item->text().'</li>';
    })
    ->after(function($level) {
        return '</ul>';
    })
    ->render();