1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\DataTable\Renderer;
use Piwik\DataTable;
use Piwik\DataTable\Renderer;
/**
* Simple output
*/
class Console extends Renderer
{
/**
* Prefix
*
* @var string
*/
protected $prefixRows = '#';
/**
* Computes the dataTable output and returns the string/binary
*
*/
public function render(): string
{
return $this->renderTable($this->table);
}
/**
* Sets the prefix to be used
*
* @param string $str new prefix
*/
public function setPrefixRow(string $str): void
{
$this->prefixRows = $str;
}
/**
* Computes the output of the given array of data tables
*
* @param DataTable\Map $map data tables to render
* @param string $prefix prefix to output before table data
*/
protected function renderDataTableMap(DataTable\Map $map, string $prefix): string
{
$output = "Set<hr />";
$prefix = $prefix . ' ';
foreach ($map->getDataTables() as $descTable => $table) {
$output .= $prefix . "<b>" . $descTable . "</b><br />";
$output .= $prefix . $this->renderTable($table, $prefix . ' ');
$output .= "<hr />";
}
return $output;
}
/**
* Computes the given dataTable output and returns the string/binary
*
* @param array|DataTable|DataTable\Map $table data table to render
* @param string $prefix prefix to output before table data
*/
protected function renderTable($table, string $prefix = ''): string
{
if (is_array($table)) {
// convert array to DataTable
$table = DataTable::makeFromSimpleArray($table);
}
if ($table instanceof DataTable\Map) {
return $this->renderDataTableMap($table, $prefix);
}
if ($table->getRowsCount() === 0) {
return "Empty table<br />\n";
}
static $depth = 0;
$output = '';
$i = 1;
foreach ($table->getRows() as $row) {
$dataTableMapBreak = false;
$columns = array();
foreach ($row->getColumns() as $column => $value) {
if ($value instanceof DataTable\Map) {
$output .= $this->renderDataTableMap($value, $prefix);
$dataTableMapBreak = true;
break;
}
if (is_string($value)) {
$value = "'$value'";
} elseif (is_array($value)) {
$value = var_export($value, true);
}
$columns[] = "'$column' => $value";
}
if ($dataTableMapBreak === true) {
continue;
}
$columns = implode(", ", $columns);
if ($this->hideMetadata) {
$output .= str_repeat($this->prefixRows, $depth)
. "- $i [" . $columns . "]<br />\n";
} else {
$metadata = [];
foreach ($row->getMetadata() as $name => $value) {
if (is_string($value)) {
$value = "'$value'";
} elseif (is_array($value)) {
$value = var_export($value, true);
}
$metadata[] = "'$name' => $value";
}
$metadata = implode(", ", $metadata);
$output .= str_repeat($this->prefixRows, $depth)
. "- $i [" . $columns . "] [" . $metadata . "] [idsubtable = "
. $row->getIdSubDataTable() . "]<br />\n";
}
if (!is_null($row->getIdSubDataTable())) {
$subTable = $row->getSubtable();
if ($subTable) {
$depth++;
$output .= $this->renderTable($subTable, $prefix . ' ');
$depth--;
} else {
$output .= "-- Sub DataTable not loaded<br />\n";
}
}
$i++;
}
if (!$this->hideMetadata) {
$metadata = $table->getAllTableMetadata();
if (!empty($metadata)) {
$output .= "<hr />Metadata<br />";
foreach ($metadata as $id => $metadataIn) {
$output .= "<br />";
$output .= $prefix . " <b>$id</b><br />";
if (is_array($metadataIn)) {
foreach ($metadataIn as $name => $value) {
if (is_object($value) && !method_exists($value, '__toString')) {
$value = 'Object [' . get_class($value) . ']';
} elseif (is_array($value)) {
$value = 'Array ' . json_encode($value);
}
if (is_array($value)) {
$value = json_encode($value);
}
$output .= $prefix . $prefix . "$name => $value";
}
}
}
}
}
return $output;
}
}
|