|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Resources\V1; |
| 4 | + |
| 5 | +use App\Helpers\Bitrate; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Illuminate\Http\Resources\Json\JsonResource; |
| 8 | + |
| 9 | +class StatResource extends JsonResource |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Transform the resource into an array. |
| 13 | + * |
| 14 | + * @return array<string, mixed> |
| 15 | + */ |
| 16 | + public function toArray(Request $request): array |
| 17 | + { |
| 18 | + return [ |
| 19 | + 'ping' => [ |
| 20 | + 'avg' => round($this->avg_ping, 2), |
| 21 | + 'min' => round($this->min_ping, 2), |
| 22 | + 'max' => round($this->max_ping, 2), |
| 23 | + ], |
| 24 | + 'download' => [ |
| 25 | + 'avg' => round($this->avg_download), |
| 26 | + 'avg_bits' => $this->when($this->avg_download, fn (): int|float => Bitrate::bytesToBits($this->avg_download)), |
| 27 | + 'avg_bits_human' => $this->when($this->avg_download, fn (): string => Bitrate::formatBits(Bitrate::bytesToBits($this->avg_download)).'ps'), |
| 28 | + 'min' => round($this->min_download), |
| 29 | + 'min_bits' => $this->when($this->min_download, fn (): int|float => Bitrate::bytesToBits($this->min_download)), |
| 30 | + 'min_bits_human' => $this->when($this->min_download, fn (): string => Bitrate::formatBits(Bitrate::bytesToBits($this->min_download)).'ps'), |
| 31 | + 'max' => round($this->max_download), |
| 32 | + 'max_bits' => $this->when($this->max_download, fn (): int|float => Bitrate::bytesToBits($this->max_download)), |
| 33 | + 'max_bits_human' => $this->when($this->max_download, fn (): string => Bitrate::formatBits(Bitrate::bytesToBits($this->max_download)).'ps'), |
| 34 | + ], |
| 35 | + 'upload' => [ |
| 36 | + 'avg' => round($this->avg_upload), |
| 37 | + 'avg_bits' => $this->when($this->avg_upload, fn (): int|float => Bitrate::bytesToBits($this->avg_upload)), |
| 38 | + 'avg_bits_human' => $this->when($this->avg_upload, fn (): string => Bitrate::formatBits(Bitrate::bytesToBits($this->avg_upload)).'ps'), |
| 39 | + 'min' => round($this->min_upload), |
| 40 | + 'min_bits' => $this->when($this->min_upload, fn (): int|float => Bitrate::bytesToBits($this->min_upload)), |
| 41 | + 'min_bits_human' => $this->when($this->min_upload, fn (): string => Bitrate::formatBits(Bitrate::bytesToBits($this->min_upload)).'ps'), |
| 42 | + 'max' => round($this->max_upload), |
| 43 | + 'max_bits' => $this->when($this->max_upload, fn (): int|float => Bitrate::bytesToBits($this->max_upload)), |
| 44 | + 'max_bits_human' => $this->when($this->max_upload, fn (): string => Bitrate::formatBits(Bitrate::bytesToBits($this->max_upload)).'ps'), |
| 45 | + ], |
| 46 | + 'total_results' => $this->total_results, |
| 47 | + ]; |
| 48 | + } |
| 49 | +} |
0 commit comments