Skip to content

Conversation

alexjustesen
Copy link

@alexjustesen alexjustesen commented Oct 6, 2025

📃 Description

This PR introduces a bitRate() number helper to convert a given bit value to a string representation. Example 1000 to 1 Kbps. This is modeled off of the existing fileSize() helper.

📖 Docs

laravel/docs#10846

Comment on lines +229 to +237
$units = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'];

$unitCount = count($units);

for ($i = 0; ($bits / 1000) > 0.9 && ($i < $unitCount - 1); $i++) {
$bits /= 1000;
}

return sprintf('%s %s', static::format($bits, $precision, $maxPrecision), $units[$i]);
Copy link
Contributor

@shaedrich shaedrich Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could reduce duplicated code

Suggested change
$units = ['bps', 'Kbps', 'Mbps', 'Gbps', 'Tbps', 'Pbps', 'Ebps', 'Zbps', 'Ybps'];
$unitCount = count($units);
for ($i = 0; ($bits / 1000) > 0.9 && ($i < $unitCount - 1); $i++) {
$bits /= 1000;
}
return sprintf('%s %s', static::format($bits, $precision, $maxPrecision), $units[$i]);
$formatted = strtolower(static::fileSize($bits, $precision, $maxPrexision) . 'ps';
return $bits < 1000 ? $formatted : ucwords($formatted);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shaedrich I didn't reuse the fileSize() method because bits and bytes are calculated differently. Bytes are calculated using binary base-2 (power of 1024) where bits are decimal base-10 (power of 1000).

Also the capitalization of the "b" matters as "Kb" = Kilobit and "KB" is Kilobyte etc.

Copy link
Contributor

@shaedrich shaedrich Oct 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalization is taken care of as you can see, but you are right about the base.

This could probably be moved to a private utility method:

    /**
	 * @template TValue of string[]
	 * 
	 * @param TValue $values
	 *
     * @return array{int|float, value-of<TValue>}
     */
    private static function convertForDisplay(int|float $value, array $units, int $base)
    {
        $unitCount = count($units);

        for ($i = 0; ($value / $base) > 0.9 && ($i < $unitCount - 1); $i++) {
            $value /= $base;
        }

        return [$value, $units[$i]];
    }

but maybe, this is overkill

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My 2c I think the utility method might be useful if there is a 3rd method that relies on converting an integer to a string with a unit. Just my personal take 🤷‍♂️

Happy to do that in a separate PR and apply that to the fileSize() method as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely 👍🏻

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants