|
| 1 | +ByteFormatter |
| 2 | +============= |
| 3 | + |
| 4 | +ByteFormatter is a PHP library that formats byte values as human-readable strings. An appropriate scale is calculated automatically such that the value never exceeds the base. For example, in base 1024, `format(1023)` gives *1023 B* but `format(1024)` gives *1 KiB* instead of *1024 B*. |
| 5 | + |
| 6 | +Requirements |
| 7 | +------------ |
| 8 | +- PHP 5.5 for generator support. |
| 9 | +- 64-bit support is required if you want to work with terabytes or run the unit tests. |
| 10 | + |
| 11 | +Usage |
| 12 | +----- |
| 13 | + |
| 14 | +By default bytes are divided using `Base::BINARY` into multiples of 1024. |
| 15 | + |
| 16 | +```php |
| 17 | +(new ByteFormatter)->format(0x80000); |
| 18 | +``` |
| 19 | +> 512 KiB |
| 20 | +
|
| 21 | +Bytes can be divided into multiples of 1000 by specifying `Base::DECIMAL` as the base. |
| 22 | + |
| 23 | +```php |
| 24 | +(new ByteFormatter)->setBase(Base::DECIMAL)->format(500000); |
| 25 | +``` |
| 26 | +> 500 KB |
| 27 | +
|
| 28 | +Precision |
| 29 | +--------- |
| 30 | +By default all values are rounded to the nearest integer. |
| 31 | + |
| 32 | +```php |
| 33 | +(new ByteFormatter)->format(0x80233); |
| 34 | +``` |
| 35 | +> 513 KiB |
| 36 | +
|
| 37 | +Increasing the precision with `setPrecision($precision)` allows the specified amount of digits after the decimal point. |
| 38 | +```php |
| 39 | +(new ByteFormatter)->setPrecision(2)->format(0x80233); |
| 40 | +``` |
| 41 | +> 512.55 KiB |
| 42 | +
|
| 43 | +Increasing the precision will increase the maximum digits allowed but the formatter will only display as many as needed. |
| 44 | + |
| 45 | +```php |
| 46 | +(new ByteFormatter)->setPrecision(2)->format(0x80200); |
| 47 | +``` |
| 48 | +> 512.5 KiB |
| 49 | +
|
| 50 | +Output format |
| 51 | +------------- |
| 52 | +The format can be changed by calling the `setFormat($format)` function which takes a string format parameter. The default format is `'%v %u'`. Occurrences of `%v` and `%u` in the format string will be replaced with the calculated *value* and *units* respectively. |
| 53 | + |
| 54 | +```php |
| 55 | +(new ByteFormatter)->setFormat('%v%u')->format(0x80000); |
| 56 | +``` |
| 57 | +> 512KiB |
| 58 | +
|
| 59 | +Customizing units |
| 60 | +----------------- |
| 61 | +Units are generated by sequence generators. The current unit sequence generator instance can be retrieved from `ByteFormatter::getUnitSequence()`. |
| 62 | + |
| 63 | +Sequence generators are notified of base changes in the formatter so that different units can be generated for different bases. For example, the default generator outputs `KiB` in base 1024 for *2<sup>10</sup> < bytes < 2<sup>20</sup>* but outputs `KB` in base 1000 for *1000 < bytes < 1000000*. This behaviour can be suppressed by calling `disableAutomaticUnitSwitching(true)` to prevent units changing when the base is changed. |
| 64 | + |
| 65 | +### Byte unit symbol sequence |
| 66 | + |
| 67 | +`ByteUnitSymbolSequence` is the default unit sequence and generates units like *B*, *KB*, *MB*, etc. The symbol's suffix can be changed according to one of the constants from the following table. |
| 68 | + |
| 69 | +| Constant | B | K | M | G | T | P | E | Z | Y | |
| 70 | +|---------------|:-:|:---:|:---:|:---:|:---:|:---:|:---:|:---:|:---:| |
| 71 | +| SUFFIX_NONE | | K | M | G | T | P | E | Z | Y | |
| 72 | +| SUFFIX_METRIC | B | KB | MB | GB | TB | PB | EB | ZB | YB | |
| 73 | +| SUFFIX_IEC | B | KiB | MiB | GiB | TiB | PiB | EiB | ZiB | YiB | |
| 74 | + |
| 75 | +The following example uses base 1024 but displays the metric suffix, like Windows Explorer. |
| 76 | + |
| 77 | +```php |
| 78 | +(new ByteFormatter) |
| 79 | + ->setBase(Base::BINARY) |
| 80 | + ->setUnitSymbolSuffix(ByteUnitSymbolSequence::SUFFIX_METRIC) |
| 81 | + ->format(0x80000); |
| 82 | +``` |
| 83 | +> 512 KB |
| 84 | +
|
| 85 | +If you prefer terse notation the suffix may be removed with `SUFFIX_NONE`. |
| 86 | + |
| 87 | +```php |
| 88 | +(new ByteFormatter) |
| 89 | + ->setUnitSymbolSuffix(ByteUnitSymbolSequence::SUFFIX_NONE) |
| 90 | + ->format(0x80000); |
| 91 | +``` |
| 92 | +> 512 K |
| 93 | +
|
| 94 | +Note that no unit is displayed for bytes when the suffix is disabled. If this is undesired, byte units can be forced with `ByteUnitSymbolSequence::alwaysShowUnit()`. |
| 95 | + |
| 96 | +```php |
| 97 | +$formatter = (new ByteFormatter) |
| 98 | + ->setUnitSymbolSuffix(ByteUnitSymbolSequence::SUFFIX_NONE) |
| 99 | + ->format(512); |
| 100 | +``` |
| 101 | +> 512 |
| 102 | +
|
| 103 | +```php |
| 104 | +$formatter->getUnitSequence()->alwaysShowUnit(); |
| 105 | +$formatter->format(512); |
| 106 | +``` |
| 107 | +> 512 B |
| 108 | +
|
| 109 | +### Byte unit name sequence |
| 110 | +`ByteUnitNameSequence` can be used to replace the default unit sequence generator and generates units like *byte*, *kilobyte*, *megabyte*, etc. |
| 111 | + |
| 112 | +```php |
| 113 | +(new ByteFormatter) |
| 114 | + ->setUnitSequence(new ByteUnitNameSequence) |
| 115 | + ->format(0x80000); |
| 116 | +``` |
| 117 | +> 512 kibibytes |
| 118 | +
|
| 119 | +Using decimal base: |
| 120 | + |
| 121 | +```php |
| 122 | +(new ByteFormatter) |
| 123 | + ->setUnitSequence(new ByteUnitNameSequence) |
| 124 | + ->setBase(Base::DECIMAL) |
| 125 | + ->format(500000); |
| 126 | +``` |
| 127 | +> 500 kilobytes |
| 128 | +
|
| 129 | +Testing |
| 130 | +------- |
| 131 | +This library is fully unit tested. Run the tests with `vendor/bin/phpunit test/` from the command line. |
0 commit comments