2017 © Pedro Peláez
 

library table-renderer

Allows to render tree structures as tables (e.g. HTML).

image

lukaszmakuch/table-renderer

Allows to render tree structures as tables (e.g. HTML).

  • Sunday, April 24, 2016
  • by lukaszmakuch
  • Repository
  • 3 Watchers
  • 2 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

travis, (*1)

Table renderer

Allows to render tree structures as tables., (*2)

HTML example

alt text Click the image for more examples., (*3)

Trees

Elements

There are 3 main elements of every tree:, (*4)

Atomic values

They are indivisible., (*5)

TextValue

It simply holds some text which will be rendered inside some cell., (*6)

use lukaszmakuch\TableRenderer\TextValue;

$flowers = new TextValue("roses");

Containers

They hold other elements., (*7)

HorizontalContainer

Its elements are on top of each other., (*8)

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., (*9)

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., (*10)

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., (*11)

HTMLRenderer

Renders HTML code., (*12)

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., (*13)

Building renderer with support of additional attributes

First, you need to build the renderer with some attribute container., (*14)

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:, (*15)

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)., (*16)

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:, (*17)

use lukaszmakuch\TableRenderer\ScalarRenderer\ScalarRendererBuilder;

/* @var @builder ScalarRendererBuilder */
$builder->addRenderer(
    NewAtomicType::class,
    new NewAtomicTypeRenderer()
);

Examples

Check examples in the examples directory, (*18)

Installation

Use composer to get the latest version:, (*19)

$ composer require lukaszmakuch/table-renderer

The Versions

13/03 2016

v0.1

0.1.0.0

Allows to render tree structures as tables (e.g. HTML).

  Sources   Download

MIT

The Requires

 

The Development Requires