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