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_BY_EMPTY_CELLS |
Using this flag as
|
ITERATE_ON_LINES |
Using this flag, the
|
ITERATE_ON_COLUMNS |
Using this flag, the
|
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 |
|
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
at line 209
public string
__toString()
Rendering of the table
at line 223
public array
getTable()
Get the full table 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).
at line 307
public array|null
getLine(int $line_index = null)
Get a line of the table body
at line 319
public Table
addLine(array|string $contents = null, mixed $default = null)
Add a new line in the table body
at line 331
public array|null
getColumn(int $column_index = null)
Get a column of the table body
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
at line 364
public string|null
getCell(int $line_index = null, int $cell_index = null)
Get a cell of the table body
at line 375
public Table
addCell(string $cell = null)
Add a new cell in the current line of the table body
at line 391
public Table
setPadFlag(int $flag)
Set the table flag used for cell padding
at line 403
public int
getPadFlag()
Get the table pad flag
at line 414
public Table
setTitle(string $title)
Set the table title
at line 425
public string
getTitle()
Get the table title
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.
at line 456
public Table
setHeaderLine(array $contents, int $line_index = null, mixed $default = null)
Set a table header line
at line 472
public Table
addHeaderLine(array $contents, int $line_index = null, mixed $default = null)
Add a new table header line
at line 488
public Table
setHeaderColumn(array $contents = array(), int $column_index = null, mixed $default = null)
Set a column in the table header
at line 504
public Table
addHeaderColumn(array $contents = array(), int $column_index = null, mixed $default = null)
Add a new column in the table header
at line 521
public Table
setHeaderCell(array $content, int $line_index = null, int $cell_index = null)
Set a table header cell
at line 538
public Table
addHeaderCell(array $content, int $line_index = null, int $cell_index = null)
Add a new table header cell
at line 550
public array
getHeader()
Get the table headers lines
at line 562
public array
getHeaderLine(int $line_index = null)
Get a table headers line
at line 574
public array
getHeaderColumn(int $column_index = null)
Get a table headers column
at line 587
public array
getHeaderCell(int $line_index = null, int $cell_index = null)
Get a table headers cell
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.
at line 617
public Table
setBodyLine(array $contents, int $line_index = null)
Set a table body line
at line 632
public Table
addBodyLine(array $contents, int $line_index = null)
Add a new table body line
at line 648
public Table
setBodyColumn(array $contents = array(), int $column_index = null, mixed $default = null)
Set a column in the table body
at line 664
public Table
addBodyColumn(array $contents = array(), int $column_index = null, mixed $default = null)
Add a new column in the table body
at line 681
public Table
setBodyCell(array $content, int $line_index = null, int $cell_index = null)
Set a table body cell
at line 698
public Table
addBodyCell(array $content, int $line_index = null, int $cell_index = null)
Add a new table body cell
at line 710
public array
getBody()
Get the table body lines
at line 722
public array
getBodyLine(int $line_index = null)
Get a table body line
at line 734
public array|null
getBodyColumn(int $column_index = null)
Get a column of the table body
at line 747
public array
getBodyCell(int $line_index = null, int $cell_index = null)
Get a table body cell
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.
at line 777
public Table
setFooterLine(array $contents, int $line_index = null)
Set a table footer line
at line 792
public Table
addFooterLine(array $contents, int $line_index = null)
Add a new table footer line
at line 808
public Table
setFooterColumn(array $contents = array(), int $column_index = null, mixed $default = null)
Set a column in the table footers
at line 824
public Table
addFooterColumn(array $contents = array(), int $column_index = null, mixed $default = null)
Add a new column in the table footers
at line 841
public Table
setFooterCell(array $content, int $line_index = null, int $cell_index = null)
Set a table footer cell
at line 858
public Table
addFooterCell(array $content, int $line_index = null, int $cell_index = null)
Add a new table footer cell
at line 870
public array
getFooter()
Get the table footers lines
at line 882
public array
getFooterLine(int $line_index = null)
Get a table footers line
at line 894
public array
getFooterColumn(int $column_index = null)
Get a table footers column
at line 907
public array
getFooterCell(int $line_index = null, int $cell_index = null)
Get a table footers cell
at line 917
public int
getTableColumnSize()
Get the table columns size
at line 930
public int
getTableLineSize()
Get the table lines size
at line 943
public int
getTableCellSize()
Get the table cells size
at line 956
public string
getSizesInfos()
Get a string information presenting an overview of the table
at line 1285
public void
_resetSizes()
Reset the table sizes
at line 1332
public string
render(int $str_pad_flag = STR_PAD_RIGHT)
Plain text rendering of the table
at line 1404
public
setContents()
at line 1412
public
setContentLine()
at line 1420
public
addContentLine()
at line 1428
public
setContentColumn()
at line 1436
public
setContentCol()
at line 1444
public
setBodyCol()
at line 1452
public
addContentColumn()
at line 1460
public
addContentCol()
at line 1468
public
addBodyCol()
at line 1476
public
setContentCell()
at line 1484
public
addContentCell()
at line 1492
public
getContents()
at line 1500
public
getContentLine()
at line 1508
public
getContentColumn()
at line 1516
public
getContentCol()
at line 1524
public
getBodyCol()
at line 1532
public
getContentCell()
at line 1540
public
getTableColSize()
at line 1548
public
getCol()
at line 1556
public
getHeaderCol()
at line 1564
public
addHeaderCol()
at line 1572
public
setHeaderCol()
at line 1580
public
getFooterCol()
at line 1588
public
addFooterCol()
at line 1596
public
setFooterCol()
at line 1604
public
getColumnSize()
at line 1612
public
getColSize()
at line 1620
public
getLineSize()
at line 1628
public
getCellSize()