Library
Class

Library\Tool\Table

class Table

Table helper tool

Presentation

This class helps to build and work with table in an HTML style, meaning that the table may have a caption, and three parts separated in a header, a footer and a body composed of lines separated in cells. The class does NOT build any HTML string but organizes and completes each table part to have a cleaned and ready-to-work-with array representation of the table.

A full text schema like a Mysql query result is embedded using method render().

Construction of the table

The table built in this class can be schematized like below ; it is considered as a set of cells stored in lines that builds columns.

+------------+------------+
| table      | headers    | // "thead" : can be more than 1 line
+------------+------------+
| table      | line 1     | // "tbody"
| table      | line 2     |
+------------+------------+
| table      | footers    | // "tfoot" : can be more than 1 line
+------------+------------+

This schema is render as a matrix like, concerning only the body:

       | col A   | col B   |
       ---------------------
line 1 | cell A1 | cell B1 |
line 2 | cell A2 | cell B2 |

Lines, columns and cells are 1 based arrays: first item has key "1" when you want to get or set a position.

Usage

For convenience, the best practice is to use an alias:

use Library\Tool\Table as TableTool;

Constants

PAD_BY_SPAN

Using this flag as $pad_flag, last cell is pad as an HTML colspan for each line if necessary

PAD_BY_EMPTY_CELLS

Using this flag as $pad_flag, each line is completed by empty cells if necessary

ITERATE_ON_LINES

Using this flag, the getTableIterator method returns an iterator on table lines

ITERATE_ON_COLUMNS

Using this flag, the getTableIterator method returns an iterator on table columns

Properties

static string $default_foot_mask

Methods

__construct(array $body = array(), array $header = array(), array $footer = array(), string $title = null, int $pad_flag = self::PAD_BY_EMPTY_CELLS)

Table construction

string __toString()

Rendering of the table

array getTable()

Get the full table array

object getTableIterator(string $part = 'body', int $iterator_flag = self::ITERATE_ON_LINES)

Get the full table or a part of the table as an ArrayIterator object

array|null getLine(int $line_index = null)

Get a line of the table body

Table addLine(array|string $contents = null, mixed $default = null)

Add a new line in the table body

array|null getColumn(int $column_index = null)

Get a column of the table body

Table addColumn(array|string $body = null, mixed $default = null, array $headers = null, array $footers = null)

Add a new column in the table body

string|null getCell(int $line_index = null, int $cell_index = null)

Get a cell of the table body

Table addCell(string $cell = null)

Add a new cell in the current line of the table body

Table setPadFlag(int $flag)

Set the table flag used for cell padding

int getPadFlag()

Get the table pad flag

Table setTitle(string $title)

Set the table title

string getTitle()

Get the table title

Table setHeader(array $contents)

Set the table headers lines

Table setHeaderLine(array $contents, int $line_index = null, mixed $default = null)

Set a table header line

Table addHeaderLine(array $contents, int $line_index = null, mixed $default = null)

Add a new table header line

Table setHeaderColumn(array $contents = array(), int $column_index = null, mixed $default = null)

Set a column in the table header

Table addHeaderColumn(array $contents = array(), int $column_index = null, mixed $default = null)

Add a new column in the table header

Table setHeaderCell(array $content, int $line_index = null, int $cell_index = null)

Set a table header cell

Table addHeaderCell(array $content, int $line_index = null, int $cell_index = null)

Add a new table header cell

array getHeader()

Get the table headers lines

array getHeaderLine(int $line_index = null)

Get a table headers line

array getHeaderColumn(int $column_index = null)

Get a table headers column

array getHeaderCell(int $line_index = null, int $cell_index = null)

Get a table headers cell

Table setBody(array $contents)

Set the table body lines

Table setBodyLine(array $contents, int $line_index = null)

Set a table body line

Table addBodyLine(array $contents, int $line_index = null)

Add a new table body line

Table setBodyColumn(array $contents = array(), int $column_index = null, mixed $default = null)

Set a column in the table body

Table addBodyColumn(array $contents = array(), int $column_index = null, mixed $default = null)

Add a new column in the table body

Table setBodyCell(array $content, int $line_index = null, int $cell_index = null)

Set a table body cell

Table addBodyCell(array $content, int $line_index = null, int $cell_index = null)

