13
13
14
14
class FileSize
15
15
{
16
+ /**
17
+ * The number base.
18
+ *
19
+ * @var int
20
+ */
21
+ private $ base ;
22
+
16
23
/**
17
24
* The number of bytes in this filesize.
18
25
*
@@ -32,10 +39,11 @@ class FileSize
32
39
*
33
40
* @param string|int $size Such as '100 MB'
34
41
*/
35
- public function __construct ($ size = null )
42
+ public function __construct ($ size = null , $ base = 2 )
36
43
{
37
44
$ this ->unitMapper = new UnitMapper ();
38
45
46
+ $ this ->base = $ base ;
39
47
$ this ->bytes = $ size ? $ this ->sizeToBytes ($ size ) : 0 ;
40
48
}
41
49
@@ -132,7 +140,7 @@ public function as($unitString, $precision = 2)
132
140
public function asAuto ($ precision = 2 )
133
141
{
134
142
$ factor = Math::factorByBytes ($ this ->bytes );
135
- $ size = $ this ->bytes / Math::bytesByFactor ($ factor );
143
+ $ size = $ this ->bytes / Math::bytesByFactor ($ factor, $ this -> base );
136
144
$ unit = $ this ->unitMapper ->keyFromIndex ($ factor );
137
145
138
146
return self ::formatNumber ($ size , $ precision , $ unit );
@@ -155,7 +163,8 @@ private function convert($size, $fromUnit, $toUnit, $precision = null)
155
163
if ($ fromUnit !== $ toUnit ) {
156
164
$ index1 = $ this ->unitMapper ->indexFromKey ($ fromUnit );
157
165
$ index2 = $ this ->unitMapper ->indexFromKey ($ toUnit );
158
- $ size = (float ) $ size * Math::bytesByFactor ($ index1 - $ index2 );
166
+ $ factor = $ index1 - $ index2 ;
167
+ $ size = (float ) $ size * Math::bytesByFactor ($ factor , $ this ->base );
159
168
}
160
169
161
170
if ($ toUnit === UnitMap::BYTE ) {
0 commit comments