Skip to content

Commit

Permalink
refactor: differentiate between kilo/kibibytes and mega/mebibytes
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasMeschke committed Nov 14, 2024
1 parent fff0c87 commit 55cd710
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
8 changes: 6 additions & 2 deletions system/Files/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ public function getSize()

/**
* Retrieve the file size by unit.
* Supports the metric units "kb" and "mb"
* and the IEC units "kib" and "mib".
*
* @return false|int|string
*/
public function getSizeByUnit(string $unit = 'b')
{
return match (strtolower($unit)) {
'kb' => number_format($this->getSize() / 1024, 3),
'mb' => number_format(($this->getSize() / 1024) / 1024, 3),
'kb' => number_format($this->getSize() / 1000, 3),
'kib' => number_format($this->getSize() / 1024, 3),
'mb' => number_format(($this->getSize() / 1000) / 1000, 3),
'mib' => number_format(($this->getSize() / 1024) / 1024, 3),
default => $this->getSize(),
};
}
Expand Down
18 changes: 16 additions & 2 deletions tests/system/Files/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,31 @@ public function testCanAccessSplFileInfoMethods(): void
$this->assertSame('file', $file->getType());
}

public function testGetSizeReturnsKB(): void
public function testGetSizeReturnsKiB(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1024, 3);
$this->assertSame($size, $file->getSizeByUnit('kib'));
}

public function testGetSizeReturnsKB(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1000, 3);
$this->assertSame($size, $file->getSizeByUnit('kb'));
}

public function testGetSizeReturnsMB(): void
public function testGetSizeReturnsMiB(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1024 / 1024, 3);
$this->assertSame($size, $file->getSizeByUnit('mib'));
}

public function testGetSizeReturnsMB(): void
{
$file = new File(SYSTEMPATH . 'Common.php');
$size = number_format(filesize(SYSTEMPATH . 'Common.php') / 1000 / 1000, 3);
$this->assertSame($size, $file->getSizeByUnit('mb'));
}

Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/libraries/files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ getSizeByUnit()
===============

Returns the size of the file default in bytes. You can pass in either ``'kb'`` or ``'mb'`` as the first parameter to get
the results in kilobytes or megabytes, respectively:
the results in kibibytes or mebibytes, or you can pass ``'kb'`` or ``'mb'`` to get the result in kilobytes or megabytes, respectively:

.. literalinclude:: files/005.php
:lines: 2-
Expand Down
6 changes: 4 additions & 2 deletions user_guide_src/source/libraries/files/005.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

$bytes = $file->getSizeByUnit(); // 256901
$kilobytes = $file->getSizeByUnit('kb'); // 250.880
$megabytes = $file->getSizeByUnit('mb'); // 0.245
$kilobytes = $file->getSizeByUnit('kb'); // 256.901
$kilobytes = $file->getSizeByUnit('kib'); // 250.880
$megabytes = $file->getSizeByUnit('mb'); // 0.256
$megabytes = $file->getSizeByUnit('mib'); // 0.245

0 comments on commit 55cd710

Please sign in to comment.