From 78e290ae9743f25e31ddacdfc29ab568dfbf929f Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Wed, 5 Mar 2025 16:27:47 +0100 Subject: [PATCH] Implement IteratorAggregate for Point::class --- src/Geometry/Point.php | 19 ++++++++++++++++++- tests/Unit/Geometry/PointTest.php | 8 ++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Geometry/Point.php b/src/Geometry/Point.php index 4522fb54..2cc67518 100644 --- a/src/Geometry/Point.php +++ b/src/Geometry/Point.php @@ -4,9 +4,15 @@ declare(strict_types=1); namespace Intervention\Image\Geometry; +use ArrayIterator; use Intervention\Image\Interfaces\PointInterface; +use IteratorAggregate; +use Traversable; -class Point implements PointInterface +/** + * @implements IteratorAggregate + */ +class Point implements PointInterface, IteratorAggregate { /** * Create new point instance @@ -19,6 +25,17 @@ class Point implements PointInterface protected int $x = 0, protected int $y = 0 ) { + // + } + + /** + * {@inheritdoc} + * + * @see IteratorAggregate::getIterator() + */ + public function getIterator(): Traversable + { + return new ArrayIterator([$this->x, $this->y]); } /** diff --git a/tests/Unit/Geometry/PointTest.php b/tests/Unit/Geometry/PointTest.php index 57167df3..aa2c9c2e 100644 --- a/tests/Unit/Geometry/PointTest.php +++ b/tests/Unit/Geometry/PointTest.php @@ -27,6 +27,14 @@ final class PointTest extends BaseTestCase $this->assertEquals(50, $point->y()); } + public function testIteration(): void + { + $point = new Point(40, 50); + foreach ($point as $value) { + $this->assertIsInt($value); + } + } + public function testGetSetX(): void { $point = new Point(0, 0);