Add a new table body cell

array getBody()

Get the table body lines

array getBodyLine(int $line_index = null)

Get a table body line

array|null getBodyColumn(int $column_index = null)

Get a column of the table body

array getBodyCell(int $line_index = null, int $cell_index = null)

Get a table body cell

Table setFooter(array $contents)

Set the table footer lines

Table setFooterLine(array $contents, int $line_index = null)

Set a table footer line

Table addFooterLine(array $contents, int $line_index = null)

Add a new table footer line

Table setFooterColumn(array $contents = array(), int $column_index = null, mixed $default = null)

Set a column in the table footers

Table addFooterColumn(array $contents = array(), int $column_index = null, mixed $default = null)

Add a new column in the table footers

Table setFooterCell(array $content, int $line_index = null, int $cell_index = null)

Set a table footer cell

Table addFooterCell(array $content, int $line_index = null, int $cell_index = null)

Add a new table footer cell

array getFooter()

Get the table footers lines

array getFooterLine(int $line_index = null)

Get a table footers line

array getFooterColumn(int $column_index = null)

Get a table footers column

array getFooterCell(int $line_index = null, int $cell_index = null)

Get a table footers cell

int getTableColumnSize()

Get the table columns size

int getTableLineSize()

Get the table lines size

int getTableCellSize()

Get the table cells size

string getSizesInfos()

Get a string information presenting an overview of the table

void _resetSizes()

Reset the table sizes

string render(int $str_pad_flag = STR_PAD_RIGHT)

Plain text rendering of the table

setContents()

setContentLine()

addContentLine()

setContentColumn()

setContentCol()

setBodyCol()

addContentColumn()

addContentCol()

addBodyCol()

setContentCell()

addContentCell()

getContents()

getContentLine()

getContentColumn()

getContentCol()

getBodyCol()

getContentCell()

getTableColSize()

getCol()

getHeaderCol()

addHeaderCol()

setHeaderCol()

getFooterCol()

addFooterCol()

setFooterCol()

getColumnSize()

getColSize()

getLineSize()

getCellSize()

Details

at line 183
public __construct(array $body = array(), array $header = array(), array $footer = array(), string $title = null, int $pad_flag = self::PAD_BY_EMPTY_CELLS)

Table construction

Parameters

array $body The array of body lines
array $header The array of headers lines
array $footer The array of footers lines
string $title The table title
int $pad_flag The flag to use for cell padding, must be one of the class PAD_ constants

See also

self::setPadFlag()
self::setTitle()
self::setBody()
self::setHeader()
self::setFooter()

at line 209
public string __toString()

Rendering of the table

Return Value

string The result of $this->render()

See also

self::render()

at line 223
public array getTable()

Get the full table array

Return Value

array

at line 249
public object getTableIterator(string $part = 'body', int $iterator_flag = self::ITERATE_ON_LINES)

Get the full table or a part of the table as an ArrayIterator object

With a null first parameter (default is body), the iterator will be constructed on all the table lines or columns, first the headers one, then the body and finally the footers one (without distinction).

Parameters

string $part One of the parts of the table array built by method getTable()
int $iterator_flag The flag to use to build the iterator, must be one of the class ITERATE_ constants

Return Value

object An ArrayIterator instance

Exceptions

InvalidArgumentException if the part doesn't exist in the table

See also

\ArrayIterator

at line 307
public array|null getLine(int $line_index = null)

Get a line of the table body

Parameters

int $line_index The index of the line to get, if null, the last line is returned

Return Value

array|null

at line 319
public Table addLine(array|string $contents = null, mixed $default = null)

Add a new line in the table body

Parameters

array|string $contents The content of the line
mixed $default The default value for empty cells

Return Value

Table

at line 331
public array|null getColumn(int $column_index = null)

Get a column of the table body

Parameters

int $column_index The index of the column to get, if null, the last column is returned

Return Value

array|null

at line 345
public Table addColumn(array|string $body = null, mixed $default = null, array $headers = null, array $footers = null)

Add a new column in the table body

Parameters

array|string $body The array of body lines
mixed $default The default value for empty cells, if null, the class will use its pad_flag
array $headers The array of headers lines
array $footers The array of footers lines

Return Value

Table

at line 364
public string|null getCell(int $line_index = null, int $cell_index = null)

