Skip to content

Commit

Permalink
Add formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
viossat committed Jul 7, 2015
1 parent e51b7e3 commit ca82eb4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 27 deletions.
29 changes: 21 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,35 @@ $data = [
'firstname' => 'Mollie',
'surname' => 'Alvarez',
'email' => 'molliealvarez@example.com',
],
],
[
'firstname' => 'Dianna',
'surname' => 'Mcbride',
'age' => 43,
'email' => 'diannamcbride@example.com',
],
],
[
'firstname' => 'Elvira',
'surname' => 'Mueller',
'age' => 50,
'email' => 'elviramueller@example.com',
],
],
[
'firstname' => 'Corine',
'surname' => 'Morton',
'age' => 35,
],
],
[
'firstname' => 'James',
'surname' => 'Allison',
],
],
[
'firstname' => 'Bowen',
'surname' => 'Kelley',
'age' => 50,
'email' => 'bowenkelley@example.com',
],
];
],
];

$renderer = new ArrayToTextTable($data);
echo $renderer->getTable();
Expand Down Expand Up @@ -91,8 +91,21 @@ $renderer->setKeysAlignment(ArrayToTextTable::AlignCenter);

$renderer->setValuesAlignment(ArrayToTextTable::AlignLeft);
// AlignLeft (default), AlignCenter, AlignRight

$formatter = function(&$value, $key, $renderer) {
if ($value === true)
$value = 'TRUE';
else if ($value === false)
$value = 'FALSE';
else if ($value === '')
$value = 'EMPTY';
else if ($value === null)
$value = 'NULL';
};
$renderer->setFormatter($formatter);
// default: null
```

## License MIT
## License

This library is published under [The MIT License](http://opensource.org/licenses/MIT).
62 changes: 43 additions & 19 deletions src/ArrayToTextTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,35 @@

namespace MathieuViossat\Util;

use \Zend\Text\Table\Decorator\Unicode;

class ArrayToTextTable {

const AlignLeft = STR_PAD_RIGHT;
const AlignCenter = STR_PAD_BOTH;
const AlignRight = STR_PAD_LEFT;

protected $table;
protected $data;
protected $keys;
protected $widths;
protected $decorator;
protected $indentation;
protected $upperKeys;
protected $keysAlignment;
protected $valuesAlignment;
protected $formatter;

public function __construct($table) {
$this->table = $table;

$this->keys = [];
foreach ($this->table as $row)
$this->keys = array_merge($this->keys, array_keys($row));
$this->keys = array_unique($this->keys);

foreach ($this->keys as $key)
$this->setWidth($key, $key);

foreach ($this->table as $row)
foreach ($row as $columnKey => $columnValue)
$this->setWidth($columnKey, $columnValue);
public function __construct($data) {
$this->data = $data;

$this->setDecorator(new \Zend\Text\Table\Decorator\Unicode())
->setIndentation('')
->setUpperKeys(true)
->setKeysAlignment(self::AlignCenter)
->setValuesAlignment(self::AlignLeft);
->setValuesAlignment(self::AlignLeft)
->setFormatter(null);
}

public function getTable() {
$data = $this->prepare();
$i = $this->indentation;
$d = $this->decorator;

Expand All @@ -65,7 +54,7 @@ public function getTable() {

$table .= $i . $this->line($d->getVerticalRight(), $d->getHorizontal(), $d->getCross(), $d->getVerticalLeft()) . PHP_EOL;

foreach ($this->table as $row)
foreach ($data as $row)
$table .= $i . $this->row($row, $this->valuesAlignment) . PHP_EOL;

$table .= $i . $this->line($d->getBottomLeft(), $d->getHorizontal(), $d->getHorizontalUp(), $d->getBottomRight()) . PHP_EOL;
Expand Down Expand Up @@ -93,6 +82,10 @@ public function getValuesAlignment() {
return $this->valuesAlignment;
}

public function getFormatter() {
return $this->formatter;
}

public function setDecorator($decorator) {
$this->decorator = $decorator;
return $this;
Expand All @@ -118,6 +111,11 @@ public function setValuesAlignment($valuesAlignment) {
return $this;
}

public function setFormatter($formatter) {
$this->formatter = $formatter;
return $this;
}

protected function line($left, $horizontal, $link, $right) {
$line = $left;
foreach ($this->keys as $key)
Expand All @@ -134,6 +132,32 @@ protected function row($row, $alignment) {
return $line;
}

protected function prepare() {
$this->keys = [];
$this->widths = [];

$data = $this->data;

if ($this->formatter instanceof \Closure) {
foreach ($data as &$row)
array_walk($row, $this->formatter, $this);
unset($row);
}

foreach ($data as $row)
$this->keys = array_merge($this->keys, array_keys($row));
$this->keys = array_unique($this->keys);

foreach ($this->keys as $key)
$this->setWidth($key, $key);

foreach ($data as $row)
foreach ($row as $columnKey => $columnValue)
$this->setWidth($columnKey, $columnValue);

return $data;
}

protected function setWidth($key, $value) {
if (!isset($this->widths[$key]))
$this->widths[$key] = 0;
Expand Down

0 comments on commit ca82eb4

Please sign in to comment.