-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Differentiate between kilobyte/kibibyte and megabyte/mebibyte (#…
- Loading branch information
1 parent
35c5784
commit 455a559
Showing
7 changed files
with
190 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of CodeIgniter 4 framework. | ||
* | ||
* (c) CodeIgniter Foundation <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CodeIgniter\Files; | ||
|
||
use CodeIgniter\Exceptions\InvalidArgumentException; | ||
|
||
enum FileSizeUnit: int | ||
{ | ||
case B = 0; | ||
case KB = 1; | ||
case MB = 2; | ||
case GB = 3; | ||
case TB = 4; | ||
|
||
/** | ||
* Allows the creation of a FileSizeUnit from Strings like "kb" or "mb" | ||
* | ||
* @throws InvalidArgumentException | ||
*/ | ||
public static function fromString(string $unit): self | ||
{ | ||
return match (strtolower($unit)) { | ||
'b' => self::B, | ||
'kb' => self::KB, | ||
'mb' => self::MB, | ||
'gb' => self::GB, | ||
'tb' => self::TB, | ||
default => throw new InvalidArgumentException("Invalid unit: {$unit}"), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
use CodeIgniter\Files\FileSizeUnit; | ||
|
||
$bytes = $file->getSizeByBinaryUnit(); // 256901 | ||
$kibibytes = $file->getSizeByBinaryUnit(FileSizeUnit::KB); // 250.880 | ||
$mebibytes = $file->getSizeByBinaryUnit(FileSizeUnit::MB); // 0.245 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
use CodeIgniter\Files\FileSizeUnit; | ||
|
||
$bytes = $file->getSizeByMetricUnit(); // 256901 | ||
$kilobytes = $file->getSizeByMetricUnit(FileSizeUnit::KB); // 256.901 | ||
$megabytes = $file->getSizeByMetricUnit(FileSizeUnit::MB); // 0.256 |