Get a cell of the table body

Parameters

int $line_index The index of the line to get, if null, last line is chosen
int $cell_index The index of the cell to get in the line, if null, last cell is returned

Return Value

string|null

at line 375
public Table addCell(string $cell = null)

Add a new cell in the current line of the table body

Parameters

string $cell The content of the cell

Return Value

Table

at line 391
public Table setPadFlag(int $flag)

Set the table flag used for cell padding

Parameters

int $flag The flag to use for cell padding, must be one of the class PAD_ constants

Return Value

Table

at line 403
public int getPadFlag()

Get the table pad flag

Return Value

int

at line 414
public Table setTitle(string $title)

Set the table title

Parameters

string $title The table title

Return Value

Table

at line 425
public string getTitle()

Get the table title

Return Value

string

at line 440
public Table setHeader(array $contents)

Set the table headers lines

The parameter may be an array of array where each item is a line, or a simple array that will be considered as 1 line.

Parameters

array $contents The array of headers lines

Return Value

Table

See also

self::_setPart()

at line 456
public Table setHeaderLine(array $contents, int $line_index = null, mixed $default = null)

Set a table header line

Parameters

array $contents The content of the line
int $line_index The index of the line to set, if null, the line will be added at the end of the headers
mixed $default The default value for empty cells

Return Value

Table

See also

self::_setPartLine()

at line 472
public Table addHeaderLine(array $contents, int $line_index = null, mixed $default = null)

Add a new table header line

Parameters

array $contents The content of the line
int $line_index The index of the line to set, if null, the line will be added at the end of the headers
mixed $default The default value for empty cells

Return Value

Table

See also

self::_setPartLine()

at line 488
public Table setHeaderColumn(array $contents = array(), int $column_index = null, mixed $default = null)

Set a column in the table header

Parameters

array $contents The content of the column
int $column_index The index of the column to set, if null, the column will be added at the end of each line
mixed $default The default value for empty cells, if null, the class will use its pad_flag

Return Value

Table

See also

self::_setPartColumn()

at line 504
public Table addHeaderColumn(array $contents = array(), int $column_index = null, mixed $default = null)

Add a new column in the table header

Parameters

array $contents The content of the column
int $column_index The index of the column to set, if null, the column will be added at the end of each line
mixed $default The default value for empty cells, if null, the class will use its pad_flag

Return Value

Table

See also

self::_setPartColumn()

at line 521
public Table setHeaderCell(array $content, int $line_index = null, int $cell_index = null)

Set a table header cell

Parameters

array $content The content of the cell
int $line_index The index of the line to set, if null, the line will be added at the headers'last line
int $cell_index The index of the cell to set in the line, if null, the cell will be added at the end of the line

Return Value

Table

See also

self::_setPartCell()

at line 538
public Table addHeaderCell(array $content, int $line_index = null, int $cell_index = null)

Add a new table header cell

Parameters

array $content The content of the cell
int $line_index The index of the line to set, if null, the line will be added at the headers'last line
int $cell_index The index of the cell to set in the line, if null, the cell will be added at the end of the line

Return Value

Table

See also

self::_setPartCell()

at line 550
public array getHeader()

Get the table headers lines

Return Value

array The lines array

See also

self::_getPart()

at line 562
public array getHeaderLine(int $line_index = null)

Get a table headers line

Parameters

int $line_index The index of the line to get, if null, the last line is returned

Return Value

array The corresponding line

See also

self::_getPartLine()

at line 574
public array getHeaderColumn(int $column_index = null)

Get a table headers column

Parameters

int $column_index The index of the column to get, if null, the last line is returned

Return Value

array The corresponding column

See also

self::_getPartColumn()

at line 587
public array getHeaderCell(int $line_index = null, int $cell_index = null)

Get a table headers cell

Parameters

int $line_index The index of the line to get, if null, the last line is returned
int $cell_index The index of the cell to get in the line, if null, the last cell is returned

Return Value

array The corresponding cell

See also

self::_getPartCell()

at line 602
public Table setBody(array $contents)

Set the table body lines

The parameter may be an array of array where each item is a line, or a simple array that will be considered as 1 line.

Parameters

array $contents The array of body lines

Return Value

Table

See also

self::_setPart()

