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