open-southeners/byte-unit-converter
PHP 8.1+ utility to convert byte sizes between multiple units with no dependencies. Inspired by macOS ByteCountFormatter, it helps format and convert storage values consistently for apps and libraries.
add, sub and subtract (object bytes still immutable, so new instance of the object will be returned on each operation)asRound method now accepts integer and boolean (default now is round up to 2 decimal positions if possible).ByteUnitConverter::numberFormat() static method to be reused within library (although can be used externally)ByteUnitConverter::new() method now accepts strings or integers as input argumentByteUnitConverter::from() method now accepts strings, floats or integers as first input argumentByteUnitConverter::asRound() method now returns zero (0) results with decimals only if results are 0ByteUnitConverter::nearestUnit() method to convert to nearest byte unit (and metric system):(string) ByteUnitConverter::new("1024")->nearestUnit(MetricSystem::Binary); // 1 KiB
(string) ByteUnitConverter::new("500")->nearestUnit(stoppingAt: ByteUnit::KB); // 0.50 KB
OpenSoutheners\ByteUnitConverter\MetricSystem enum for decimal or binary metric systemsOpenSoutheners\ByteUnitConverter\DataUnit enum for bytes or bits data unitsByteUnitConverter::toArray method to serialise to arrayByteUnitConverter::__toString method to serialise to string with the closest unit appendedByteUnitConverter::__serialize & ByteUnitConverter::__deserialize methods to object serialization and deserializationByteUnit::lowerThan method to check current unit lower than specified (opposite than already available greaterThan)ByteUnitConverter::new convenience method to create an instance of ByteUnitConverter classByteUnitConverter::usingBits & ByteUnitConverter::usingBytes to switch data units (using bytes by default)ByteUnitConverter::asRound while precision method only operates during decimal conversions, this method will return a round number with no decimals (defaulted to false). E.g:(string) ByteUnitConverter::new('1924')->toKiB(); // "1.87 KiB"
(string) ByteUnitConverter::new('1924')->asRound()->toKiB(); // "2 KiB"
OpenSoutheners\ByteUnitConverter\DecimalByteUnit & OpenSoutheners\ByteUnitConverter\BinaryByteUnit replaced by OpenSoutheners\ByteUnitConverter\ByteUnitconversion and toBytesFromUnit methodsto* methods (toTB, toGB, toMB, etc...) now returns instance instead of number. Use the following to get it casted to a string:(string) ByteUnitConverter::new('1024')->toKiB(); // "1.00 KiB"
ByteUnitConverter::conversion static method (simplified to ByteUnitConverter::new()...)ByteUnitConverter::toBitsFromUnit static method (we only use bytes as a starter, can switch to bits while having an instance but not before)How can I help you explore Laravel packages today?