gh-o-st / bytesize
A simple conversion library for dealing with digital size units.
dev-master
2024-10-15 18:24 UTC
Requires
- php: >=8.3
- ext-bcmath: *
Requires (Dev)
- phpstan/phpstan: @stable
- phpunit/phpunit: @stable
This package is auto-updated.
Last update: 2025-06-15 19:35:53 UTC
README
A simple conversion library for dealing with digital size units.
Features
- Comprehensive Unit Support: Supports both IEC and SI units, from bits and bytes up to ronabits and ronabytes.
- Simple API: Offers a straightforward static method to perform conversions without the need to instantiate classes.
Requirements
- PHP ≥ 8.3: Leverages modern PHP features, including enums and strict typing.
- BCMath Extension: Ensure that the
ext-bcmath
extension is enabled in your PHP installation.
Installation
Install Bytesize via Composer:
composer require gh-o-st/bytesize
Make sure Composer's autoloader is included in your project:
require 'vendor/autoload.php';
Error Handling
The convert
method will throw an InvalidArgumentException
in the following cases:
- If the value provided is not numeric.
- If the source or target units are invalid or not supported.
use InvalidArgumentException; try { $result = SizeConverter::convert('abc', SI::BYTE->value, SI::KILOBYTE->value); } catch (InvalidArgumentException $e) { echo $e->getMessage(); }
Supported Units
SI Units
- Bits
b
and BytesB
- Kilobit
Kb
and KilobyteKB
- Megabit
Mb
and MegabyteMB
- Gigabit
Gb
and GigabyteGB
- Terabit
Tb
and TerabyteTB
- Petabit
Pb
and PetabytePB
- Exabit
Eb
and ExabyteEB
- Zetabit
Zb
and ZetabyteZB
- Yotabit
Yb
and YotabyteYB
- Ronabit
Rb
and RonabyteRB
IEC Units
- Bits
b
and BytesB
- Kibibit
Kib
and KibibyteKiB
- Mebibit
Mib
and MebibyteMiB
- Gibibit
Gib
and GibibyteGiB
- Tebibit
Tib
and TebibyteTiB
- Pebibit
Pib
and PebibytePiB
- Exbibit
Eib
and ExbibyteEiB
- Zebibit
Zib
and ZebibyteZiB
- Yobibit
Yib
and YobibyteYiB
- Ronbibit
Rib
and RonbibyteRiB
Running Tests
To run the included test suite, you'll need to have PHPUnit installed:
composer install --dev vendor/bin/phpunit
License
This project is licensed under the MIT License