at line 617
public Table setBodyLine(array $contents, int $line_index = null)

Set a table body line

Parameters

array $contents The content of the line
int $line_index The index of the line to set, if null, the line will be added at the end of the body

Return Value

Table

See also

self::_setPartLine()

at line 632
public Table addBodyLine(array $contents, int $line_index = null)

Add a new table body line

Parameters

array $contents The content of the line
int $line_index The index of the line to set, if null, the line will be added at the end of the body

Return Value

Table

See also

self::_setPartLine()

at line 648
public Table setBodyColumn(array $contents = array(), int $column_index = null, mixed $default = null)

Set a column in the table body

Parameters

array $contents The content of the column
int $column_index The index of the column to set, if null, the column will be added at the end of each line
mixed $default The default value for empty cells, if null, the class will use its pad_flag

Return Value

Table

See also

self::_setPartColumn()

at line 664
public Table addBodyColumn(array $contents = array(), int $column_index = null, mixed $default = null)

Add a new column in the table body

Parameters

array $contents The content of the column
int $column_index The index of the column to set, if null, the column will be added at the end of each line
mixed $default The default value for empty cells, if null, the class will use its pad_flag

Return Value

Table

See also

self::_setPartColumn()

at line 681
public Table setBodyCell(array $content, int $line_index = null, int $cell_index = null)

Set a table body cell

Parameters

array $content The content of the cell
int $line_index The index of the line to set, if null, the line will be added at the body's last line
int $cell_index The index of the cell to set in the line, if null, the cell will be added at the end of the line

Return Value

Table

See also

self::_setPartCell()

at line 698
public Table addBodyCell(array $content, int $line_index = null, int $cell_index = null)

Add a new table body cell

Parameters

array $content The content of the cell
int $line_index The index of the line to set, if null, the line will be added at the body's last line
int $cell_index The index of the cell to set in the line, if null, the cell will be added at the end of the line

Return Value

Table

See also

self::_setPartCell()

at line 710
public array getBody()

Get the table body lines

Return Value

array The lines array

See also

self::_getPart()

at line 722
public array getBodyLine(int $line_index = null)

Get a table body line

Parameters

int $line_index The index of the line to get, if null, the last line is returned

Return Value

array The corresponding line

See also

self::_getPartLine()

at line 734
public array|null getBodyColumn(int $column_index = null)

Get a column of the table body

Parameters

int $column_index The index of the column to get, if null, the last column is returned

Return Value

array|null

See also

self::_getPartColumn()

at line 747
public array getBodyCell(int $line_index = null, int $cell_index = null)

Get a table body cell

Parameters

int $line_index The index of the line to get, if null, the last line is returned
int $cell_index The index of the cell to get in the line, if null, the last cell is returned

Return Value

array The corresponding cell

See also

self::_getPartCell()

at line 762
public Table setFooter(array $contents)

Set the table footer lines

The parameter may be an array of array where each item is a line, or a simple array that will be considered as 1 line.

Parameters

array $contents The array of footer lines

Return Value

Table

See also

self::_setPart()

at line 777
public Table setFooterLine(array $contents, int $line_index = null)

Set a table footer line

Parameters

array $contents The content of the line
int $line_index The index of the line to set, if null, the line will be added at the end of the footers

Return Value

Table

See also

self::_setPartLine()

at line 792
public Table addFooterLine(array $contents, int $line_index = null)

Add a new table footer line

Parameters

array $contents The content of the line
int $line_index The index of the line to set, if null, the line will be added at the end of the footers

Return Value

Table

See also

self::_setPartLine()

at line 808
public Table setFooterColumn(array $contents = array(), int $column_index = null, mixed $default = null)

Set a column in the table footers

Parameters

array $contents The content of the column
int $column_index The index of the column to set, if null, the column will be added at the end of each line
mixed $default The default value for empty cells, if null, the class will use its pad_flag

Return Value

Table

See also

self::_setPartColumn()

at line 824
public Table addFooterColumn(array $contents = array(), int $column_index = null, mixed $default = null)

Add a new column in the table footers

Parameters

array $contents The content of the column
int $column_index The index of the column to set, if null, the column will be added at the end of each line
mixed $default The default value for empty cells, if null, the class will use its pad_flag

Return Value

Table

See also

self::_setPartColumn()

