diff --git a/VERSION b/VERSION index 37f6d20..c9307ce 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2307.50127 +0.2307.52303 diff --git a/src/ByteFormat.php b/src/ByteFormat.php new file mode 100644 index 0000000..9478c1a --- /dev/null +++ b/src/ByteFormat.php @@ -0,0 +1,85 @@ + 1) + $string .= 's'; + } else { + if(!$decimal) + $string .= 'i'; + $string .= 'B'; + } + + return $string; + } + + /** + * Formats a raw amount of bytes as a human readable string in the power of 10 (e.g. MB). + * + * @param int $bytes Number of bytes. + * @return string Formatted byte string. + */ + public static function formatDecimal(int $bytes): string { + return self::format($bytes, true); + } + + /** + * Formats a raw amount of bytes as a human readable string in the power of 2 (e.g. MiB). + * + * @param int $bytes Number of bytes. + * @return string Formatted byte string. + */ + public static function formatBinary(int $bytes): string { + return self::format($bytes, false); + } +} diff --git a/src/XString.php b/src/XString.php index 61f527e..6651495 100644 --- a/src/XString.php +++ b/src/XString.php @@ -42,7 +42,7 @@ final class XString { /** * Counts unique characters in a string. * - * @param string String to count unique characters of. + * @param string $string String to count unique characters of. * @return int Unique character count. */ public static function countUnique(string $string): int { diff --git a/tests/ByteFormatTest.php b/tests/ByteFormatTest.php new file mode 100644 index 0000000..270ebfc --- /dev/null +++ b/tests/ByteFormatTest.php @@ -0,0 +1,103 @@ +assertEquals(ByteFormat::formatDecimal(0), 'Zero Bytes'); + $this->assertEquals(ByteFormat::formatBinary(0), 'Zero Bytes'); + + $this->assertEquals(ByteFormat::formatDecimal(64), '64 Bytes'); + $this->assertEquals(ByteFormat::formatBinary(64), '64 Bytes'); + + $this->assertEquals(ByteFormat::formatDecimal(-64), '-64 Bytes'); + $this->assertEquals(ByteFormat::formatBinary(-64), '-64 Bytes'); + + $this->assertEquals(ByteFormat::formatDecimal(512), '512 Bytes'); + $this->assertEquals(ByteFormat::formatBinary(512), '512 Bytes'); + + $this->assertEquals(ByteFormat::formatDecimal(-512), '-512 Bytes'); + $this->assertEquals(ByteFormat::formatBinary(-512), '-512 Bytes'); + + $this->assertEquals(ByteFormat::formatDecimal(1000), '1.00 KB'); + $this->assertEquals(ByteFormat::formatBinary(1000), '1000 Bytes'); + + $this->assertEquals(ByteFormat::formatDecimal(-1000), '-1.00 KB'); + $this->assertEquals(ByteFormat::formatBinary(-1000), '-1000 Bytes'); + + $this->assertEquals(ByteFormat::formatDecimal(1024), '1.02 KB'); + $this->assertEquals(ByteFormat::formatBinary(1024), '1.00 KiB'); + + $this->assertEquals(ByteFormat::formatDecimal(-1024), '-1.02 KB'); + $this->assertEquals(ByteFormat::formatBinary(-1024), '-1.00 KiB'); + + $this->assertEquals(ByteFormat::formatDecimal(1000000), '1.00 MB'); + $this->assertEquals(ByteFormat::formatBinary(1000000), '976.6 KiB'); + + $this->assertEquals(ByteFormat::formatDecimal(-1000000), '-1.00 MB'); + $this->assertEquals(ByteFormat::formatBinary(-1000000), '-976.6 KiB'); + + $this->assertEquals(ByteFormat::formatDecimal(1048576), '1.05 MB'); + $this->assertEquals(ByteFormat::formatBinary(1048576), '1.00 MiB'); + + $this->assertEquals(ByteFormat::formatDecimal(-1048576), '-1.05 MB'); + $this->assertEquals(ByteFormat::formatBinary(-1048576), '-1.00 MiB'); + + $this->assertEquals(ByteFormat::formatDecimal(25252525), '25.3 MB'); + $this->assertEquals(ByteFormat::formatBinary(25252525), '24.1 MiB'); + + $this->assertEquals(ByteFormat::formatDecimal(-25252525), '-25.3 MB'); + $this->assertEquals(ByteFormat::formatBinary(-25252525), '-24.1 MiB'); + + $this->assertEquals(ByteFormat::formatDecimal(26476544), '26.5 MB'); + $this->assertEquals(ByteFormat::formatBinary(26476544), '25.2 MiB'); + + $this->assertEquals(ByteFormat::formatDecimal(-26476544), '-26.5 MB'); + $this->assertEquals(ByteFormat::formatBinary(-26476544), '-25.2 MiB'); + + $this->assertEquals(ByteFormat::formatDecimal(1000000000), '1.00 GB'); + $this->assertEquals(ByteFormat::formatBinary(1000000000), '953.7 MiB'); + + $this->assertEquals(ByteFormat::formatDecimal(-1000000000), '-1.00 GB'); + $this->assertEquals(ByteFormat::formatBinary(-1000000000), '-953.7 MiB'); + + $this->assertEquals(ByteFormat::formatDecimal(1073741824), '1.07 GB'); + $this->assertEquals(ByteFormat::formatBinary(1073741824), '1.00 GiB'); + + $this->assertEquals(ByteFormat::formatDecimal(-1073741824), '-1.07 GB'); + $this->assertEquals(ByteFormat::formatBinary(-1073741824), '-1.00 GiB'); + + $this->assertEquals(ByteFormat::formatDecimal(1000000000000), '1.00 TB'); + $this->assertEquals(ByteFormat::formatBinary(1000000000000), '931.3 GiB'); + + $this->assertEquals(ByteFormat::formatDecimal(-1000000000000), '-1.00 TB'); + $this->assertEquals(ByteFormat::formatBinary(-1000000000000), '-931.3 GiB'); + + $this->assertEquals(ByteFormat::formatDecimal(1099511627776), '1.10 TB'); + $this->assertEquals(ByteFormat::formatBinary(1099511627776), '1.00 TiB'); + + $this->assertEquals(ByteFormat::formatDecimal(-1099511627776), '-1.10 TB'); + $this->assertEquals(ByteFormat::formatBinary(-1099511627776), '-1.00 TiB'); + + $this->assertEquals(ByteFormat::formatDecimal(1000000000000000), '1.00 PB'); + $this->assertEquals(ByteFormat::formatBinary(1000000000000000), '909.5 TiB'); + + $this->assertEquals(ByteFormat::formatDecimal(-1000000000000000), '-1.00 PB'); + $this->assertEquals(ByteFormat::formatBinary(-1000000000000000), '-909.5 TiB'); + + $this->assertEquals(ByteFormat::formatDecimal(1125899906842624), '1.13 PB'); + $this->assertEquals(ByteFormat::formatBinary(1125899906842624), '1.00 PiB'); + + $this->assertEquals(ByteFormat::formatDecimal(-1125899906842624), '-1.13 PB'); + $this->assertEquals(ByteFormat::formatBinary(-1125899906842624), '-1.00 PiB'); + } +}