mirror of
https://github.com/Intervention/image.git
synced 2025-08-20 04:31:24 +02:00
Implement create() & tryCreate() for FileExtension & MediaType
This commit is contained in:
@@ -4,6 +4,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Intervention\Image;
|
namespace Intervention\Image;
|
||||||
|
|
||||||
|
use Error;
|
||||||
|
use Intervention\Image\Exceptions\NotSupportedException;
|
||||||
|
|
||||||
enum FileExtension: string
|
enum FileExtension: string
|
||||||
{
|
{
|
||||||
case JPG = 'jpg';
|
case JPG = 'jpg';
|
||||||
@@ -27,6 +30,55 @@ enum FileExtension: string
|
|||||||
case HEIC = 'heic';
|
case HEIC = 'heic';
|
||||||
case HEIF = 'heif';
|
case HEIF = 'heif';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create file extension from given identifier
|
||||||
|
*
|
||||||
|
* @param string|Format|MediaType|FileExtension $identifier
|
||||||
|
* @throws NotSupportedException
|
||||||
|
* @return FileExtension
|
||||||
|
*/
|
||||||
|
public static function create(string|self|Format|MediaType $identifier): self
|
||||||
|
{
|
||||||
|
if ($identifier instanceof self) {
|
||||||
|
return $identifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($identifier instanceof Format) {
|
||||||
|
return $identifier->fileExtension();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($identifier instanceof MediaType) {
|
||||||
|
return $identifier->fileExtension();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$extension = FileExtension::from(strtolower($identifier));
|
||||||
|
} catch (Error) {
|
||||||
|
try {
|
||||||
|
$extension = MediaType::from(strtolower($identifier))->fileExtension();
|
||||||
|
} catch (Error) {
|
||||||
|
throw new NotSupportedException('Unable to create file extension from "' . $identifier . '".');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $extension;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try to create media type from given identifier and return null on failure
|
||||||
|
*
|
||||||
|
* @param string|Format|MediaType|FileExtension $identifier
|
||||||
|
* @return FileExtension|null
|
||||||
|
*/
|
||||||
|
public static function tryCreate(string|self|Format|MediaType $identifier): ?self
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return self::create($identifier);
|
||||||
|
} catch (NotSupportedException) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the matching format for the current file extension
|
* Return the matching format for the current file extension
|
||||||
*
|
*
|
||||||
|
@@ -4,6 +4,9 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Intervention\Image;
|
namespace Intervention\Image;
|
||||||
|
|
||||||
|
use Error;
|
||||||
|
use Intervention\Image\Exceptions\NotSupportedException;
|
||||||
|
|
||||||
enum MediaType: string
|
enum MediaType: string
|
||||||
{
|
{
|
||||||
case IMAGE_JPEG = 'image/jpeg';
|
case IMAGE_JPEG = 'image/jpeg';
|
||||||
@@ -35,6 +38,55 @@ enum MediaType: string
|
|||||||
case IMAGE_X_HEIC = 'image/x-heic';
|
case IMAGE_X_HEIC = 'image/x-heic';
|
||||||
case IMAGE_HEIF = 'image/heif';
|
case IMAGE_HEIF = 'image/heif';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create media type from given identifier
|
||||||
|
*
|
||||||
|
* @param string|Format|MediaType|FileExtension $identifier
|
||||||
|
* @throws NotSupportedException
|
||||||
|
* @return MediaType
|
||||||
|
*/
|
||||||
|
public static function create(string|self|Format|FileExtension $identifier): self
|
||||||
|
{
|
||||||
|
if ($identifier instanceof self) {
|
||||||
|
return $identifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($identifier instanceof Format) {
|
||||||
|
return $identifier->mediaType();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($identifier instanceof FileExtension) {
|
||||||
|
return $identifier->mediaType();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$type = MediaType::from(strtolower($identifier));
|
||||||
|
} catch (Error) {
|
||||||
|
try {
|
||||||
|
$type = FileExtension::from(strtolower($identifier))->mediaType();
|
||||||
|
} catch (Error) {
|
||||||
|
throw new NotSupportedException('Unable to create media type from "' . $identifier . '".');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try to create media type from given identifier and return null on failure
|
||||||
|
*
|
||||||
|
* @param string|Format|MediaType|FileExtension $identifier
|
||||||
|
* @return MediaType|null
|
||||||
|
*/
|
||||||
|
public static function tryCreate(string|self|Format|FileExtension $identifier): ?self
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return self::create($identifier);
|
||||||
|
} catch (NotSupportedException) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the matching format for the current media (MIME) type
|
* Return the matching format for the current media (MIME) type
|
||||||
*
|
*
|
||||||
|
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace Intervention\Image\Tests\Unit;
|
namespace Intervention\Image\Tests\Unit;
|
||||||
|
|
||||||
use Generator;
|
use Generator;
|
||||||
|
use Intervention\Image\Exceptions\NotSupportedException;
|
||||||
use Intervention\Image\FileExtension;
|
use Intervention\Image\FileExtension;
|
||||||
use Intervention\Image\Format;
|
use Intervention\Image\Format;
|
||||||
use Intervention\Image\MediaType;
|
use Intervention\Image\MediaType;
|
||||||
@@ -15,6 +16,33 @@ use PHPUnit\Framework\Attributes\DataProvider;
|
|||||||
#[CoversClass(FileExtension::class)]
|
#[CoversClass(FileExtension::class)]
|
||||||
final class FileExtensionTest extends BaseTestCase
|
final class FileExtensionTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
|
public function testCreate(): void
|
||||||
|
{
|
||||||
|
$this->assertEquals(FileExtension::JPG, FileExtension::create(MediaType::IMAGE_JPEG));
|
||||||
|
$this->assertEquals(FileExtension::JPG, FileExtension::create(Format::JPEG));
|
||||||
|
$this->assertEquals(FileExtension::JPG, FileExtension::create(FileExtension::JPG));
|
||||||
|
$this->assertEquals(FileExtension::JPG, FileExtension::create('jpg'));
|
||||||
|
$this->assertEquals(FileExtension::JPEG, FileExtension::create('jpeg'));
|
||||||
|
$this->assertEquals(FileExtension::JPG, FileExtension::create('image/jpeg'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateUnknown(): void
|
||||||
|
{
|
||||||
|
$this->expectException(NotSupportedException::class);
|
||||||
|
FileExtension::create('foo');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTryCreate(): void
|
||||||
|
{
|
||||||
|
$this->assertEquals(FileExtension::JPG, FileExtension::tryCreate(MediaType::IMAGE_JPEG));
|
||||||
|
$this->assertEquals(FileExtension::JPG, FileExtension::tryCreate(Format::JPEG));
|
||||||
|
$this->assertEquals(FileExtension::JPG, FileExtension::tryCreate(FileExtension::JPG));
|
||||||
|
$this->assertEquals(FileExtension::JPG, FileExtension::tryCreate('jpg'));
|
||||||
|
$this->assertEquals(FileExtension::JPEG, FileExtension::tryCreate('jpeg'));
|
||||||
|
$this->assertEquals(FileExtension::JPG, FileExtension::tryCreate('image/jpeg'));
|
||||||
|
$this->assertNull(FileExtension::tryCreate('no-format'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testFormatJpeg(): void
|
public function testFormatJpeg(): void
|
||||||
{
|
{
|
||||||
$ext = FileExtension::JPEG;
|
$ext = FileExtension::JPEG;
|
||||||
|
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||||||
namespace Intervention\Image\Tests\Unit;
|
namespace Intervention\Image\Tests\Unit;
|
||||||
|
|
||||||
use Generator;
|
use Generator;
|
||||||
|
use Intervention\Image\Exceptions\NotSupportedException;
|
||||||
use Intervention\Image\FileExtension;
|
use Intervention\Image\FileExtension;
|
||||||
use Intervention\Image\Format;
|
use Intervention\Image\Format;
|
||||||
use Intervention\Image\MediaType;
|
use Intervention\Image\MediaType;
|
||||||
@@ -15,6 +16,33 @@ use PHPUnit\Framework\Attributes\DataProvider;
|
|||||||
#[CoversClass(MediaType::class)]
|
#[CoversClass(MediaType::class)]
|
||||||
final class MediaTypeTest extends BaseTestCase
|
final class MediaTypeTest extends BaseTestCase
|
||||||
{
|
{
|
||||||
|
public function testCreate(): void
|
||||||
|
{
|
||||||
|
$this->assertEquals(MediaType::IMAGE_JPEG, MediaType::create(MediaType::IMAGE_JPEG));
|
||||||
|
$this->assertEquals(MediaType::IMAGE_JPEG, MediaType::create(Format::JPEG));
|
||||||
|
$this->assertEquals(MediaType::IMAGE_JPEG, MediaType::create(FileExtension::JPG));
|
||||||
|
$this->assertEquals(MediaType::IMAGE_JPEG, MediaType::create('jpg'));
|
||||||
|
$this->assertEquals(MediaType::IMAGE_JPEG, MediaType::create('jpeg'));
|
||||||
|
$this->assertEquals(MediaType::IMAGE_JPEG, MediaType::create('image/jpeg'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCreateUnknown(): void
|
||||||
|
{
|
||||||
|
$this->expectException(NotSupportedException::class);
|
||||||
|
MediaType::create('foo');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testTryCreate(): void
|
||||||
|
{
|
||||||
|
$this->assertEquals(MediaType::IMAGE_JPEG, MediaType::tryCreate(MediaType::IMAGE_JPEG));
|
||||||
|
$this->assertEquals(MediaType::IMAGE_JPEG, MediaType::tryCreate(Format::JPEG));
|
||||||
|
$this->assertEquals(MediaType::IMAGE_JPEG, MediaType::tryCreate(FileExtension::JPG));
|
||||||
|
$this->assertEquals(MediaType::IMAGE_JPEG, MediaType::tryCreate('jpg'));
|
||||||
|
$this->assertEquals(MediaType::IMAGE_JPEG, MediaType::tryCreate('jpeg'));
|
||||||
|
$this->assertEquals(MediaType::IMAGE_JPEG, MediaType::tryCreate('image/jpeg'));
|
||||||
|
$this->assertNull(Format::tryCreate('no-format'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testFormatJpeg(): void
|
public function testFormatJpeg(): void
|
||||||
{
|
{
|
||||||
$mime = MediaType::IMAGE_JPEG;
|
$mime = MediaType::IMAGE_JPEG;
|
||||||
|
Reference in New Issue
Block a user