mirror of
https://github.com/Intervention/image.git
synced 2025-08-29 00:29:55 +02:00
Add Format::tryCreate()
This commit is contained in:
@@ -64,6 +64,21 @@ enum Format
|
|||||||
return $format;
|
return $format;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Try to create format from given identifier and return null on failure
|
||||||
|
*
|
||||||
|
* @param string|Format|MediaType|FileExtension $identifier
|
||||||
|
* @return Format|null
|
||||||
|
*/
|
||||||
|
public static function tryCreate(string|self|MediaType|FileExtension $identifier): self|null
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
return self::create($identifier);
|
||||||
|
} catch (NotSupportedException) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the possible media (MIME) types for the current format
|
* Return the possible media (MIME) types for the current format
|
||||||
*
|
*
|
||||||
|
@@ -38,6 +38,18 @@ final class FormatTest extends BaseTestCase
|
|||||||
Format::create('foo');
|
Format::create('foo');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testTryCreate(): void
|
||||||
|
{
|
||||||
|
$this->assertEquals(Format::JPEG, Format::tryCreate(Format::JPEG));
|
||||||
|
$this->assertEquals(Format::JPEG, Format::tryCreate('jpg'));
|
||||||
|
$this->assertEquals(Format::JPEG, Format::tryCreate('jpeg'));
|
||||||
|
$this->assertEquals(Format::JPEG, Format::tryCreate('image/jpeg'));
|
||||||
|
$this->assertEquals(Format::GIF, Format::tryCreate('image/gif'));
|
||||||
|
$this->assertEquals(Format::PNG, Format::tryCreate(FileExtension::PNG));
|
||||||
|
$this->assertEquals(Format::WEBP, Format::tryCreate(MediaType::IMAGE_WEBP));
|
||||||
|
$this->assertNull(Format::tryCreate('no-format'));
|
||||||
|
}
|
||||||
|
|
||||||
public function testMediaTypesJpeg(): void
|
public function testMediaTypesJpeg(): void
|
||||||
{
|
{
|
||||||
$format = Format::JPEG;
|
$format = Format::JPEG;
|
||||||
|
Reference in New Issue
Block a user