You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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*.
4
+
[![Version][Version image]][Releases]
5
+
[![Build status][Build image]][Build]
6
+
7
+
ByteFormatter is a [PSR-2](http://www.php-fig.org/psr/psr-2/) compliant PHP library that formats byte values as
8
+
human-readable strings. An appropriate scale is calculated automatically such that the value never exceeds the base.
9
+
For example, in base 1024, `format(1023)` gives *1023 B* but `format(1024)` gives *1 KiB* instead of *1024 B*.
5
10
6
11
Requirements
7
12
------------
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.
13
+
14
+
- PHP 5.5 and Composer.
15
+
- Nothing else and no production dependencies!
10
16
11
17
Usage
12
18
-----
@@ -27,6 +33,7 @@ Bytes can be divided into multiples of 1000 by specifying `Base::DECIMAL` as the
27
33
28
34
Precision
29
35
---------
36
+
30
37
By default all values are rounded to the nearest integer.
31
38
32
39
```php
@@ -40,7 +47,8 @@ Increasing the precision with `setPrecision($precision)` allows the specified am
40
47
```
41
48
> 512.55 KiB
42
49
43
-
Increasing the precision will increase the maximum digits allowed but the formatter will only display as many as needed.
50
+
Increasing the precision will increase the maximum digits allowed but the formatter will only display as many as
51
+
needed.
44
52
45
53
```php
46
54
(new ByteFormatter)->setPrecision(2)->format(0x80200);
@@ -49,22 +57,32 @@ Increasing the precision will increase the maximum digits allowed but the format
49
57
50
58
Output format
51
59
-------------
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.
60
+
61
+
The format can be changed by calling the `setFormat($format)` function which takes a string format parameter.
62
+
The default format is `'%v %u'`. Occurrences of `%v` and `%u` in the format string will be replaced with the calculated
63
+
*value* and *units* respectively.
53
64
54
65
```php
55
66
(new ByteFormatter)->setFormat('%v%u')->format(0x80000);
56
67
```
57
68
> 512KiB
58
69
59
-
Customizing units
60
-
-----------------
61
-
Units are provided by collections that extend `ByteUnitCollection`. The current unit collection instance can be retrieved from `ByteFormatter::getUnitCollection()`.
70
+
Unit customization
71
+
------------------
72
+
73
+
Units are provided by decorators extending `UnitDecorator`. Two implementations are provided: the default
74
+
`SymbolDecorator` and an optional `NameDecorator`.
62
75
63
-
Unit collections are notified of base changes in the formatter so that different units can be returned for different bases. For example, the default collection 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.
76
+
Unit decorators receive the base of the formatter when asked to decorate a value so that different units can be
77
+
returned for different bases. For example, the default decorator outputs `KiB` in base 1024 for
78
+
*2<sup>10</sup> < bytes < 2<sup>20</sup>* but outputs `KB` in base 1000 for *1000 < bytes < 1000000*. This behaviour
79
+
can be suppressed by calling`SymbolDecorator::setSuffix()` with the desired `SymbolDecorator` suffix constant to
80
+
prevent units changing when the base is changed. Decorators also receive the exponent and scaled byte value.
64
81
65
-
### Symbol collection
82
+
### Symbol decorator
66
83
67
-
`ByteUnitSymbolCollection` is the default unit collection and returns units like *B*, *KB*, *MB*, etc. The symbol's suffix can be changed using one of the class constants from the following table.
84
+
`SymbolDecorator` is the default unit decorator and returns units like *B*, *KB*, *MB*, etc. The symbol's suffix can be
85
+
changed using one of the class constants from the following table.
(new ByteFormatter(new SymbolDecorator(SymbolDecorator::SUFFIX_NONE)))
105
+
->format(0x80000)
91
106
```
92
107
> 512 K
93
108
94
-
Note that no unit is displayed for bytes when the suffix is disabled. If this is undesired, byte units can be forced with `ByteUnitSymbolCollection::alwaysShowUnit()`.
109
+
Note that no unit is displayed for bytes when the suffix is disabled. If this is undesired, byte units can be forced
0 commit comments