1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-11 16:34:00 +02:00

Replace method AbstractDecoder::fail()

This commit is contained in:
Oliver Vogel
2022-05-22 17:42:12 +02:00
parent 190f46fc12
commit 58585c81f3
19 changed files with 53 additions and 51 deletions

View File

@@ -22,7 +22,7 @@ abstract class AbstractDecoder implements DecoderInterface
$decoded = $this->decode($input);
} catch (DecoderException $e) {
if (!$this->hasSuccessor()) {
$this->fail($e->getMessage());
throw new DecoderException($e->getMessage());
}
return $this->successor->handle($input);
@@ -36,11 +36,6 @@ abstract class AbstractDecoder implements DecoderInterface
return $this->successor !== null;
}
protected function fail(string $message = 'Unable to decode given input.'): void
{
throw new DecoderException($message);
}
protected function inputType($input): AbstractType
{
return MimeSniffer::createFromString($input)->getType();

View File

@@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Drivers\Gd\Color;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
@@ -16,7 +17,7 @@ class ArrayColorDecoder extends AbstractDecoder implements DecoderInterface
public function decode($input): ImageInterface|ColorInterface
{
if (! $this->isValidColorArray($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
if (count($input) === 3) {

View File

@@ -2,6 +2,7 @@
namespace Intervention\Image\Drivers\Gd\Decoders;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
@@ -14,7 +15,7 @@ class Base64ImageDecoder extends BinaryImageDecoder implements DecoderInterface
public function decode($input): ImageInterface|ColorInterface
{
if (! $this->isValidBase64($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
return parent::decode(base64_decode($input));

View File

@@ -2,7 +2,6 @@
namespace Intervention\Image\Drivers\Gd\Decoders;
use GdImage;
use Intervention\Image\Collection;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Drivers\Gd\Frame;
@@ -10,21 +9,21 @@ use Intervention\Image\Drivers\Gd\Image;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\MimeSniffer\MimeSniffer;
use Intervention\MimeSniffer\Types\ImageGif;
use Intervention\Gif\Decoder as GifDecoder;
use Intervention\Gif\Splitter as GifSplitter;
use Intervention\Image\Exceptions\DecoderException;
class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
{
public function decode($input): ImageInterface|ColorInterface
{
if (!is_string($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
if (! $this->inputType($input)->isBinary()) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
if (is_a($this->inputType($input), ImageGif::class)) {
@@ -34,7 +33,7 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
$gd = @imagecreatefromstring($input);
if ($gd === false) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
if (! imageistruecolor($gd)) {

View File

@@ -2,11 +2,11 @@
namespace Intervention\Image\Drivers\Gd\Decoders;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Traits\CanDecodeDataUri;
use Intervention\MimeSniffer\MimeSniffer;
class DataUriImageDecoder extends BinaryImageDecoder implements DecoderInterface
{
@@ -15,13 +15,13 @@ class DataUriImageDecoder extends BinaryImageDecoder implements DecoderInterface
public function decode($input): ImageInterface|ColorInterface
{
if (!is_string($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
$uri = $this->decodeDataUri($input);
if (! $uri->isValid()) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
if ($uri->isBase64Encoded()) {

View File

@@ -3,6 +3,7 @@
namespace Intervention\Image\Drivers\Gd\Decoders;
use Exception;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
@@ -12,15 +13,15 @@ class FilePathImageDecoder extends BinaryImageDecoder implements DecoderInterfac
public function decode($input): ImageInterface|ColorInterface
{
if (! is_string($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
try {
if (! @is_file($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
} catch (Exception $e) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
return parent::decode(file_get_contents($input));

View File

@@ -2,7 +2,7 @@
namespace Intervention\Image\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\Gd\Color;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
@@ -12,14 +12,14 @@ class HexColorDecoder extends ArrayColorDecoder implements DecoderInterface
public function decode($input): ImageInterface|ColorInterface
{
if (!is_string($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
$pattern = '/^#?([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{1,2})$/i';
$result = preg_match($pattern, $input, $matches);
if ($result !== 1) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
return parent::decode([

View File

@@ -3,6 +3,7 @@
namespace Intervention\Image\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
@@ -12,7 +13,7 @@ class ImageObjectDecoder extends AbstractDecoder implements DecoderInterface
public function decode($input): ImageInterface|ColorInterface
{
if (! is_a($input, ImageInterface::class)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
return $input;

View File

@@ -2,6 +2,7 @@
namespace Intervention\Image\Drivers\Gd\Decoders;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
@@ -11,11 +12,11 @@ class RgbStringColorDecoder extends ArrayColorDecoder implements DecoderInterfac
public function decode($input): ImageInterface|ColorInterface
{
if (!is_string($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
if (substr($input, 0, 3) !== 'rgb') {
$this->fail();
throw new DecoderException('Unable to decode input');
}
// rgb string like rgb(102, 200, 0)
@@ -30,6 +31,6 @@ class RgbStringColorDecoder extends ArrayColorDecoder implements DecoderInterfac
return parent::decode([$matches['r'], $matches['g'], $matches['b'], $matches['a']]);
}
$this->fail();
throw new DecoderException('Unable to decode input');
}
}

View File

@@ -2,18 +2,17 @@
namespace Intervention\Image\Drivers\Gd\Decoders;
use Intervention\Image\Drivers\Gd\Color;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Traits\CanValidateColors;
class TransparentColorDecoder extends ArrayColorDecoder implements DecoderInterface
{
public function decode($input): ImageInterface|ColorInterface
{
if (! is_string($input) || strtolower($input) !== 'transparent') {
$this->fail();
throw new DecoderException('Unable to decode input');
}
return parent::decode([0, 0, 0, 0]);

View File

@@ -5,6 +5,7 @@ namespace Intervention\Image\Drivers\Imagick\Decoders;
use ImagickPixel;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
@@ -17,7 +18,7 @@ class ArrayColorDecoder extends AbstractDecoder implements DecoderInterface
public function decode($input): ImageInterface|ColorInterface
{
if (! $this->isValidColorArray($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
if (count($input) === 3) {

View File

@@ -2,6 +2,7 @@
namespace Intervention\Image\Drivers\Imagick\Decoders;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
@@ -14,7 +15,7 @@ class Base64ImageDecoder extends BinaryImageDecoder implements DecoderInterface
public function decode($input): ImageInterface|ColorInterface
{
if (! $this->isValidBase64($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
return parent::decode(base64_decode($input));

View File

@@ -7,6 +7,7 @@ use Intervention\Image\Collection;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Drivers\Imagick\Frame;
use Intervention\Image\Drivers\Imagick\Image;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
@@ -16,11 +17,11 @@ class BinaryImageDecoder extends AbstractDecoder implements DecoderInterface
public function decode($input): ImageInterface|ColorInterface
{
if (!is_string($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
if (! $this->inputType($input)->isBinary()) {
$this->fail();
if (!$this->inputType($input)->isBinary()) {
throw new DecoderException('Unable to decode input');
}
$imagick = new Imagick();

View File

@@ -2,11 +2,11 @@
namespace Intervention\Image\Drivers\Imagick\Decoders;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Traits\CanDecodeDataUri;
use Intervention\MimeSniffer\MimeSniffer;
class DataUriImageDecoder extends BinaryImageDecoder implements DecoderInterface
{
@@ -15,13 +15,13 @@ class DataUriImageDecoder extends BinaryImageDecoder implements DecoderInterface
public function decode($input): ImageInterface|ColorInterface
{
if (!is_string($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
$uri = $this->decodeDataUri($input);
if (! $uri->isValid()) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
if ($uri->isBase64Encoded()) {

View File

@@ -3,6 +3,7 @@
namespace Intervention\Image\Drivers\Imagick\Decoders;
use Exception;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
@@ -12,15 +13,15 @@ class FilePathImageDecoder extends BinaryImageDecoder implements DecoderInterfac
public function decode($input): ImageInterface|ColorInterface
{
if (! is_string($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
try {
if (! @is_file($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
} catch (Exception $e) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
return parent::decode(file_get_contents($input));

View File

@@ -2,7 +2,7 @@
namespace Intervention\Image\Drivers\Imagick\Decoders;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
@@ -12,14 +12,14 @@ class HexColorDecoder extends ArrayColorDecoder implements DecoderInterface
public function decode($input): ImageInterface|ColorInterface
{
if (!is_string($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
$pattern = '/^#?([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{1,2})$/i';
$result = preg_match($pattern, $input, $matches);
if ($result !== 1) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
return parent::decode([

View File

@@ -3,6 +3,7 @@
namespace Intervention\Image\Drivers\Imagick\Decoders;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
@@ -12,7 +13,7 @@ class ImageObjectDecoder extends AbstractDecoder implements DecoderInterface
public function decode($input): ImageInterface|ColorInterface
{
if (! is_a($input, ImageInterface::class)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
return $input;

View File

@@ -6,27 +6,27 @@ use ImagickPixel;
use ImagickPixelException;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Traits\CanValidateColors;
class RgbStringColorDecoder extends AbstractDecoder implements DecoderInterface
{
public function decode($input): ImageInterface|ColorInterface
{
if (!is_string($input)) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
if (substr($input, 0, 3) !== 'rgb') {
$this->fail();
throw new DecoderException('Unable to decode input');
}
try {
$pixel = new ImagickPixel($input);
} catch (ImagickPixelException $e) {
$this->fail();
throw new DecoderException('Unable to decode input');
}
return new Color($pixel);

View File

@@ -2,18 +2,17 @@
namespace Intervention\Image\Drivers\Imagick\Decoders;
use Intervention\Image\Drivers\Imagick\Color;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Traits\CanValidateColors;
class TransparentColorDecoder extends ArrayColorDecoder implements DecoderInterface
{
public function decode($input): ImageInterface|ColorInterface
{
if (! is_string($input) || strtolower($input) !== 'transparent') {
$this->fail();
throw new DecoderException('Unable to decode input');
}
return parent::decode([0, 0, 0, 0]);