(string $html, array $option)
| 307 | } |
| 308 | |
| 309 | private function parsePackageDownloadRows(string $html, array $option): array { |
| 310 | $rows = []; |
| 311 | $limit = isset($option['limit']) && $option['limit'] !== '' ? (int)$option['limit'] : 0; |
| 312 | |
| 313 | preg_match_all('/<li\b[^>]*class="[^"]*\bBox-row\b[^"]*"[^>]*>(.*?)<\/li>/is', $html, $matches); |
| 314 | foreach ($matches[1] as $rowHtml) { |
| 315 | preg_match_all('/<a\b[^>]*class="[^"]*\bLabel\b[^"]*"[^>]*>(.*?)<\/a>/is', $rowHtml, $tagMatches); |
| 316 | if ($tagMatches[1] === []) { |
| 317 | continue; |
| 318 | } |
| 319 | |
| 320 | if (!preg_match('/octicon-download.*?<\/svg>\s*([\d,.]+)/is', $rowHtml, $downloadMatch)) { |
| 321 | continue; |
| 322 | } |
| 323 | |
| 324 | $downloads = $this->parseDownloadCount($downloadMatch[1]); |
| 325 | foreach ($tagMatches[1] as $tagHtml) { |
| 326 | $tag = trim(html_entity_decode(strip_tags($tagHtml), ENT_QUOTES | ENT_HTML5)); |
| 327 | if ($tag !== '') { |
| 328 | $rows[] = [ |
| 329 | 'tag' => $tag, |
| 330 | 'downloads' => $downloads, |
| 331 | ]; |
| 332 | } |
| 333 | |
| 334 | if ($limit > 0 && count($rows) >= $limit) { |
| 335 | return $rows; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | return $rows; |
| 341 | } |
| 342 | |
| 343 | private function parseDownloadCount(string $value): int { |
| 344 | $digits = preg_replace('/\D+/', '', $value); |
no test coverage detected