Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions app/Helpers/Number.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Helpers;

use Illuminate\Support\Number as SupportNumber;

class Number extends SupportNumber
{
/**
* Convert the given number to its file size equivalent in bits.
*/
public static function fileSizeBits(int|float $bits, int $precision = 0, ?int $maxPrecision = null, bool $perSecond = false): string
{
$units = match ($perSecond) {
true => ['Bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'],
default => ['B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb']
};

for ($i = 0; ($bits / 1024) > 0.9 && ($i < count($units) - 1); $i++) {
$bits /= 1024;
}

return sprintf('%s %s', static::format($bits, $precision, $maxPrecision), $units[$i]);
}
}
25 changes: 25 additions & 0 deletions app/Models/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Events\ResultCreated;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -106,4 +107,28 @@ public function getJitterData(): array
'ping' => $data['ping']['jitter'] ?? null,
];
}

/**
* Get the result's download in bits.
*/
protected function downloadBits(): Attribute
{
return Attribute::make(
get: fn (mixed $value): ?string => ! blank($this->download) && is_numeric($this->download)
? number_format(num: $this->download * 8, decimals: 0, thousands_separator: '')
: null,
);
}

/**
* Get the result's upload in bits.
*/
protected function uploadBits(): Attribute
{
return Attribute::make(
get: fn (mixed $value): ?string => ! blank($this->upload) && is_numeric($this->upload)
? number_format(num: $this->upload * 8, decimals: 0, thousands_separator: '')
: null,
);
}
}