From 4a5a7f6065427ab59b7fccedaf8c053b0747f1f8 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 6 Oct 2024 17:07:06 +0200 Subject: [PATCH] Add method Format::mediaType() --- src/Format.php | 12 ++++++++++++ tests/Unit/FormatTest.php | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Format.php b/src/Format.php index 51246967..a9aa090a 100644 --- a/src/Format.php +++ b/src/Format.php @@ -91,6 +91,18 @@ enum Format }); } + /** + * Return the first found media type for the current format + * + * @return MediaType + */ + public function mediaType(): MediaType + { + $types = $this->mediaTypes(); + + return reset($types); + } + /** * Return the possible file extension for the current format * diff --git a/tests/Unit/FormatTest.php b/tests/Unit/FormatTest.php index 33bf2d87..711b1ebb 100644 --- a/tests/Unit/FormatTest.php +++ b/tests/Unit/FormatTest.php @@ -56,6 +56,8 @@ final class FormatTest extends BaseTestCase $mediaTypes = $format->mediaTypes(); $this->assertIsArray($mediaTypes); $this->assertCount(4, $mediaTypes); + + $this->assertEquals(MediaType::IMAGE_JPEG, $format->mediaType()); } public function testMediaTypesWebp(): void @@ -64,6 +66,8 @@ final class FormatTest extends BaseTestCase $mediaTypes = $format->mediaTypes(); $this->assertIsArray($mediaTypes); $this->assertCount(2, $mediaTypes); + + $this->assertEquals(MediaType::IMAGE_WEBP, $format->mediaType()); } public function testMediaTypesFGif(): void @@ -72,6 +76,8 @@ final class FormatTest extends BaseTestCase $mediaTypes = $format->mediaTypes(); $this->assertIsArray($mediaTypes); $this->assertCount(1, $mediaTypes); + + $this->assertEquals(MediaType::IMAGE_GIF, $format->mediaType()); } public function testMediaTypesPng(): void @@ -80,6 +86,8 @@ final class FormatTest extends BaseTestCase $mediaTypes = $format->mediaTypes(); $this->assertIsArray($mediaTypes); $this->assertCount(2, $mediaTypes); + + $this->assertEquals(MediaType::IMAGE_PNG, $format->mediaType()); } public function testMediaTypesAvif(): void @@ -88,6 +96,8 @@ final class FormatTest extends BaseTestCase $mediaTypes = $format->mediaTypes(); $this->assertIsArray($mediaTypes); $this->assertCount(2, $mediaTypes); + + $this->assertEquals(MediaType::IMAGE_AVIF, $format->mediaType()); } public function testMediaTypesBmp(): void @@ -96,6 +106,8 @@ final class FormatTest extends BaseTestCase $mediaTypes = $format->mediaTypes(); $this->assertIsArray($mediaTypes); $this->assertCount(8, $mediaTypes); + + $this->assertEquals(MediaType::IMAGE_BMP, $format->mediaType()); } public function testMediaTypesTiff(): void @@ -104,6 +116,8 @@ final class FormatTest extends BaseTestCase $mediaTypes = $format->mediaTypes(); $this->assertIsArray($mediaTypes); $this->assertCount(1, $mediaTypes); + + $this->assertEquals(MediaType::IMAGE_TIFF, $format->mediaType()); } public function testMediaTypesJpeg2000(): void @@ -112,6 +126,8 @@ final class FormatTest extends BaseTestCase $mediaTypes = $format->mediaTypes(); $this->assertIsArray($mediaTypes); $this->assertCount(3, $mediaTypes); + + $this->assertEquals(MediaType::IMAGE_JP2, $format->mediaType()); } public function testMediaTypesHeic(): void @@ -120,6 +136,8 @@ final class FormatTest extends BaseTestCase $mediaTypes = $format->mediaTypes(); $this->assertIsArray($mediaTypes); $this->assertCount(3, $mediaTypes); + + $this->assertEquals(MediaType::IMAGE_HEIC, $format->mediaType()); } public function testEncoderJpeg(): void