lukaszmakuch / table-renderer
Allows to render tree structures as tables (e.g. HTML).
Requires
- lukaszmakuch/class-based-registry: ^0.0.2
- lukaszmakuch/math: ^0.1.0
- lukaszmakuch/object-attribute-container: ^0.1.0
- lukaszmakuch/property-setter: ^0.2.0
Requires (Dev)
- phpdocumentor/phpdocumentor: 2.*
- phpunit/phpunit: 4.7.*
This package is not auto-updated.
Last update: 2025-01-08 20:37:56 UTC
README
Table renderer
Allows to render tree structures as tables.
HTML example
Click the image for more examples.
Trees
Elements
There are 3 main elements of every tree:
Atomic values
They are indivisible.
TextValue
It simply holds some text which will be rendered inside some cell.
use lukaszmakuch\TableRenderer\TextValue; $flowers = new TextValue("roses");
Containers
They hold other elements.
HorizontalContainer
Its elements are on top of each other.
use lukaszmakuch\TableRenderer\HorizontalContainer; use lukaszmakuch\TableRenderer\TextValue; $column = (new HorizontalContainer()) ->add(new TextValue("top")) ->add(new TextValue("bottom"));
HorizontalContainer
Its elements are next to each other.
use lukaszmakuch\TableRenderer\VerticalContainer; use lukaszmakuch\TableRenderer\TextValue; $row = (new VerticalContainer()) ->add(new TextValue("left")) ->add(new TextValue("right"));
Building trees
It's possible to build a composite of any complexity.
use lukaszmakuch\TableRenderer\HorizontalContainer; use lukaszmakuch\TableRenderer\VerticalContainer; use lukaszmakuch\TableRenderer\TextValue; $table = (new HorizontalContainer()) ->add((new VerticalContainer()) ->add(new TextValue("top left")) ->add(new TextValue("top middle")) ->add(new TextValue("top right")) ) ->add((new VerticalContainer()) ->add(new TextValue("bottom left")) ->add(new TextValue("bottom right")) );
Renderers
Allows to render tables based on tree structures.
HTMLRenderer
Renders HTML code.
Getting the renderer
use lukaszmakuch\TableRenderer\HTMLRenderer\HTMLRendererBuilder; $builder = new HTMLRendererBuilder(); $htmlRenderer = $builder->buildRenderer();
Basic usage
use lukaszmakuch\TableRenderer\VerticalContainer; use lukaszmakuch\TableRenderer\TextValue; use lukaszmakuch\TableRenderer\HTMLRenderer\HTMLRenderer; $tree = (new VerticalContainer()) ->add(new TextValue("left")) ->add(new TextValue("right")); /* @var $renderer HTMLRenderer */ echo $renderer->renderHTMLBasedOn($tree);
Adding HTML attributes
It's possible to assign HTML attributes to atomic values as well as to a whole table. ObjectAttributeContainer is used to achieve that.
Building renderer with support of additional attributes
First, you need to build the renderer with some attribute container.
use lukaszmakuch\TableRenderer\HTMLRenderer\HTMLRendererBuilder; use lukaszmakuch\ObjectAttributeContainer\Impl\ObjectAttributeContainerImpl; //source of attributes $attrs = new ObjectAttributeContainerImpl(); //building with the attribute container $builder = new HTMLRendererBuilder(); $builder->setAttributeContainer($attrs); $htmlRenderer = $builder->buildRenderer();
Adding attributes to a tree.
use lukaszmakuch\ObjectAttributeContainer\ObjectAttributeContainer; use lukaszmakuch\TableRenderer\VerticalContainer; use lukaszmakuch\TableRenderer\TextValue; lukaszmakuch\TableRenderer\HTMLRenderer\HTMLRenderer; /* @var $attrs ObjectAttributeContainer */ //table with border 1 $tree = $attrs->addObjAttrs( (new VerticalContainer()) //cell with no extra style ->add(new TextValue("left")) //cell with red text ->add($attrs->addObjAttrs( new TextValue("right"), ["attrs" => ["style" => "color: #f00"]] )), ["attrs" => ["border" => 1]] ); /* @var $renderer HTMLRenderer */ echo $renderer->renderHTMLBasedOn($tree);
Support of custom atomic values
This renderer supports custom atomic values. A custom value should extend AtomicCellValue and be supported by an instance of AtomicValueRenderer. Then you can register a new atomic value renderer like that:
use lukaszmakuch\TableRenderer\HTMLRenderer\HTMLRendererBuilder; /* @var @builder HTMLRendererBuilder */ $builder->addAtomicValueRenderer( NewAtomicType::class, new NewAtomicTypeRenderer() );
ScalarRenderer
It renders models of tables as scalar values or arrays of scalar values (or other arrays).
Getting the renderer
use lukaszmakuch\TableRenderer\ScalarRenderer\ScalarRendererBuilder; $renderer = (new ScalarRendererBuilder())->build();
Basic usage
use lukaszmakuch\TableRenderer\VerticalContainer; use lukaszmakuch\TableRenderer\TextValue; use lukaszmakuch\TableRenderer\ScalarRenderer\ScalarRenderer; $tree = (new VerticalContainer()) ->add(new TextValue("first")) ->add(new TextValue("second")); /* @var $renderer ScalarRenderer */ var_dump($renderer->getScalarRepresentationOf($tree)); //[ // 'type' => 'vertical-container', // 'value' => [ // ['type' => 'text', 'value' => 'first'], // ['type' => 'text', 'value' => 'second'] // ] //]
Support of custom elements
This renderer supports custom elements. A custom element should extend AtomicCellValue and be supported by an instance of ScalarRenderer. Then you can register a new element renderer like that:
use lukaszmakuch\TableRenderer\ScalarRenderer\ScalarRendererBuilder; /* @var @builder ScalarRendererBuilder */ $builder->addRenderer( NewAtomicType::class, new NewAtomicTypeRenderer() );
Examples
Check examples in the examples directory
Installation
Use composer to get the latest version:
$ composer require lukaszmakuch/table-renderer