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

Move method to trait

This commit is contained in:
Oliver Vogel
2023-11-15 15:25:41 +01:00
parent afd5c7395b
commit a9922e2c9a
2 changed files with 30 additions and 15 deletions

View File

@@ -17,23 +17,16 @@ use Intervention\Image\Interfaces\ModifierInterface;
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) {
@@ -248,7 +241,7 @@ abstract class AbstractImage implements ImageInterface
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);
@@ -264,7 +257,7 @@ abstract class AbstractImage implements 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);
@@ -272,7 +265,7 @@ abstract class AbstractImage implements ImageInterface
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);
@@ -280,7 +273,7 @@ abstract class AbstractImage implements ImageInterface
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);
@@ -288,7 +281,7 @@ abstract class AbstractImage implements ImageInterface
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);
@@ -296,7 +289,7 @@ abstract class AbstractImage implements ImageInterface
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);

View File

@@ -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;
}
}