Skip to content

Commit

Permalink
refactor FileSizeUnit enum
Browse files Browse the repository at this point in the history
* reorder cases and function
* add DocBlock for return and throws
  • Loading branch information
ThomasMeschke committed Nov 19, 2024
1 parent f1464e3 commit 066c03d
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions system/Files/FileSizeUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,30 @@

namespace CodeIgniter\Files;

use 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"),
'b' => self::B,
'kb' => self::KB,
'mb' => self::MB,
'gb' => self::GB,
'tb' => self::TB,
default => throw new InvalidArgumentException("Invalid unit: {$unit}"),
};
}

case B = 0;
case KB = 1;
case MB = 2;
case GB = 3;
case TB = 4;
}

0 comments on commit 066c03d

Please sign in to comment.