TableHelper
extends Helper
in package
Create a visually pleasing ASCII art table from 2 dimensional array data.
Table of Contents
Properties
- $_config : array<string, mixed>
- Runtime config
- $_configInitialized : bool
- Whether the config property has already been configured with defaults
- $_defaultConfig : array<string, mixed>
- Default config for this helper.
- $_io : ConsoleIo
- ConsoleIo instance.
Methods
- __construct() : mixed
- Constructor.
- configShallow() : $this
- Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
- getConfig() : mixed
- Returns the config.
- getConfigOrFail() : mixed
- Returns the config for this specific key.
- output() : void
- Output a table.
- setConfig() : $this
- Sets the config.
- _addStyle() : string
- Add style tags
- _calculateWidths() : array<string|int, int>
- Calculate the column widths
- _cellWidth() : int
- Get the width of a cell exclusive of style tags.
- _configDelete() : void
- Deletes a single config key.
- _configRead() : mixed
- Reads a config key.
- _configWrite() : void
- Writes a config key.
- _render() : void
- Output a row.
- _rowSeparator() : void
- Output a row separator.
Properties
$_config
Runtime config
protected
array<string, mixed>
$_config
= []
$_configInitialized
Whether the config property has already been configured with defaults
protected
bool
$_configInitialized
= false
$_defaultConfig
Default config for this helper.
protected
array<string, mixed>
$_defaultConfig
= ['headers' => true, 'rowSeparator' => false, 'headerStyle' => 'info']
$_io
ConsoleIo instance.
protected
ConsoleIo
$_io
Methods
__construct()
Constructor.
public
__construct(ConsoleIo $io[, array<string, mixed> $config = [] ]) : mixed
Parameters
- $io : ConsoleIo
-
The ConsoleIo instance to use.
- $config : array<string, mixed> = []
-
The settings for this helper.
configShallow()
Merge provided config with existing config. Unlike `config()` which does a recursive merge for nested keys, this method does a simple merge.
public
configShallow(array<string, mixed>|string $key[, mixed|null $value = null ]) : $this
Setting a specific value:
$this->configShallow('key', $value);
Setting a nested value:
$this->configShallow('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->configShallow(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
Return values
$thisgetConfig()
Returns the config.
public
getConfig([string|null $key = null ][, mixed $default = null ]) : mixed
Usage
Reading the whole config:
$this->getConfig();
Reading a specific value:
$this->getConfig('key');
Reading a nested value:
$this->getConfig('some.nested.key');
Reading with default value:
$this->getConfig('some-key', 'default-value');
Parameters
- $key : string|null = null
-
The key to get or null for the whole config.
- $default : mixed = null
-
The return value when the key does not exist.
Return values
mixed —Configuration data at the named key or null if the key does not exist.
getConfigOrFail()
Returns the config for this specific key.
public
getConfigOrFail(string $key) : mixed
The config value for this key must exist, it can never be null.
Parameters
- $key : string
-
The key to get.
Tags
Return values
mixed —Configuration data at the named key
output()
Output a table.
public
output(array<string|int, mixed> $args) : void
Data will be output based on the order of the values in the array. The keys will not be used to align data.
Parameters
- $args : array<string|int, mixed>
-
The data to render out.
setConfig()
Sets the config.
public
setConfig(array<string, mixed>|string $key[, mixed|null $value = null ][, bool $merge = true ]) : $this
Usage
Setting a specific value:
$this->setConfig('key', $value);
Setting a nested value:
$this->setConfig('some.nested.key', $value);
Updating multiple config settings at the same time:
$this->setConfig(['one' => 'value', 'another' => 'value']);
Parameters
- $key : array<string, mixed>|string
-
The key to set, or a complete array of configs.
- $value : mixed|null = null
-
The value to set.
- $merge : bool = true
-
Whether to recursively merge or overwrite existing config, defaults to true.
Tags
Return values
$this_addStyle()
Add style tags
protected
_addStyle(string $text, string $style) : string
Parameters
- $text : string
-
The text to be surrounded
- $style : string
-
The style to be applied
Return values
string_calculateWidths()
Calculate the column widths
protected
_calculateWidths(array<string|int, mixed> $rows) : array<string|int, int>
Parameters
- $rows : array<string|int, mixed>
-
The rows on which the columns width will be calculated on.
Return values
array<string|int, int>_cellWidth()
Get the width of a cell exclusive of style tags.
protected
_cellWidth(string $text) : int
Parameters
- $text : string
-
The text to calculate a width for.
Return values
int —The width of the textual content in visible characters.
_configDelete()
Deletes a single config key.
protected
_configDelete(string $key) : void
Parameters
- $key : string
-
Key to delete.
Tags
_configRead()
Reads a config key.
protected
_configRead(string|null $key) : mixed
Parameters
- $key : string|null
-
Key to read.
_configWrite()
Writes a config key.
protected
_configWrite(array<string, mixed>|string $key, mixed $value[, string|bool $merge = false ]) : void
Parameters
- $key : array<string, mixed>|string
-
Key to write to.
- $value : mixed
-
Value to write.
- $merge : string|bool = false
-
True to merge recursively, 'shallow' for simple merge, false to overwrite, defaults to false.
Tags
_render()
Output a row.
protected
_render(array<string|int, mixed> $row, array<string|int, int> $widths[, array<string, mixed> $options = [] ]) : void
Parameters
- $row : array<string|int, mixed>
-
The row to output.
- $widths : array<string|int, int>
-
The widths of each column to output.
- $options : array<string, mixed> = []
-
Options to be passed.
_rowSeparator()
Output a row separator.
protected
_rowSeparator(array<string|int, int> $widths) : void
Parameters
- $widths : array<string|int, int>
-
The widths of each column to output.