From 7a103cbfa8b36f73c82028e2030cb08e2bc2c6ab Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Mon, 25 Jul 2022 11:13:38 +0200 Subject: [PATCH] Implement IteratorAggregate in Polygon::class --- src/Geometry/Polygon.php | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Geometry/Polygon.php b/src/Geometry/Polygon.php index 732193f0..50cf3cb0 100644 --- a/src/Geometry/Polygon.php +++ b/src/Geometry/Polygon.php @@ -3,10 +3,19 @@ namespace Intervention\Image\Geometry; use ArrayAccess; +use ArrayIterator; use Countable; +use Traversable; +use IteratorAggregate; +use Intervention\Image\Geometry\Traits\HasBackgroundColor; +use Intervention\Image\Geometry\Traits\HasBorder; +use Intervention\Image\Interfaces\DrawableInterface; -class Polygon implements Countable, ArrayAccess +class Polygon implements IteratorAggregate, Countable, ArrayAccess, DrawableInterface { + use HasBorder; + use HasBackgroundColor; + public function __construct( protected array $points = [], protected ?Point $pivot = null @@ -14,6 +23,11 @@ class Polygon implements Countable, ArrayAccess $this->pivot = $pivot ? $pivot : new Point(); } + public function getIterator(): Traversable + { + return new ArrayIterator($this->points); + } + /** * Return current pivot point * @@ -133,6 +147,13 @@ class Polygon implements Countable, ArrayAccess return $this; } + public function point(int $x, int $y): self + { + $this->addPoint(new Point($x, $y)); + + return $this; + } + /** * Calculate total horizontal span of polygon *