1 | <?php |
||
17 | class Bytes |
||
18 | { |
||
19 | /** |
||
20 | * 1024 binary base. |
||
21 | */ |
||
22 | const BASE_BINARY = 1024; |
||
23 | /** |
||
24 | * 1000 decimal base. |
||
25 | */ |
||
26 | const BASE_DECIMAL = 1000; |
||
27 | /** |
||
28 | * B, M, G, ... |
||
29 | */ |
||
30 | const SUFFIX_SIMPLE = 0; |
||
31 | /** |
||
32 | * KiB, MiB, GiB, ... (SHOULD be used with BASE_BINARY). |
||
33 | */ |
||
34 | const SUFFIX_IEC = 1; |
||
35 | /** |
||
36 | * kibibytes, mebibytes, gibibytes, ... (SHOULD be used with BASE_BINARY). |
||
37 | */ |
||
38 | const SUFFIX_IEC_LONG = 2; |
||
39 | /** |
||
40 | * kB, MB, GB, ... (SHOULD be used with BASE_DECIMAL). |
||
41 | */ |
||
42 | const SUFFIX_SI = 3; |
||
43 | /** |
||
44 | * kilobytes, megabytes, gigabytes, ... (SHOULD be used with BASE_DECIMAL). |
||
45 | */ |
||
46 | const SUFFIX_SI_LONG = 4; |
||
47 | |||
48 | private static $suffixes = [ |
||
49 | self::SUFFIX_SIMPLE => ['B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'], |
||
50 | self::SUFFIX_IEC => ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'], |
||
51 | self::SUFFIX_IEC_LONG => ['byte(s)', 'kibibyte(s)', 'mebibyte(s)', 'gibibyte(s)', 'tebibyte(s)', 'pebibyte(s)', 'exbibyte(s)', 'zebibyte(s)', 'yobibyte(s)'], |
||
52 | self::SUFFIX_SI => ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'], |
||
53 | self::SUFFIX_SI_LONG => ['byte(s)', 'kilobyte(s)', 'megabyte(s)', 'gigabyte(s)', 'terabyte(s)', 'petabyte(s)', 'exabyte(s)', 'zettabyte(s)', 'yottabyte(s)'], |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * @var int |
||
58 | */ |
||
59 | private $base = self::BASE_BINARY; |
||
60 | /** |
||
61 | * @var int |
||
62 | */ |
||
63 | private $suffix = self::SUFFIX_IEC; |
||
64 | /** |
||
65 | * @var int |
||
66 | */ |
||
67 | private $precision = 2; |
||
68 | /** |
||
69 | * @var bool |
||
70 | */ |
||
71 | private $precisionTrimming = true; |
||
72 | |||
73 | /** |
||
74 | * Create a new Bytes instance. |
||
75 | * |
||
76 | * @param int $base The base to use when converting to different units. Must be one of the `Bytes::BASE_*` |
||
77 | * constants. Optional, defaults to `BASE_BINARY`. |
||
78 | * @param int $suffix The suffix style for units. Must be one of the `Bytes::SUFFIX_*` constants. Optional, |
||
79 | * defaults to SUFFIX_IEC (KiB, MiB, etc.) |
||
80 | * |
||
81 | * @return \nochso\Omni\Bytes |
||
82 | */ |
||
83 | public static function create($base = self::BASE_BINARY, $suffix = self::SUFFIX_IEC) |
||
90 | |||
91 | /** |
||
92 | * setBase to use when converting to different units. |
||
93 | * |
||
94 | * @param int $base Must be one of the `Bytes::BASE_*` constants. |
||
95 | * |
||
96 | * @return $this |
||
97 | */ |
||
98 | public function setBase($base) |
||
106 | |||
107 | /** |
||
108 | * setSuffix style for units. |
||
109 | * |
||
110 | * @param int $suffix Must be one of the `Bytes::SUFFIX_*` constants. |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function setSuffix($suffix) |
||
122 | |||
123 | /** |
||
124 | * setPrecision of floating point values after the decimal point. |
||
125 | * |
||
126 | * @param int $precision Any non-negative integer. |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function setPrecision($precision) |
||
135 | |||
136 | /** |
||
137 | * enablePrecisionTrimming to remove trailing zeroes and decimal points. |
||
138 | * |
||
139 | * @return $this |
||
140 | */ |
||
141 | public function enablePrecisionTrimming() |
||
146 | |||
147 | /** |
||
148 | * disablePrecisionTrimming to keep trailing zeroes. |
||
149 | * |
||
150 | * @return $this |
||
151 | */ |
||
152 | public function disablePrecisionTrimming() |
||
157 | |||
158 | /** |
||
159 | * Format a quantity of bytes for human consumption. |
||
160 | * |
||
161 | * @param int $bytes |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | public function format($bytes) |
||
186 | |||
187 | /** |
||
188 | * @param string $number |
||
189 | * |
||
190 | * @return float |
||
191 | */ |
||
192 | private function trimPrecision($number) |
||
201 | } |
||
202 |