* Parses JSON data based on the provided options and raw input. * * @param array $option * @param string $rawResult * @return array */
($option, $rawResult)
| 147 | * @return array |
| 148 | */ |
| 149 | private function jsonParser($option, $rawResult): array { |
| 150 | $pathString = $option['path']; |
| 151 | $json = $this->decodeJsonInput($rawResult); |
| 152 | |
| 153 | // Check for array extraction pattern (e.g., {BTC,tmsp,price}) |
| 154 | $arrayFields = $this->extractArrayFields($pathString); |
| 155 | if (!empty($arrayFields)) { |
| 156 | // Handle absolute path before array fields (e.g., data/data{from,to,intensity/forecast}) |
| 157 | $json = $this->extractAbsolutePathIfPresent($json, $pathString); |
| 158 | |
| 159 | // Normalize paths and ensure at least 3 columns |
| 160 | $paths = $this->normalizePaths($arrayFields, 3, 'empty'); |
| 161 | $data = $this->extractRowsFromArray($json, $paths); |
| 162 | } else { |
| 163 | // Single value extraction (e.g., data/currentHashrate,data/averageHashrate) |
| 164 | $paths = array_map('trim', explode(',', $pathString)); |
| 165 | $data = $this->extractSingleValues($json, $paths); |
| 166 | } |
| 167 | |
| 168 | // Derive header from paths (use last segment for each path) |
| 169 | $header = array_map([$this, 'getHeaderFromPath'], $paths); |
| 170 | |
| 171 | return [ |
| 172 | 'header' => $header, |
| 173 | 'dimensions' => array_slice($header, 0, count($header) - 1), |
| 174 | 'data' => $data, |
| 175 | ]; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Decode JSON input, supporting both standard and line-delimited JSON. |
no test coverage detected