ConsoleTools: ezcConsoleTable
[ ]
[ ]
[ ]
[ ]
[ ]
Class: ezcConsoleTable
|
Creating tables to be printed to the console. [
source]
Implemented Interfaces
- Countable (internal interface)
- Iterator (internal interface)
- ArrayAccess (internal interface)
Every ezcConsoleTable object can be accessed as if it was a multidimensional, numerically indexed array. The first dimension represents the rows of the table, so $table[0] gives you access to the first row of the table, which is represented by a
ezcConsoleTableRow object. You can access its properties directly, using e.g. $table[0]->format. The second dimension gives you direct access to the cells of your table, like $table[0][0] accesses the first cell in the first row of your table. You can access its properties diretly here, too. This works like e.g. $table[0][0]->format. Table row and cell objects are created on the fly, when you access them for the first time. You can also create them as if you simply create new array elements. E.g. $table[] creates a new row in the table.
1. // Initialize the console output handler
2. $out = new ezcConsoleOutput();
3. // Define a new format "headline"
4. $out->formats->headline->color = 'red';
5. $out->formats->headline->style = array( 'bold' );
6. // Define a new format "sum"
7. $out->formats->sum->color = 'blue';
8. $out->formats->sum->style = array( 'negative' );
9.
10. // Create a new table
11. $table = new ezcConsoleTable( $out, 60 );
12.
13. // Create first row and in it the first cell
14. $table[0][0]->content = 'Headline 1';
15.
16. // Create 3 more cells in row 0
17. for ( $i = 2; $i < 5; $i++ )
18. {
19. $table[0][]->content = "Headline $i";
20. }
21.
22. $data = array( 1, 2, 3, 4 );
23.
24. // Create some more data in the table...
25. foreach ( $data as $value )
26. {
27. // Create a new row each time and set it's contents to the actual value
28. $table[][0]->content = $value;
29. }
30.
31. // Set another border format for our headline row
32. $table[0]->borderFormat = 'headline';
33.
34. // Set the content format for all cells of the 3rd row to "sum"
35. $table[2]->format = 'sum';
36.
37. $table->outputTable();
Constants
ALIGN_CENTER
= STR_PAD_BOTH
|
Align text in cells to the center. |
ALIGN_DEFAULT
= -1
|
Align text in the default direction. |
ALIGN_LEFT
= STR_PAD_RIGHT
|
Align text in cells to the right. |
ALIGN_RIGHT
= STR_PAD_LEFT
|
Align text in cells to the left. |
WIDTH_FIXED
= 1
|
The width given by settings must be used even if the data allows it smaller. |
WIDTH_MAX
= 2
|
The width given by settings is a maximum value, if data allows it, the table gets smaller. |
WRAP_AUTO
= 1
|
Automatically wrap text to fit into a column. |
WRAP_CUT
= 3
|
Text will be cut to fit into a column. |
WRAP_NONE
= 2
|
Do not wrap text. Columns will be extended to fit the largest text. ATTENTION: This is risky! |
Properties
Member Variables
|
protected ezcConsoleOutput |
$outputHandler
The ezcConsoleOutput object to use. |
|
protected array(string=>mixed) |
$properties
Container to hold the properties |
|
protected array(int=>ezcConsoleTableRow) |
$rows
Collection of the rows that are contained in the table. |
Method Summary
|
public ezcConsoleTable |
__construct(
$outHandler, $width, [$options = array()] )
Creates a new table. |
|
public int |
count(
)
Returns the number of cells in the row. |
|
public ezcConsoleTableCell |
current(
)
Returns the currently selected cell. |
|
public ezcConsoleTableOptions |
getOptions(
)
Returns the current options. |
|
public array |
getTable(
)
Returns the table in an array. |
|
public int |
key(
)
Returns the key of the currently selected cell. |
|
public mixed |
next(
)
Returns the next cell and selects it or false on the last cell. |
|
public bool |
offsetExists(
$offset )
Returns if the given offset exists. |
|
public ezcConsoleTableCell |
offsetGet(
$offset )
Returns the element with the given offset. |
|
public void |
offsetSet(
$offset, $value )
Set the element with the given offset. |
|
public void |
offsetUnset(
$offset )
Unset the element with the given offset. |
|
public void |
outputTable(
)
Output the table. |
|
public ezcConsoleTableCell |
rewind(
)
Selects the very first cell and returns it. |
|
public void |
setOptions(
[$options = array()] )
Set new options. |
|
public ezcConsoleTableCell |
valid(
)
Returns if the current cell is valid. |
|
public string |
__toString(
)
Returns the table in a string. |
Methods
__construct
ezcConsoleTable __construct(
ezcConsoleOutput
$outHandler, int
$width, [array
$options = array()] )
Creates a new table.
Parameters
| Name |
Type |
Description |
$outHandler |
ezcConsoleOutput |
Output handler to utilize |
$width |
int |
Overall width of the table (chars). |
$options |
array |
Options |
Throws
| Class | Description |
ezcBaseValueException |
On an invalid setting. |
See also:
ezcConsoleTable::$options.
count
int count(
)
Returns the number of cells in the row.
This method is part of the Countable interface to allow the usage of PHP's count() function to check how many cells this row has.
current
Returns the currently selected cell.
This method is part of the Iterator interface to allow access to the cells of this row by iterating over it like an array (e.g. using foreach).
getOptions
Returns the current options.
Returns the options currently set for this table.
getTable
array getTable(
)
Returns the table in an array.
Returns the entire table as an array of printable lines. Each element of the array represents a physical line of the drawn table, including all borders and stuff, so you can simply print the table using
1. echo implode( "\n" , $table->getTable() ):
which is basically what
ezcConsoleTable::outputTable() does.
key
int key(
)
Returns the key of the currently selected cell.
This method is part of the Iterator interface to allow access to the cells of this row by iterating over it like an array (e.g. using foreach).
next
mixed next(
)
Returns the next cell and selects it or false on the last cell.
This method is part of the Iterator interface to allow access to the cells of this row by iterating over it like an array (e.g. using foreach).
offsetExists
bool offsetExists(
int
$offset )
Returns if the given offset exists.
This method is part of the ArrayAccess interface to allow access to the data of this object as if it was an array.
Parameters
| Name |
Type |
Description |
$offset |
int |
The offset to check. |
Throws
| Class | Description |
ezcBaseValueException |
If a non numeric row ID is requested. |
offsetGet
Returns the element with the given offset.
This method is part of the ArrayAccess interface to allow access to the data of this object as if it was an array. In case of the ezcConsoleTable class this method always returns a valid row object since it creates them on the fly, if a given item does not exist.
Parameters
| Name |
Type |
Description |
$offset |
int |
The offset to check. |
Throws
| Class | Description |
ezcBaseValueException |
If a non numeric row ID is requested. |
offsetSet
Set the element with the given offset.
This method is part of the ArrayAccess interface to allow access to the data of this object as if it was an array.
Parameters
| Name |
Type |
Description |
$offset |
int |
The offset to assign an item to. |
$value |
ezcConsoleTableRow |
The row to assign. |
Throws
| Class | Description |
ezcBaseValueException |
If a non numeric row ID is requested. |
ezcBaseValueException |
If the provided value is not of type ezcConsoleTableRow. |
offsetUnset
void offsetUnset(
int
$offset )
Unset the element with the given offset.
This method is part of the ArrayAccess interface to allow access to the data of this object as if it was an array.
Parameters
| Name |
Type |
Description |
$offset |
int |
The offset to unset the value for. |
Throws
| Class | Description |
ezcBaseValueException |
If a non numeric row ID is requested. |
outputTable
void outputTable(
)
Output the table.
Prints the complete table to the console.
rewind
Selects the very first cell and returns it.
This method is part of the Iterator interface to allow access to the cells of this row by iterating over it like an array (e.g. using foreach).
setOptions
Set new options.
This method allows you to change the options of the table.
Parameters
Throws
| Class | Description |
ezcBaseSettingNotFoundException |
If you tried to set a non-existent option value. |
ezcBaseSettingValueException |
If the value is not valid for the desired option. |
ezcBaseValueException |
If you submit neither an array nor an instance of ezcConsoleTableOptions. |
valid
Returns if the current cell is valid.
This method is part of the Iterator interface to allow access to the cells of this row by iterating over it like an array (e.g. using foreach).
__toString
string __toString(
)
Returns the table in a string.
Last updated: Mon, 30 Mar 2009