diff --git a/app/Enums/FileFormat.php b/app/Enums/FileFormat.php index cd4acc8..99a25ab 100644 --- a/app/Enums/FileFormat.php +++ b/app/Enums/FileFormat.php @@ -2,10 +2,29 @@ namespace App\Enums; +use App\Support\Converters\Exceptions\UnsupportedFormatException; + enum FileFormat: string { case Png = 'png'; case Jpg = 'jpg'; case Webp = 'webp'; case Pdf = 'pdf'; + + public static function normalize(string $input): string + { + $value = ltrim(strtolower(trim($input)), '.'); + + if ($value === 'jpeg') { + return self::Jpg->value; + } + + foreach (self::cases() as $case) { + if ($case->value === $value) { + return $case->value; + } + } + + throw UnsupportedFormatException::forInput($input); + } } diff --git a/app/Support/Converters/Exceptions/UnsupportedFormatException.php b/app/Support/Converters/Exceptions/UnsupportedFormatException.php new file mode 100644 index 0000000..a713773 --- /dev/null +++ b/app/Support/Converters/Exceptions/UnsupportedFormatException.php @@ -0,0 +1,13 @@ +