From 25572c0ef9f694c3acfbc97173b48a294f5a5656 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sat, 30 Sep 2023 11:30:03 +0200 Subject: [PATCH] Refactor checking for driver objects --- src/Drivers/Abstract/AbstractFont.php | 2 ++ src/Drivers/Abstract/AbstractTextWriter.php | 3 ++ .../Modifiers/AbstractDrawModifier.php | 2 ++ .../Modifiers/AbstractPadModifier.php | 3 ++ .../Modifiers/AbstractRotateModifier.php | 2 ++ src/Drivers/Gd/Modifiers/TextWriter.php | 27 +++++----------- src/Drivers/Imagick/Font.php | 16 ++-------- .../Imagick/Modifiers/DrawEllipseModifier.php | 31 ++++--------------- .../Imagick/Modifiers/DrawLineModifier.php | 4 ++- .../Imagick/Modifiers/DrawPixelModifier.php | 20 +++++------- .../Imagick/Modifiers/DrawPolygonModifier.php | 30 +++--------------- .../Modifiers/DrawRectangleModifier.php | 30 ++++-------------- src/Drivers/Imagick/Modifiers/PadModifier.php | 7 +---- .../Imagick/Modifiers/RotateModifier.php | 6 +--- src/Drivers/Imagick/Modifiers/TextWriter.php | 15 +++------ src/Traits/CanCheckType.php | 17 ++++++++++ src/Traits/CanHandleInput.php | 11 +++++-- .../Modifiers/AbstractRotateModifierTest.php | 2 +- 18 files changed, 82 insertions(+), 146 deletions(-) create mode 100644 src/Traits/CanCheckType.php diff --git a/src/Drivers/Abstract/AbstractFont.php b/src/Drivers/Abstract/AbstractFont.php index 9c8784ce..dd39739c 100644 --- a/src/Drivers/Abstract/AbstractFont.php +++ b/src/Drivers/Abstract/AbstractFont.php @@ -4,11 +4,13 @@ namespace Intervention\Image\Drivers\Abstract; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\FontInterface; +use Intervention\Image\Traits\CanCheckType; use Intervention\Image\Traits\CanHandleInput; abstract class AbstractFont implements FontInterface { use CanHandleInput; + use CanCheckType; protected $size = 12; protected $angle = 0; diff --git a/src/Drivers/Abstract/AbstractTextWriter.php b/src/Drivers/Abstract/AbstractTextWriter.php index 502f4172..29078bd7 100644 --- a/src/Drivers/Abstract/AbstractTextWriter.php +++ b/src/Drivers/Abstract/AbstractTextWriter.php @@ -5,10 +5,13 @@ namespace Intervention\Image\Drivers\Abstract; use Intervention\Image\Geometry\Point; use Intervention\Image\Interfaces\FontInterface; use Intervention\Image\Interfaces\ModifierInterface; +use Intervention\Image\Traits\CanCheckType; use Intervention\Image\Typography\TextBlock; abstract class AbstractTextWriter implements ModifierInterface { + use CanCheckType; + public function __construct( protected Point $position, protected FontInterface $font, diff --git a/src/Drivers/Abstract/Modifiers/AbstractDrawModifier.php b/src/Drivers/Abstract/Modifiers/AbstractDrawModifier.php index 42e89eff..50d67c0a 100644 --- a/src/Drivers/Abstract/Modifiers/AbstractDrawModifier.php +++ b/src/Drivers/Abstract/Modifiers/AbstractDrawModifier.php @@ -11,11 +11,13 @@ use Intervention\Image\Geometry\Rectangle; use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\DrawableInterface; use Intervention\Image\Interfaces\PointInterface; +use Intervention\Image\Traits\CanCheckType; use Intervention\Image\Traits\CanHandleInput; class AbstractDrawModifier { use CanHandleInput; + use CanCheckType; public function __construct( protected PointInterface $position, diff --git a/src/Drivers/Abstract/Modifiers/AbstractPadModifier.php b/src/Drivers/Abstract/Modifiers/AbstractPadModifier.php index 521dab30..0ae8e725 100644 --- a/src/Drivers/Abstract/Modifiers/AbstractPadModifier.php +++ b/src/Drivers/Abstract/Modifiers/AbstractPadModifier.php @@ -5,9 +5,12 @@ namespace Intervention\Image\Drivers\Abstract\Modifiers; use Intervention\Image\Geometry\Rectangle; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\SizeInterface; +use Intervention\Image\Traits\CanCheckType; abstract class AbstractPadModifier { + use CanCheckType; + public function __construct( protected int $width, protected int $height, diff --git a/src/Drivers/Abstract/Modifiers/AbstractRotateModifier.php b/src/Drivers/Abstract/Modifiers/AbstractRotateModifier.php index 016211e3..113a4ba1 100644 --- a/src/Drivers/Abstract/Modifiers/AbstractRotateModifier.php +++ b/src/Drivers/Abstract/Modifiers/AbstractRotateModifier.php @@ -5,11 +5,13 @@ namespace Intervention\Image\Drivers\Abstract\Modifiers; use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Exceptions\TypeException; use Intervention\Image\Interfaces\ColorInterface; +use Intervention\Image\Traits\CanCheckType; use Intervention\Image\Traits\CanHandleInput; abstract class AbstractRotateModifier { use CanHandleInput; + use CanCheckType; public function __construct(protected float $angle, protected $background) { diff --git a/src/Drivers/Gd/Modifiers/TextWriter.php b/src/Drivers/Gd/Modifiers/TextWriter.php index 8a404b44..fd117bca 100644 --- a/src/Drivers/Gd/Modifiers/TextWriter.php +++ b/src/Drivers/Gd/Modifiers/TextWriter.php @@ -4,8 +4,6 @@ namespace Intervention\Image\Drivers\Gd\Modifiers; use Intervention\Image\Drivers\Abstract\AbstractTextWriter; use Intervention\Image\Drivers\Gd\Font; -use Intervention\Image\Exceptions\FontException; -use Intervention\Image\Interfaces\FontInterface; use Intervention\Image\Interfaces\ImageInterface; class TextWriter extends AbstractTextWriter @@ -13,30 +11,27 @@ class TextWriter extends AbstractTextWriter public function apply(ImageInterface $image): ImageInterface { $lines = $this->getAlignedTextBlock(); + $font = $this->failIfNotClass($this->getFont(), Font::class); + foreach ($image as $frame) { if ($this->font->hasFilename()) { foreach ($lines as $line) { imagettftext( $frame->getCore(), - $this->getFont()->getSize(), - $this->getFont()->getAngle() * (-1), + $font->getSize(), + $font->getAngle() * (-1), $line->getPosition()->getX(), $line->getPosition()->getY(), - $this->getFont()->getColor()->toInt(), - $this->getFont()->getFilename(), + $font->getColor()->toInt(), + $font->getFilename(), $line ); } - - // debug - // $lines = new TextBlock($this->text); - // $box = $lines->getBoundingBox($this->font, $this->position); - // imagepolygon($frame->getCore(), $box->toArray(), 0); } else { foreach ($lines as $line) { imagestring( $frame->getCore(), - $this->getFont()->getGdFont(), + $font->getGdFont(), $line->getPosition()->getX(), $line->getPosition()->getY(), $line, @@ -48,12 +43,4 @@ class TextWriter extends AbstractTextWriter return $image; } - - protected function getFont(): FontInterface - { - if (!is_a($this->font, Font::class)) { - throw new FontException('Font is not compatible to current driver.'); - } - return $this->font; - } } diff --git a/src/Drivers/Imagick/Font.php b/src/Drivers/Imagick/Font.php index 8e2394af..2e8441bf 100644 --- a/src/Drivers/Imagick/Font.php +++ b/src/Drivers/Imagick/Font.php @@ -8,7 +8,6 @@ use Intervention\Image\Drivers\Abstract\AbstractFont; use Intervention\Image\Exceptions\FontException; use Intervention\Image\Geometry\Polygon; use Intervention\Image\Geometry\Rectangle; -use Intervention\Image\Interfaces\ColorInterface; class Font extends AbstractFont { @@ -18,28 +17,19 @@ class Font extends AbstractFont throw new FontException('No font file specified.'); } + $color = $this->failIfNotClass($this->getColor(), Color::class); + $draw = new ImagickDraw(); $draw->setStrokeAntialias(true); $draw->setTextAntialias(true); $draw->setFont($this->getFilename()); $draw->setFontSize($this->getSize()); - $draw->setFillColor($this->getColor()->getPixel()); + $draw->setFillColor($color->getPixel()); $draw->setTextAlignment(Imagick::ALIGN_LEFT); return $draw; } - public function getColor(): ?ColorInterface - { - $color = parent::getColor(); - - if (!is_a($color, Color::class)) { - throw new FontException('Font is not compatible to current driver.'); - } - - return $color; - } - /** * Calculate box size of current font * diff --git a/src/Drivers/Imagick/Modifiers/DrawEllipseModifier.php b/src/Drivers/Imagick/Modifiers/DrawEllipseModifier.php index b0ef639d..289500c2 100644 --- a/src/Drivers/Imagick/Modifiers/DrawEllipseModifier.php +++ b/src/Drivers/Imagick/Modifiers/DrawEllipseModifier.php @@ -5,8 +5,6 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers; use ImagickDraw; use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier; use Intervention\Image\Drivers\Imagick\Color; -use Intervention\Image\Exceptions\DecoderException; -use Intervention\Image\Interfaces\ColorInterface; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ModifierInterface; @@ -14,13 +12,16 @@ class DrawEllipseModifier extends AbstractDrawModifier implements ModifierInterf { public function apply(ImageInterface $image): ImageInterface { - return $image->eachFrame(function ($frame) { + $background_color = $this->failIfNotClass($this->getBackgroundColor(), Color::class); + $border_color = $this->failIfNotClass($this->getBorderColor(), Color::class); + + return $image->eachFrame(function ($frame) use ($background_color, $border_color) { $drawing = new ImagickDraw(); - $drawing->setFillColor($this->getBackgroundColor()->getPixel()); + $drawing->setFillColor($background_color->getPixel()); if ($this->ellipse()->hasBorder()) { $drawing->setStrokeWidth($this->ellipse()->getBorderSize()); - $drawing->setStrokeColor($this->getBorderColor()->getPixel()); + $drawing->setStrokeColor($border_color->getPixel()); } $drawing->ellipse( @@ -35,24 +36,4 @@ class DrawEllipseModifier extends AbstractDrawModifier implements ModifierInterf $frame->getCore()->drawImage($drawing); }); } - - protected function getBackgroundColor(): ColorInterface - { - $color = parent::getBackgroundColor(); - if (!is_a($color, Color::class)) { - throw new DecoderException('Unable to decode background color.'); - } - - return $color; - } - - protected function getBorderColor(): ColorInterface - { - $color = parent::getBorderColor(); - if (!is_a($color, Color::class)) { - throw new DecoderException('Unable to decode border color.'); - } - - return $color; - } } diff --git a/src/Drivers/Imagick/Modifiers/DrawLineModifier.php b/src/Drivers/Imagick/Modifiers/DrawLineModifier.php index 0cd6fcd8..17c4ac76 100644 --- a/src/Drivers/Imagick/Modifiers/DrawLineModifier.php +++ b/src/Drivers/Imagick/Modifiers/DrawLineModifier.php @@ -4,6 +4,7 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers; use ImagickDraw; use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier; +use Intervention\Image\Drivers\Imagick\Color; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ModifierInterface; @@ -12,7 +13,8 @@ class DrawLineModifier extends AbstractDrawModifier implements ModifierInterface public function apply(ImageInterface $image): ImageInterface { $drawing = new ImagickDraw(); - $drawing->setStrokeColor($this->getBackgroundColor()->getPixel()); + $color = $this->failIfNotClass($this->getBackgroundColor(), Color::class); + $drawing->setStrokeColor($color->getPixel()); $drawing->setStrokeWidth($this->line()->getWidth()); $drawing->line( $this->line()->getStart()->getX(), diff --git a/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php b/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php index d975f93f..20888991 100644 --- a/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php +++ b/src/Drivers/Imagick/Modifiers/DrawPixelModifier.php @@ -4,15 +4,16 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers; use ImagickDraw; use Intervention\Image\Drivers\Imagick\Color; -use Intervention\Image\Exceptions\TypeException; use Intervention\Image\Geometry\Point; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ModifierInterface; +use Intervention\Image\Traits\CanCheckType; use Intervention\Image\Traits\CanHandleInput; class DrawPixelModifier implements ModifierInterface { use CanHandleInput; + use CanCheckType; public function __construct(protected Point $position, protected mixed $color) { @@ -21,7 +22,11 @@ class DrawPixelModifier implements ModifierInterface public function apply(ImageInterface $image): ImageInterface { - $color = $this->decodeColor(); + $color = $this->failIfNotClass( + $this->handleInput($this->color), + Color::class, + ); + $pixel = new ImagickDraw(); $pixel->setFillColor($color->getPixel()); $pixel->point($this->position->getX(), $this->position->getY()); @@ -30,15 +35,4 @@ class DrawPixelModifier implements ModifierInterface $frame->getCore()->drawImage($pixel); }); } - - private function decodeColor(): Color - { - $color = $this->handleInput($this->color); - - if (!is_a($color, Color::class)) { - throw new TypeException('Color is not compatible to current driver.'); - } - - return $color; - } } diff --git a/src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php b/src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php index 8c39081b..ea8b511c 100644 --- a/src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php +++ b/src/Drivers/Imagick/Modifiers/DrawPolygonModifier.php @@ -5,7 +5,6 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers; use ImagickDraw; use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier; use Intervention\Image\Drivers\Imagick\Color; -use Intervention\Image\Exceptions\TypeException; use Intervention\Image\Interfaces\DrawableInterface; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ModifierInterface; @@ -21,12 +20,15 @@ class DrawPolygonModifier extends AbstractDrawModifier implements ModifierInterf public function apply(ImageInterface $image): ImageInterface { $drawing = new ImagickDraw(); + $background_color = $this->failIfNotClass($this->getBackgroundColor(), Color::class); + $border_color = $this->failIfNotClass($this->getBorderColor(), Color::class); + if ($this->polygon()->hasBackgroundColor()) { - $drawing->setFillColor($this->getBackgroundColor()->getPixel()); + $drawing->setFillColor($background_color->getPixel()); } if ($this->polygon()->hasBorder()) { - $drawing->setStrokeColor($this->getBorderColor()->getPixel()); + $drawing->setStrokeColor($border_color->getPixel()); $drawing->setStrokeWidth($this->polygon()->getBorderSize()); } @@ -46,26 +48,4 @@ class DrawPolygonModifier extends AbstractDrawModifier implements ModifierInterf return $points; } - - protected function getBackgroundColor(): ?Color - { - $color = parent::getBackgroundColor(); - - if (!is_a($color, Color::class)) { - throw new TypeException('Color is not compatible to current driver.'); - } - - return $color; - } - - protected function getBorderColor(): ?Color - { - $color = parent::getBorderColor(); - - if (!is_a($color, Color::class)) { - throw new TypeException('Color is not compatible to current driver.'); - } - - return $color; - } } diff --git a/src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php b/src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php index 8993de8a..2baf9721 100644 --- a/src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php +++ b/src/Drivers/Imagick/Modifiers/DrawRectangleModifier.php @@ -5,7 +5,6 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers; use ImagickDraw; use Intervention\Image\Drivers\Abstract\Modifiers\AbstractDrawModifier; use Intervention\Image\Drivers\Imagick\Color; -use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ModifierInterface; @@ -13,11 +12,14 @@ class DrawRectangleModifier extends AbstractDrawModifier implements ModifierInte { public function apply(ImageInterface $image): ImageInterface { - // setup rectangle + // setup $drawing = new ImagickDraw(); - $drawing->setFillColor($this->getBackgroundColor()->getPixel()); + $background_color = $this->failIfNotClass($this->getBackgroundColor(), Color::class); + $border_color = $this->failIfNotClass($this->getBorderColor(), Color::class); + + $drawing->setFillColor($background_color->getPixel()); if ($this->rectangle()->hasBorder()) { - $drawing->setStrokeColor($this->getBorderColor()->getPixel()); + $drawing->setStrokeColor($border_color->getPixel()); $drawing->setStrokeWidth($this->rectangle()->getBorderSize()); } @@ -35,24 +37,4 @@ class DrawRectangleModifier extends AbstractDrawModifier implements ModifierInte return $image; } - - protected function getBackgroundColor(): Color - { - $color = parent::getBackgroundColor(); - if (!is_a($color, Color::class)) { - throw new DecoderException('Unable to decode background color.'); - } - - return $color; - } - - protected function getBorderColor(): Color - { - $color = parent::getBorderColor(); - if (!is_a($color, Color::class)) { - throw new DecoderException('Unable to decode border color.'); - } - - return $color; - } } diff --git a/src/Drivers/Imagick/Modifiers/PadModifier.php b/src/Drivers/Imagick/Modifiers/PadModifier.php index 94d44a65..3b0d38e2 100644 --- a/src/Drivers/Imagick/Modifiers/PadModifier.php +++ b/src/Drivers/Imagick/Modifiers/PadModifier.php @@ -6,7 +6,6 @@ use Imagick; use ImagickDraw; use Intervention\Image\Drivers\Abstract\Modifiers\AbstractPadModifier; use Intervention\Image\Drivers\Imagick\Color; -use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ModifierInterface; use Intervention\Image\Interfaces\SizeInterface; @@ -22,11 +21,7 @@ class PadModifier extends AbstractPadModifier implements ModifierInterface { $resize = $this->getResizeSize($image); $crop = $this->getCropSize($image); - $background = $this->handleInput($this->background); - - if (!is_a($background, Color::class)) { - throw new DecoderException('Unable to decode backgroud color.'); - } + $background = $this->failIfNotClass($this->handleInput($this->background), Color::class); foreach ($image as $frame) { // resize current core diff --git a/src/Drivers/Imagick/Modifiers/RotateModifier.php b/src/Drivers/Imagick/Modifiers/RotateModifier.php index 304585e7..ff908c04 100644 --- a/src/Drivers/Imagick/Modifiers/RotateModifier.php +++ b/src/Drivers/Imagick/Modifiers/RotateModifier.php @@ -4,7 +4,6 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers; use Intervention\Image\Drivers\Abstract\Modifiers\AbstractRotateModifier; use Intervention\Image\Drivers\Imagick\Color; -use Intervention\Image\Exceptions\DecoderException; use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ModifierInterface; @@ -12,10 +11,7 @@ class RotateModifier extends AbstractRotateModifier implements ModifierInterface { public function apply(ImageInterface $image): ImageInterface { - $background = $this->backgroundColor(); - if (!is_a($background, Color::class)) { - throw new DecoderException('Unable to decode given background color.'); - } + $background = $this->failIfNotClass($this->backgroundColor(), Color::class); foreach ($image as $frame) { $frame->getCore()->rotateImage( diff --git a/src/Drivers/Imagick/Modifiers/TextWriter.php b/src/Drivers/Imagick/Modifiers/TextWriter.php index 90469983..4bf3af51 100644 --- a/src/Drivers/Imagick/Modifiers/TextWriter.php +++ b/src/Drivers/Imagick/Modifiers/TextWriter.php @@ -4,7 +4,6 @@ namespace Intervention\Image\Drivers\Imagick\Modifiers; use Intervention\Image\Drivers\Abstract\AbstractTextWriter; use Intervention\Image\Drivers\Imagick\Font; -use Intervention\Image\Exceptions\FontException; use Intervention\Image\Interfaces\ImageInterface; class TextWriter extends AbstractTextWriter @@ -12,13 +11,15 @@ class TextWriter extends AbstractTextWriter public function apply(ImageInterface $image): ImageInterface { $lines = $this->getAlignedTextBlock(); + $font = $this->failIfNotClass($this->getFont(), Font::class); + foreach ($image as $frame) { foreach ($lines as $line) { $frame->getCore()->annotateImage( - $this->getFont()->toImagickDraw(), + $font->toImagickDraw(), $line->getPosition()->getX(), $line->getPosition()->getY(), - $this->getFont()->getAngle(), + $font->getAngle(), $line ); } @@ -26,12 +27,4 @@ class TextWriter extends AbstractTextWriter return $image; } - - protected function getFont(): Font - { - if (!is_a($this->font, Font::class)) { - throw new FontException('Font is not compatible to current driver.'); - } - return $this->font; - } } diff --git a/src/Traits/CanCheckType.php b/src/Traits/CanCheckType.php new file mode 100644 index 00000000..f70e85bd --- /dev/null +++ b/src/Traits/CanCheckType.php @@ -0,0 +1,17 @@ +resolveDriverClass('InputHandler')->handle($input); + $result = $this->resolveDriverClass('InputHandler')->handle($input); + + if (!is_null($check_result_against_classname) && get_class($result) != $check_result_against_classname) { + throw new DecoderException('Decoded result is not an instance of ' . $check_result_against_classname); + } + + return $result; } } diff --git a/tests/Drivers/Abstract/Modifiers/AbstractRotateModifierTest.php b/tests/Drivers/Abstract/Modifiers/AbstractRotateModifierTest.php index 42de945b..554e95f9 100644 --- a/tests/Drivers/Abstract/Modifiers/AbstractRotateModifierTest.php +++ b/tests/Drivers/Abstract/Modifiers/AbstractRotateModifierTest.php @@ -64,7 +64,7 @@ final class AbstractRotateModifierTest extends TestCase return parent::backgroundColor(); } - public function handleInput($input): ImageInterface|ColorInterface + public function handleInput($input, ?string $check_result_against_classname = null): ImageInterface|ColorInterface { if ($this->background === 'bad value') { throw new DecoderException();