File tree Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Expand file tree Collapse file tree 2 files changed +22
-4
lines changed Original file line number Diff line number Diff line change @@ -72,14 +72,18 @@ public function getSize()
72
72
73
73
/**
74
74
* Retrieve the file size by unit.
75
+ * Supports the metric units "kb" and "mb"
76
+ * and the IEC units "kib" and "mib".
75
77
*
76
78
* @return false|int|string
77
79
*/
78
80
public function getSizeByUnit (string $ unit = 'b ' )
79
81
{
80
82
return match (strtolower ($ unit )) {
81
- 'kb ' => number_format ($ this ->getSize () / 1024 , 3 ),
82
- 'mb ' => number_format (($ this ->getSize () / 1024 ) / 1024 , 3 ),
83
+ 'kb ' => number_format ($ this ->getSize () / 1000 , 3 ),
84
+ 'kib ' => number_format ($ this ->getSize () / 1024 , 3 ),
85
+ 'mb ' => number_format (($ this ->getSize () / 1000 ) / 1000 , 3 ),
86
+ 'mib ' => number_format (($ this ->getSize () / 1024 ) / 1024 , 3 ),
83
87
default => $ this ->getSize (),
84
88
};
85
89
}
Original file line number Diff line number Diff line change @@ -92,17 +92,31 @@ public function testCanAccessSplFileInfoMethods(): void
92
92
$ this ->assertSame ('file ' , $ file ->getType ());
93
93
}
94
94
95
- public function testGetSizeReturnsKB (): void
95
+ public function testGetSizeReturnsKiB (): void
96
96
{
97
97
$ file = new File (SYSTEMPATH . 'Common.php ' );
98
98
$ size = number_format (filesize (SYSTEMPATH . 'Common.php ' ) / 1024 , 3 );
99
+ $ this ->assertSame ($ size , $ file ->getSizeByUnit ('kib ' ));
100
+ }
101
+
102
+ public function testGetSizeReturnsKB (): void
103
+ {
104
+ $ file = new File (SYSTEMPATH . 'Common.php ' );
105
+ $ size = number_format (filesize (SYSTEMPATH . 'Common.php ' ) / 1000 , 3 );
99
106
$ this ->assertSame ($ size , $ file ->getSizeByUnit ('kb ' ));
100
107
}
101
108
102
- public function testGetSizeReturnsMB (): void
109
+ public function testGetSizeReturnsMiB (): void
103
110
{
104
111
$ file = new File (SYSTEMPATH . 'Common.php ' );
105
112
$ size = number_format (filesize (SYSTEMPATH . 'Common.php ' ) / 1024 / 1024 , 3 );
113
+ $ this ->assertSame ($ size , $ file ->getSizeByUnit ('mib ' ));
114
+ }
115
+
116
+ public function testGetSizeReturnsMB (): void
117
+ {
118
+ $ file = new File (SYSTEMPATH . 'Common.php ' );
119
+ $ size = number_format (filesize (SYSTEMPATH . 'Common.php ' ) / 1000 / 1000 , 3 );
106
120
$ this ->assertSame ($ size , $ file ->getSizeByUnit ('mb ' ));
107
121
}
108
122
You can’t perform that action at this time.
0 commit comments