at line 841
public Table setFooterCell(array $content, int $line_index = null, int $cell_index = null)

Set a table footer cell

Parameters

array $content The content of the cell
int $line_index The index of the line to set, if null, the line will be added at the footers'last line
int $cell_index The index of the cell to set in the line, if null, the cell will be added at the end of the line

Return Value

Table

See also

self::_setPartCell()

at line 858
public Table addFooterCell(array $content, int $line_index = null, int $cell_index = null)

Add a new table footer cell

Parameters

array $content The content of the cell
int $line_index The index of the line to set, if null, the line will be added at the footers'last line
int $cell_index The index of the cell to set in the line, if null, the cell will be added at the end of the line

Return Value

Table

See also

self::_setPartCell()

at line 870
public array getFooter()

Get the table footers lines

Return Value

array The lines array

See also

self::_getPart()

at line 882
public array getFooterLine(int $line_index = null)

Get a table footers line

Parameters

int $line_index The index of the line to get, if null, the last line is returned

Return Value

array The corresponding line

See also

self::_getPartLine()

at line 894
public array getFooterColumn(int $column_index = null)

Get a table footers column

Parameters

int $column_index The index of the column to get, if null, the last line is returned

Return Value

array The corresponding column

See also

self::_getPartColumn()

at line 907
public array getFooterCell(int $line_index = null, int $cell_index = null)

Get a table footers cell

Parameters

int $line_index The index of the line to get, if null, the last line is returned
int $cell_index The index of the cell to get in the line, if null, the last cell is returned

Return Value

array The corresponding cell

See also

self::_getPartCell()

at line 917
public int getTableColumnSize()

Get the table columns size

Return Value

int

at line 930
public int getTableLineSize()

Get the table lines size

Return Value

int

at line 943
public int getTableCellSize()

Get the table cells size

Return Value

int

at line 956
public string getSizesInfos()

Get a string information presenting an overview of the table

Return Value

string

at line 1285
public void _resetSizes()

Reset the table sizes

Return Value

void

at line 1332
public string render(int $str_pad_flag = STR_PAD_RIGHT)

Plain text rendering of the table

Parameters

int $str_pad_flag One of the PHP internal str_pad() $pad_type

Return Value

string

See also

str_pad()

at line 1404
public setContents()

See also

self::setBody()

at line 1412
public setContentLine()

See also

self::setBodyLine()

at line 1420
public addContentLine()

See also

self::addBodyLine()

at line 1428
public setContentColumn()

See also

self::setBodyColumn()

at line 1436
public setContentCol()

See also

self::setBodyColumn()

at line 1444
public setBodyCol()

See also

self::setBodyColumn()

at line 1452
public addContentColumn()

See also

self::addBodyColumn()

at line 1460
public addContentCol()

See also

self::addBodyColumn()

at line 1468
public addBodyCol()

See also

self::addBodyColumn()

at line 1476
public setContentCell()

See also

self::setBodyCell()

at line 1484
public addContentCell()

See also

self::addBodyCell()

at line 1492
public getContents()

See also

self::getBody()

at line 1500
public getContentLine()

See also

self::getBodyLine()

at line 1508
public getContentColumn()

See also

self::getBodyColumn()

at line 1516
public getContentCol()

See also

self::getBodyColumn()

at line 1524
public getBodyCol()

See also

self::getBodyColumn()

at line 1532
public getContentCell()

See also

self::getBodyCell()

at line 1540
public getTableColSize()

See also

self::getTableColumnSize()

at line 1548
public getCol()

See also

self::getBodyColumn()

at line 1556
public getHeaderCol()

See also

self::getHeaderColumn()

at line 1564
public addHeaderCol()

See also

self::addHeaderColumn()

at line 1572
public setHeaderCol()

See also

self::setHeaderColumn()

at line 1580
public getFooterCol()

See also

self::getFooterColumn()

at line 1588
public addFooterCol()

See also

self::addFooterColumn()

at line 1596
public setFooterCol()

See also

self::setFooterColumn()

at line 1604
public getColumnSize()

See also

self::getTableColumnSize()

at line 1612
public getColSize()

See also

self::getTableColumnSize()

at line 1620
public getLineSize()

See also

self::getTableLineSize()

at line 1628
public getCellSize()

See also

self::getTableCellSize()