Skip to content

Commit

Permalink
Add MediaType::fileExtensions() & MediaType::fileExtension()
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Oct 6, 2024
1 parent 20ad74e commit 1e0a2b6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/MediaType.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,24 @@ public function format(): Format
self::IMAGE_X_HEIC => Format::HEIC,
};
}

/**
* Return the possible file extension for the current media type
*
* @return array<FileExtension>
*/
public function fileExtensions(): array
{
return $this->format()->fileExtensions();
}

/**
* Return the first file extension for the current media type
*
* @return FileExtension
*/
public function fileExtension(): FileExtension
{
return $this->format()->fileExtension();
}
}
44 changes: 44 additions & 0 deletions tests/Unit/MediaTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Intervention\Image\Tests\Unit;

use Intervention\Image\FileExtension;
use Intervention\Image\Format;
use Intervention\Image\MediaType;
use Intervention\Image\Tests\BaseTestCase;
use PHPUnit\Framework\Attributes\DataProvider;

final class MediaTypeTest extends BaseTestCase
{
Expand Down Expand Up @@ -105,4 +107,46 @@ public function testFormatHeic(): void
$mime = MediaType::IMAGE_HEIF;
$this->assertEquals(Format::HEIC, $mime->format());
}

#[DataProvider('fileExtensionsDataProvider')]
public function testFileExtensions(
MediaType $mediaType,
int $fileExtensionCount,
FileExtension $fileExtension
): void {
$this->assertCount($fileExtensionCount, $mediaType->fileExtensions());
$this->assertEquals($fileExtension, $mediaType->fileExtension());
}

public static function fileExtensionsDataProvider(): array
{
return [
[MediaType::IMAGE_JPEG, 2, FileExtension::JPG],
[MediaType::IMAGE_JPG, 2, FileExtension::JPG],
[MediaType::IMAGE_PJPEG, 2, FileExtension::JPG],
[MediaType::IMAGE_X_JPEG, 2, FileExtension::JPG],
[MediaType::IMAGE_WEBP, 1, FileExtension::WEBP],
[MediaType::IMAGE_X_WEBP, 1, FileExtension::WEBP],
[MediaType::IMAGE_GIF, 1, FileExtension::GIF],
[MediaType::IMAGE_PNG, 1, FileExtension::PNG],
[MediaType::IMAGE_X_PNG, 1, FileExtension::PNG],
[MediaType::IMAGE_AVIF, 1, FileExtension::AVIF],
[MediaType::IMAGE_X_AVIF, 1, FileExtension::AVIF],
[MediaType::IMAGE_BMP, 1, FileExtension::BMP],
[MediaType::IMAGE_MS_BMP, 1, FileExtension::BMP],
[MediaType::IMAGE_X_BITMAP, 1, FileExtension::BMP],
[MediaType::IMAGE_X_BMP, 1, FileExtension::BMP],
[MediaType::IMAGE_X_MS_BMP, 1, FileExtension::BMP],
[MediaType::IMAGE_X_WINDOWS_BMP, 1, FileExtension::BMP],
[MediaType::IMAGE_X_WIN_BITMAP, 1, FileExtension::BMP],
[MediaType::IMAGE_X_XBITMAP, 1, FileExtension::BMP],
[MediaType::IMAGE_TIFF, 2, FileExtension::TIF],
[MediaType::IMAGE_JP2, 8, FileExtension::JP2],
[MediaType::IMAGE_JPX, 8, FileExtension::JP2],
[MediaType::IMAGE_JPM, 8, FileExtension::JP2],
[MediaType::IMAGE_HEIC, 2, FileExtension::HEIC],
[MediaType::IMAGE_X_HEIC, 2, FileExtension::HEIC],
[MediaType::IMAGE_HEIF, 2, FileExtension::HEIC],
];
}
}

0 comments on commit 1e0a2b6

Please sign in to comment.