字节之间的转换

v1.1.0 2024-02-25 12:32 UTC

This package is auto-updated.

Last update: 2024-09-25 13:50:33 UTC


README

此库允许您以近乎原生的方式创建定义数据单元,比较它们,并在它们之间进行转换。

使用此库

composer require withinboredom/bytes

此库很特别!

以下代码是100%有效的

$kb = \Withinboredom\Bytes\Kilobytes(128)->multiply(1000);
$mb = \Withinboredom\Bytes\Megabytes(125);
$tb = \Withinboredom\Bytes\Terrabytes(1);

$arr = [$kb, $mb, $tb];
// sort the array
usort($arr, \Withinboredom\Bytes\Bytes::compare(...));

// values with the same units are always triple-equal to each other
assert($kb->megabytes() === $mb);

// values with the same units are always natively comparable
assert($mb < $tb->megabytes());

// you can always expect a certain unit
function test(\Withinboredom\Bytes\Megabytes $megabytes): void;

// or convert them to your expected unit in your body
function test(\Withinboredom\Bytes\DataUnit $size): void;

// get base-2 sized values
echo $kb->getBinaryValue();

// or si-units
echo $kb->getSiValue();