Skip to content

Commit

Permalink
Add combined Drawable class
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Jun 8, 2024
1 parent 9c7e4b2 commit 6314169
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 23 deletions.
7 changes: 5 additions & 2 deletions src/Geometry/Factories/CircleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

class CircleFactory implements DrawableFactoryInterface
{
/**
* Finished product of factory
*/
protected Circle $circle;

/**
Expand All @@ -36,9 +39,9 @@ final public function __construct(
*
* @see DrawableFactoryInterface::create()
*/
public static function create(null|callable|DrawableInterface $init = null): DrawableFactoryInterface
public function create(): DrawableInterface
{
return new static(init: $init);
return $this();
}

/**
Expand Down
58 changes: 58 additions & 0 deletions src/Geometry/Factories/Drawable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Geometry\Factories;

class Drawable
{
/**
* Create circle factory statically
*
* @return CircleFactory
*/
public static function circle(): CircleFactory
{
return new CircleFactory();
}

/**
* Create ellipse factory statically
*
* @return EllipseFactory
*/
public static function ellipse(): EllipseFactory
{
return new EllipseFactory();
}

/**
* Create line factory statically
*
* @return LineFactory
*/
public static function line(): LineFactory
{
return new LineFactory();
}

/**
* Create polygon factory statically
*
* @return PolygonFactory
*/
public static function polygon(): PolygonFactory
{
return new PolygonFactory();
}

/**
* Create rectangle factory statically
*
* @return RectangleFactory
*/
public static function rectangle(): RectangleFactory
{
return new RectangleFactory();
}
}
7 changes: 5 additions & 2 deletions src/Geometry/Factories/EllipseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

class EllipseFactory implements DrawableFactoryInterface
{
/**
* Finished product of factory
*/
protected Ellipse $ellipse;

/**
Expand All @@ -36,9 +39,9 @@ final public function __construct(
*
* @see DrawableFactoryInterface::create()
*/
public static function create(null|callable|DrawableInterface $init = null): DrawableFactoryInterface
public function create(): DrawableInterface
{
return new static(init: $init);
return $this();
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Geometry/Factories/LineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

class LineFactory implements DrawableFactoryInterface
{
/**
* Finished product of factory
*/
protected Line $line;

/**
Expand All @@ -33,9 +36,9 @@ final public function __construct(null|callable|Line $init = null)
*
* @see DrawableFactoryInterface::create()
*/
public static function create(null|callable|DrawableInterface $init = null): DrawableFactoryInterface
public function create(): DrawableInterface
{
return new static($init);
return $this();
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Geometry/Factories/PolygonFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

class PolygonFactory implements DrawableFactoryInterface
{
/**
* Finished product of factory
*/
protected Polygon $polygon;

/**
Expand All @@ -33,9 +36,9 @@ final public function __construct(null|callable|Polygon $init = null)
*
* @see DrawableFactoryInterface::create()
*/
public static function create(null|callable|DrawableInterface $init = null): DrawableFactoryInterface
public function create(): DrawableInterface
{
return new static($init);
return $this();
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Geometry/Factories/RectangleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

class RectangleFactory implements DrawableFactoryInterface
{
/**
* Finished product of factory
*/
protected Rectangle $rectangle;

/**
Expand All @@ -36,9 +39,9 @@ final public function __construct(
*
* @see DrawableFactoryInterface::create()
*/
public static function create(null|callable|DrawableInterface $init = null): DrawableFactoryInterface
public function create(): DrawableInterface
{
return new static(init: $init);
return $this();
}

/**
Expand Down
9 changes: 3 additions & 6 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -888,16 +888,13 @@ public function drawLine(callable $init): ImageInterface
* @throws GeometryException
* @see ImageInterface::draw()
*/
public function draw(DrawableInterface|DrawableFactoryInterface $drawable): ImageInterface
public function draw(DrawableInterface $drawable): ImageInterface
{
$drawable = $drawable instanceof DrawableFactoryInterface ? $drawable() : $drawable;

return $this->modify(match ($drawable::class) {
Polygon::class => new DrawPolygonModifier($drawable),
Line::class => new DrawLineModifier($drawable),
Circle::class, Ellipse::class => new DrawEllipseModifier($drawable),
Rectangle::class => new DrawRectangleModifier($drawable),
Line::class => new DrawLineModifier($drawable),
Polygon::class => new DrawPolygonModifier($drawable),
Rectangle::class => new DrawRectangleModifier($drawable),
default => throw new GeometryException('Unable to draw' . $drawable::class . ' object.'),
});
}
Expand Down
9 changes: 4 additions & 5 deletions src/Interfaces/DrawableFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
interface DrawableFactoryInterface
{
/**
* Create factory statically
* Return drawable object
*
* @param null|callable|DrawableInterface $init
* @return DrawableFactoryInterface
* @return DrawableInterface
*/
public static function create(null|callable|DrawableInterface $init = null): self;
public function create(): DrawableInterface;

/**
* Return finished object
* Return drawable object
*
* @return DrawableInterface
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Interfaces/ImageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,10 +753,10 @@ public function drawLine(callable $init): self;
* Draw the given geometric object on the current image
*
* @link https://image.intervention.io/v3/modifying/drawing#draw-objects
* @param DrawableInterface|DrawableFactoryInterface $drawable
* @param DrawableInterface $drawable
* @return ImageInterface
*/
public function draw(DrawableInterface|DrawableFactoryInterface $drawable): self;
public function draw(DrawableInterface $drawable): self;

/**
* Encode image to given media (mime) type. If no type is given the image
Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/Geometry/Factories/DrawableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Intervention\Image\Tests\Unit\Geometry\Factories;

use Intervention\Image\Geometry\Factories\CircleFactory;
use Intervention\Image\Geometry\Factories\Drawable;
use Intervention\Image\Geometry\Factories\EllipseFactory;
use Intervention\Image\Geometry\Factories\LineFactory;
use Intervention\Image\Geometry\Factories\PolygonFactory;
use Intervention\Image\Geometry\Factories\RectangleFactory;
use Intervention\Image\Tests\BaseTestCase;

final class DrawableTest extends BaseTestCase
{
public function testCircle(): void
{
$this->assertInstanceOf(CircleFactory::class, Drawable::circle());
}

public function testEllipse(): void
{
$this->assertInstanceOf(EllipseFactory::class, Drawable::ellipse());
}

public function testLine(): void
{
$this->assertInstanceOf(LineFactory::class, Drawable::line());
}

public function testPolygon(): void
{
$this->assertInstanceOf(PolygonFactory::class, Drawable::polygon());
}

public function testRectangle(): void
{
$this->assertInstanceOf(RectangleFactory::class, Drawable::rectangle());
}
}

0 comments on commit 6314169

Please sign in to comment.