Skip to content

Commit

Permalink
Move method to trait
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Nov 15, 2023
1 parent afd5c73 commit a9922e2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
23 changes: 8 additions & 15 deletions src/Drivers/Abstract/AbstractImage.php
Expand Up @@ -17,23 +17,16 @@
use Intervention\Image\Interfaces\SizeInterface;
use Intervention\Image\Traits\CanHandleInput;
use Intervention\Image\Traits\CanResolveDriverClass;
use Intervention\Image\Traits\CanRunCallback;

abstract class AbstractImage implements ImageInterface
{
use CanResolveDriverClass;
use CanHandleInput;
use CanRunCallback;

protected Collection $exif;

protected function runCallback(?callable $callback, object $object): object
{
if (is_callable($callback)) {
$callback($object);
}

return $object;
}

public function mapFrames(callable $callback): ImageInterface
{
foreach ($this as $frame) {
Expand Down Expand Up @@ -248,7 +241,7 @@ public function pickColors(int $x, int $y): CollectionInterface

public function text(string $text, int $x, int $y, ?callable $init = null): ImageInterface
{
$font = $this->runCallback($init, $this->resolveDriverClass('Font'));
$font = $this->maybeRunCallback($init, $this->resolveDriverClass('Font'));

$modifier = $this->resolveDriverClass('Modifiers\TextWriter', new Point($x, $y), $font, $text);

Expand All @@ -264,39 +257,39 @@ public function drawPixel(int $x, int $y, $color = null): ImageInterface

public function drawRectangle(int $x, int $y, ?callable $init = null): ImageInterface
{
$rectangle = $this->runCallback($init, new Rectangle(0, 0));
$rectangle = $this->maybeRunCallback($init, new Rectangle(0, 0));
$modifier = $this->resolveDriverClass('Modifiers\DrawRectangleModifier', new Point($x, $y), $rectangle);

return $this->modify($modifier);
}

public function drawEllipse(int $x, int $y, ?callable $init = null): ImageInterface
{
$ellipse = $this->runCallback($init, new Ellipse(0, 0));
$ellipse = $this->maybeRunCallback($init, new Ellipse(0, 0));
$modifier = $this->resolveDriverClass('Modifiers\DrawEllipseModifier', new Point($x, $y), $ellipse);

return $this->modify($modifier);
}

public function drawCircle(int $x, int $y, ?callable $init = null): ImageInterface
{
$circle = $this->runCallback($init, new Circle(0));
$circle = $this->maybeRunCallback($init, new Circle(0));
$modifier = $this->resolveDriverClass('Modifiers\DrawEllipseModifier', new Point($x, $y), $circle);

return $this->modify($modifier);
}

public function drawLine(callable $init = null): ImageInterface
{
$line = $this->runCallback($init, new Line(new Point(), new Point()));
$line = $this->maybeRunCallback($init, new Line(new Point(), new Point()));
$modifier = $this->resolveDriverClass('Modifiers\DrawLineModifier', $line->getStart(), $line);

return $this->modify($modifier);
}

public function drawPolygon(callable $init = null): ImageInterface
{
$polygon = $this->runCallback($init, new Polygon());
$polygon = $this->maybeRunCallback($init, new Polygon());
$modifier = $this->resolveDriverClass('Modifiers\DrawPolygonModifier', $polygon);

return $this->modify($modifier);
Expand Down
22 changes: 22 additions & 0 deletions src/Traits/CanRunCallback.php
@@ -0,0 +1,22 @@
<?php

namespace Intervention\Image\Traits;

trait CanRunCallback
{
/**
* Runs given callback against given object and returns object
*
* @param null|callable $callback
* @param object $object
* @return object
*/
protected function maybeRunCallback(?callable $callback, object $object): object
{
if (is_callable($callback)) {
$callback($object);
}

return $object;
}
}

0 comments on commit a9922e2

Please sign in to comment.