Zeta Components Manual :: Docs For Class ezcConsoleTable
ConsoleTools::ezcConsoleTable
Class ezcConsoleTable
Creating tables to be printed to the console.
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.
- // Initialize the console output handler
- // Define a new format "headline"
- $out->formats->headline->color = 'red';
- $out->formats->headline->style = array( 'bold' );
- // Define a new format "sum"
- $out->formats->sum->color = 'blue';
- $out->formats->sum->style = array( 'negative' );
- // Create a new table
- // Create first row and in it the first cell
- $table[0][0]->content = 'Headline 1';
- // Create 3 more cells in row 0
- for ( $i = 2; $i < 5; $i++ )
- {
- $table[0][]->content = "Headline $i";
- }
- $data = array( 1, 2, 3, 4 );
- // Create some more data in the table...
- foreach ( $data as $value )
- {
- // Create a new row each time and set it's contents to the actual value
- $table[][0]->content = $value;
- }
- // Set another border format for our headline row
- $table[0]->borderFormat = 'headline';
- // Set the content format for all cells of the 3rd row to "sum"
- $table[2]->format = 'sum';
Source for this file: /ConsoleTools/src/table.php
Implements interfaces:
- Countable (internal interface)
- Iterator (internal interface)
- ArrayAccess (internal interface)
Version: | //autogen// |
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
ezcConsoleTableOptions | read/write |
$options
Contains the options for this class. |
int | read/write |
$width
Width of the table. |
Member Variables
protected ezcConsoleOutput |
$outputHandler
The ezcConsoleOutput object to use. |
protected array(string=>mixed) |
$properties
Container to hold the properties |
protected array(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
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 |
Exceptions:
Type | Description |
---|---|
ezcBaseValueException |
On an invalid setting. |
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.
Implementation of:
Method | Description |
---|---|
Countable::count |
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).
Implementation of:
Method | Description |
---|---|
Iterator::current |
getOptions
Returns the current options.
Returns the options currently set for this table.
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
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).
Implementation of:
Method | Description |
---|---|
Iterator::key |
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).
Implementation of:
Method | Description |
---|---|
Iterator::next |
offsetExists
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. |
Exceptions:
Type | Description |
---|---|
ezcBaseValueException |
If a non numeric row ID is requested. |
Implementation of:
Method | Description |
---|---|
ArrayAccess::offsetExists |
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. |
Exceptions:
Type | Description |
---|---|
ezcBaseValueException |
If a non numeric row ID is requested. |
Implementation of:
Method | Description |
---|---|
ArrayAccess::offsetGet |
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. |
Exceptions:
Type | Description |
---|---|
ezcBaseValueException |
If the provided value is not of type ezcConsoleTableRow. |
ezcBaseValueException |
If a non numeric row ID is requested. |
Implementation of:
Method | Description |
---|---|
ArrayAccess::offsetSet |
offsetUnset
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. |
Exceptions:
Type | Description |
---|---|
ezcBaseValueException |
If a non numeric row ID is requested. |
Implementation of:
Method | Description |
---|---|
ArrayAccess::offsetUnset |
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).
Implementation of:
Method | Description |
---|---|
Iterator::rewind |
setOptions
Set new options.
This method allows you to change the options of the table.
Parameters:
Name | Type | Description |
---|---|---|
$options |
ezcConsoleTableOptions | The options to set. |
Exceptions:
Type | 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).
Implementation of:
Method | Description |
---|---|
Iterator::valid |
__toString
Returns the table in a string.