* Parses JSON data based on the provided options and raw input. * * @param array $option * @param string $rawResult * @return array */
($option, $rawResult)
| 112 | * @return array |
| 113 | */ |
| 114 | private function jsonParser($option, $rawResult): array { |
| 115 | $pathString = $option['path']; |
| 116 | $json = $this->decodeJsonInput($rawResult); |
| 117 | |
| 118 | // Check for array extraction pattern (e.g., {BTC,tmsp,price}) |
| 119 | $arrayFields = $this->extractArrayFields($pathString); |
| 120 | |
| 121 | if (!empty($arrayFields)) { |
| 122 | // Handle absolute path before array fields (e.g., data/data{from,to,intensity/forecast}) |
| 123 | $json = $this->extractAbsolutePathIfPresent($json, $pathString); |
| 124 | |
| 125 | // Normalize paths and ensure at least 3 columns |
| 126 | $paths = $this->normalizePaths($arrayFields, 3, 'empty'); |
| 127 | $data = $this->extractRowsFromArray($json, $paths); |
| 128 | } else { |
| 129 | // Single value extraction (e.g., data/currentHashrate,data/averageHashrate) |
| 130 | $paths = array_map('trim', explode(',', $pathString)); |
| 131 | $data = $this->extractSingleValues($json, $paths); |
| 132 | } |
| 133 | |
| 134 | // Derive header from paths (use last segment for each path) |
| 135 | $header = array_map([$this, 'getHeaderFromPath'], $paths); |
| 136 | |
| 137 | return [ |
| 138 | 'header' => $header, |
| 139 | 'dimensions' => array_slice($header, 0, count($header) - 1), |
| 140 | 'data' => $data, |
| 141 | ]; |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Decode JSON input, supporting both standard and line-delimited JSON. |
no test coverage detected