*/ public static function listFromJson(string $contents): array { try { $data = json_decode($contents, true, flags: JSON_THROW_ON_ERROR); } catch (JsonException $jsonException) { throw new HttpException(502, 'Pterodactyl returned an invalid egg catalog. Please try again later.', $jsonException); } throw_unless(is_array($data) && array_is_list($data), HttpException::class, 502, 'Pterodactyl returned an invalid egg catalog. Please try again later.'); $entries = []; foreach ($data as $value) { $entry = self::fromValue($value); if ($entry instanceof self) { $entries[] = $entry; } } throw_if($data !== [] && $entries === [], HttpException::class, 502, 'Pterodactyl returned an invalid egg catalog. Please try again later.'); return $entries; } public static function fromValue(mixed $value): ?self { if (! is_array($value) || ! is_string($value['id'] ?? null) || ! is_string($value['name'] ?? null) || ! is_string($value['downloadUrl'] ?? null) || $value['id'] === '' || $value['name'] === '') { return null; } return new self( $value['id'], $value['name'], is_string($value['description'] ?? null) ? $value['description'] : '', is_string($value['category'] ?? null) ? $value['category'] : '', $value['downloadUrl'], ); } public function downloadUrl(): string { try { $uri = new Uri($this->downloadUrl); } catch (InvalidArgumentException $invalidArgumentException) { throw new HttpException(502, 'The catalog egg has an invalid download URL.', $invalidArgumentException); } throw_if($uri->getScheme() !== 'https' || $uri->getHost() !== 'raw.githubusercontent.com' || ! str_starts_with($uri->getPath(), '/pterodactyl/') || $uri->getUserInfo() !== '' || $uri->getPort() !== null || $uri->getQuery() !== '' || $uri->getFragment() !== '', HttpException::class, 502, 'The catalog egg does not have an official Pterodactyl download URL.'); return $uri->__toString(); } /** @return array{id: string, name: string, description: string, category: string, source_url: string} */ public function toArray(): array { return [ 'id' => $this->id, 'name' => $this->name, 'description' => $this->description, 'category' => $this->category, 'source_url' => 'https://eggs.pterodactyl.io/egg/'.rawurlencode($this->id), ]; } }