Console.php
1 month ago
Csv.php
2 weeks ago
Html.php
1 month ago
Json.php
1 month ago
Rss.php
2 weeks ago
Tsv.php
1 month ago
Xml.php
1 month ago
Json.php
104 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\Common; |
| 12 | use Piwik\DataTable\Renderer; |
| 13 | use Piwik\DataTable; |
| 14 | /** |
| 15 | * JSON export. |
| 16 | * Works with recursive DataTable (when a row can be associated with a subDataTable). |
| 17 | * |
| 18 | */ |
| 19 | class Json extends Renderer |
| 20 | { |
| 21 | /** |
| 22 | * Computes the dataTable output and returns the string/binary |
| 23 | */ |
| 24 | public function render() : string |
| 25 | { |
| 26 | return $this->renderTable($this->table); |
| 27 | } |
| 28 | /** |
| 29 | * Computes the output for the given data table |
| 30 | * |
| 31 | * @param DataTable $table |
| 32 | * @return string |
| 33 | */ |
| 34 | protected function renderTable($table) |
| 35 | { |
| 36 | if (is_array($table)) { |
| 37 | $array = $table; |
| 38 | if (self::shouldWrapArrayBeforeRendering($array, $wrapSingleValues = \true)) { |
| 39 | $array = array($array); |
| 40 | } |
| 41 | foreach ($array as $key => $tab) { |
| 42 | if ($tab instanceof DataTable\Map || $tab instanceof DataTable || $tab instanceof DataTable\Simple) { |
| 43 | $array[$key] = $this->convertDataTableToArray($tab); |
| 44 | if (!is_array($array[$key])) { |
| 45 | $array[$key] = array('value' => $array[$key]); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } else { |
| 50 | $array = $this->convertDataTableToArray($table); |
| 51 | } |
| 52 | if (!is_array($array)) { |
| 53 | $array = array('value' => $array); |
| 54 | } |
| 55 | // convert datatable column/metadata values |
| 56 | $this->convertDataTableColumnMetadataValues($array); |
| 57 | // decode all entities |
| 58 | $callback = function (&$value, $key) { |
| 59 | if (is_string($value)) { |
| 60 | $value = html_entity_decode($value, \ENT_QUOTES, "UTF-8"); |
| 61 | } |
| 62 | }; |
| 63 | array_walk_recursive($array, $callback); |
| 64 | // silence "Warning: json_encode(): Invalid UTF-8 sequence in argument" |
| 65 | $str = @json_encode($array); |
| 66 | if ($str === \false && json_last_error() === \JSON_ERROR_UTF8 && $this->canMakeArrayUtf8()) { |
| 67 | $array = $this->makeArrayUtf8($array); |
| 68 | $str = json_encode($array); |
| 69 | } |
| 70 | return $str; |
| 71 | } |
| 72 | private function canMakeArrayUtf8() |
| 73 | { |
| 74 | return function_exists('mb_convert_encoding'); |
| 75 | } |
| 76 | private function makeArrayUtf8($array) |
| 77 | { |
| 78 | if (is_array($array)) { |
| 79 | foreach ($array as $key => $value) { |
| 80 | $array[$key] = self::makeArrayUtf8($value); |
| 81 | } |
| 82 | } elseif (is_string($array)) { |
| 83 | return mb_convert_encoding($array, 'UTF-8', 'auto'); |
| 84 | } |
| 85 | return $array; |
| 86 | } |
| 87 | public static function sendHeaderJSON() |
| 88 | { |
| 89 | Common::sendHeader('Content-Type: application/json; charset=utf-8'); |
| 90 | } |
| 91 | private function convertDataTableColumnMetadataValues(&$table) |
| 92 | { |
| 93 | if (empty($table)) { |
| 94 | return; |
| 95 | } |
| 96 | array_walk_recursive($table, function (&$value, $key) { |
| 97 | if ($value instanceof DataTable) { |
| 98 | $value = $this->convertDataTableToArray($value); |
| 99 | $this->convertDataTableColumnMetadataValues($value); |
| 100 | } |
| 101 | }); |
| 102 | } |
| 103 | } |
| 104 |