2017 © Pedro Pelรกez
 

library tree-helper

A symfony console helper to print a tree

image

pbergman/tree-helper

A symfony console helper to print a tree

  • Thursday, March 15, 2018
  • by pbergman
  • Repository
  • 1 Watchers
  • 13 Stars
  • 69,649 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 6 Forks
  • 1 Open issues
  • 3 Versions
  • 21 % Grown

The README.md

Tree builder helper

this is a small library for symfony console to print a tree in a fluent and flexible way., (*1)

The output is similar as the linux tree command, an can be used to debug/output relational data., (*2)

Usage:

$tree = new TreeHelper($output);

$tree
    ->newNode('first')
        ->addValue('fist value')
        ->addValue('second value')
        ->newNode('fist sub')
            ->addValue('fist sub value')
            ->addValue('second sub value')
            ->newNode('fist sub sub')
                ->addValue('fist sub sub value')
                ->addValue('second sub sub value')            
            ->end()
        ->end()
    ->newNode('second')
        ->addValue('second value')
        ->addValue('second second value')
    ->end();

$tree->printTree();    

or, (*3)

$tree = new TreeHelper($output);
$tree->addArray([
    'first' => [
        0 => 'fist value',
        1 => 'second value',
        'fist sub' => [
            0 => 'fist sub value',
            1 => 'second sub value',
            'fist sub sub' => [
                'fist sub sub value',
                'second sub sub value',
            ],
        ],
    ],
    'second' => [
        'second value',
        'second second value',
    ]
]);

that will output:, (*4)

.
โ”‚
โ””โ”€โ”€ first
    โ”œโ”€โ”€ fist value
    โ”œโ”€โ”€ second value
    โ”œโ”€โ”€ fist sub
    โ”‚   โ”œโ”€โ”€ fist sub value
    โ”‚   โ”œโ”€โ”€ second sub value
    โ”‚   โ””โ”€โ”€ fist sub sub
    โ”‚       โ”œโ”€โ”€ fist sub sub value
    โ”‚       โ””โ”€โ”€ second sub sub value
    โ””โ”€โ”€ second
        โ”œโ”€โ”€ second value
        โ””โ”€โ”€ second second value

for other example see the the tests., (*5)

Methods

newNode($name)

Create an new node and bind it to the parent, (*6)

addNode($node)

Append node to tree, (*7)

end()

Will return the parent object (if set else null), (*8)

getRoot()

Will return the root node, (*9)

printTree($output)

Print the tree to output, (*10)

findNode($name)

Search a node by title, (*11)

toArray()

Returns a array representation of the data, (*12)

getTitle()

Returns the title of the node, (*13)

setTitle($title)

Sets the title of the node, (*14)

addValue($value)

Add a value to the node, this can be a valid scalar or a object with method __toString, (*15)

addArray($array)

Add a nested array to node, (*16)

setValues($array)

Add a flat array to node, this be set as the values of the node, (*17)

setParent($parent)

Set the parent of the node, will trow a RuntimeException is the the node all ready is linked to the given node, so we don`t get infinite loops., (*18)

getNodes()

Get all the (child) nodes defined in the node, all the nodes are only saved in the root so to get all nodes you probably have to do this $t->getRoot()->getNodes(); else it will return the nodes where the parent is set as node where from you calling the method., (*19)

setMaxDepth($depth)

Set the max depth of the node values, this can be done globally (on root) or on a specific node., (*20)

getMaxDepth()

Return the max depth that is set, (*21)

setFormats()

Set the form that is used for example:, (*22)

$tree->setFormats([
    TreeHelper::LINE_PREFIX_EMPTY => '  ',
    TreeHelper::LINE_PREFIX => 'โ”‚ ',
    TreeHelper::TEXT_PREFIX => 'โ”œ ',
    TreeHelper::TEXT_PREFIX_END => 'โ”” ',
]);

no it will use a 2 space indent instead of 4, see tests, (*23)

setFormat($id, $format)

Overwrite a specific format, (*24)

The Versions

15/03 2018

dev-master

9999999-dev

A symfony console helper to print a tree

  Sources   Download

MIT

The Requires

 

by Philip Bergman

console array symfony tree builder

15/03 2018

1.0.1

1.0.1.0

A symfony console helper to print a tree

  Sources   Download

MIT

The Requires

 

by Philip Bergman

console array symfony tree builder

26/03 2015

1.0

1.0.0.0

A symfony console helper to print a tree

  Sources   Download

MIT

The Requires

 

by Philip Bergman

console array symfony tree builder