From 874655fa065250d43449629f0041c3538c614b16 Mon Sep 17 00:00:00 2001 From: Matan Yadaev Date: Thu, 6 Oct 2022 23:48:41 +0300 Subject: [PATCH 1/5] Add tests --- tests/Objects/GeometryCollectionTest.php | 23 +++++++++++++++++++++++ tests/Objects/LineStringTest.php | 15 +++++++++++++++ tests/Objects/MultiLineStringTest.php | 17 +++++++++++++++++ tests/Objects/MultiPointTest.php | 14 ++++++++++++++ tests/Objects/MultiPolygonTest.php | 24 +++++++++++++++++++++++- tests/Objects/PointTest.php | 13 +++++++++++++ tests/Objects/PolygonTest.php | 22 +++++++++++++++++++++- 7 files changed, 126 insertions(+), 2 deletions(-) diff --git a/tests/Objects/GeometryCollectionTest.php b/tests/Objects/GeometryCollectionTest.php index 3f51ba6..7a50c6b 100644 --- a/tests/Objects/GeometryCollectionTest.php +++ b/tests/Objects/GeometryCollectionTest.php @@ -5,6 +5,7 @@ use MatanYadaev\EloquentSpatial\Objects\LineString; use MatanYadaev\EloquentSpatial\Objects\Point; use MatanYadaev\EloquentSpatial\Objects\Polygon; +use MatanYadaev\EloquentSpatial\Objects\Geometry; use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; uses(DatabaseMigrations::class); @@ -365,3 +366,25 @@ expect($geometryCollection->__toString())->toEqual('GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))'); }); + +it('adds a Macro method to GeometryCollection', function (): void { + Geometry::macro('getName', function (): string { + return class_basename($this); + }); + + $geometryCollection = new GeometryCollection([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + new Point(0, 180), + ]); + + // @phpstan-ignore-next-line + expect($geometryCollection->getName())->toBe('GeometryCollection'); +}); diff --git a/tests/Objects/LineStringTest.php b/tests/Objects/LineStringTest.php index 4cc89b8..f208658 100644 --- a/tests/Objects/LineStringTest.php +++ b/tests/Objects/LineStringTest.php @@ -4,6 +4,7 @@ use MatanYadaev\EloquentSpatial\Objects\LineString; use MatanYadaev\EloquentSpatial\Objects\Point; use MatanYadaev\EloquentSpatial\Objects\Polygon; +use MatanYadaev\EloquentSpatial\Objects\Geometry; use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; uses(DatabaseMigrations::class); @@ -160,3 +161,17 @@ expect($lineString->__toString())->toEqual('LINESTRING(180 0, 179 1)'); }); + +it('adds a Macro method to LineString', function (): void { + Geometry::macro('getName', function (): string { + return class_basename($this); + }); + + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ]); + + // @phpstan-ignore-next-line + expect($lineString->getName())->toBe('LineString'); +}); diff --git a/tests/Objects/MultiLineStringTest.php b/tests/Objects/MultiLineStringTest.php index 91bde31..c66060c 100644 --- a/tests/Objects/MultiLineStringTest.php +++ b/tests/Objects/MultiLineStringTest.php @@ -4,6 +4,7 @@ use MatanYadaev\EloquentSpatial\Objects\LineString; use MatanYadaev\EloquentSpatial\Objects\MultiLineString; use MatanYadaev\EloquentSpatial\Objects\Point; +use MatanYadaev\EloquentSpatial\Objects\Geometry; use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; uses(DatabaseMigrations::class); @@ -182,3 +183,19 @@ expect($multiLineString->__toString())->toEqual('MULTILINESTRING((180 0, 179 1))'); }); + +it('adds a Macro method to MultiLineString', function (): void { + Geometry::macro('getName', function (): string { + return class_basename($this); + }); + + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ]); + + // @phpstan-ignore-next-line + expect($multiLineString->getName())->toBe('MultiLineString'); +}); diff --git a/tests/Objects/MultiPointTest.php b/tests/Objects/MultiPointTest.php index 0420768..ff99d6b 100644 --- a/tests/Objects/MultiPointTest.php +++ b/tests/Objects/MultiPointTest.php @@ -4,6 +4,7 @@ use MatanYadaev\EloquentSpatial\Objects\MultiPoint; use MatanYadaev\EloquentSpatial\Objects\Point; use MatanYadaev\EloquentSpatial\Objects\Polygon; +use MatanYadaev\EloquentSpatial\Objects\Geometry; use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; uses(DatabaseMigrations::class); @@ -146,3 +147,16 @@ expect($multiPoint->__toString())->toEqual('MULTIPOINT(180 0)'); }); + +it('adds a Macro method to MultiPoint', function (): void { + Geometry::macro('getName', function (): string { + return class_basename($this); + }); + + $multiPoint = new MultiPoint([ + new Point(0, 180), + ]); + + // @phpstan-ignore-next-line + expect($multiPoint->getName())->toBe('MultiPoint'); +}); diff --git a/tests/Objects/MultiPolygonTest.php b/tests/Objects/MultiPolygonTest.php index 9d51453..b7d7087 100644 --- a/tests/Objects/MultiPolygonTest.php +++ b/tests/Objects/MultiPolygonTest.php @@ -3,6 +3,7 @@ use Illuminate\Foundation\Testing\DatabaseMigrations; use MatanYadaev\EloquentSpatial\Objects\LineString; use MatanYadaev\EloquentSpatial\Objects\MultiPolygon; +use MatanYadaev\EloquentSpatial\Objects\Geometry; use MatanYadaev\EloquentSpatial\Objects\Point; use MatanYadaev\EloquentSpatial\Objects\Polygon; use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; @@ -239,7 +240,28 @@ new Point(0, 180), ]), ]), - ], 4326); + ]); expect($multiPolygon->__toString())->toEqual('MULTIPOLYGON(((180 0, 179 1, 178 2, 177 3, 180 0)))'); }); + +it('adds a Macro method to MultiPolygon', function (): void { + Geometry::macro('getName', function (): string { + return class_basename($this); + }); + + $multiPolygon = new MultiPolygon([ + new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]), + ]); + + // @phpstan-ignore-next-line + expect($multiPolygon->getName())->toBe('MultiPolygon'); +}); diff --git a/tests/Objects/PointTest.php b/tests/Objects/PointTest.php index ab25897..8325e2f 100644 --- a/tests/Objects/PointTest.php +++ b/tests/Objects/PointTest.php @@ -2,6 +2,7 @@ use Illuminate\Foundation\Testing\DatabaseMigrations; use MatanYadaev\EloquentSpatial\Objects\Point; +use MatanYadaev\EloquentSpatial\Objects\Geometry; use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; uses(DatabaseMigrations::class); @@ -102,3 +103,15 @@ expect($point->__toString())->toEqual('POINT(180 0)'); }); + + +it('adds a Macro method to Point', function (): void { + Geometry::macro('getName', function (): string { + return class_basename($this); + }); + + $point = new Point(0, 180, 4326); + + // @phpstan-ignore-next-line + expect($point->getName())->toBe('Point'); +}); diff --git a/tests/Objects/PolygonTest.php b/tests/Objects/PolygonTest.php index d78fdc1..384df05 100644 --- a/tests/Objects/PolygonTest.php +++ b/tests/Objects/PolygonTest.php @@ -4,6 +4,7 @@ use MatanYadaev\EloquentSpatial\Objects\LineString; use MatanYadaev\EloquentSpatial\Objects\Point; use MatanYadaev\EloquentSpatial\Objects\Polygon; +use MatanYadaev\EloquentSpatial\Objects\Geometry; use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace; uses(DatabaseMigrations::class); @@ -206,7 +207,7 @@ }); it('casts a Polygon to a string', function (): void { - $polygon = $polygon = new Polygon([ + $polygon = new Polygon([ new LineString([ new Point(0, 180), new Point(1, 179), @@ -218,3 +219,22 @@ expect($polygon->__toString())->toEqual('POLYGON((180 0, 179 1, 178 2, 177 3, 180 0))'); }); + +it('adds a Macro method to Polygon', function (): void { + Geometry::macro('getName', function (): string { + return class_basename($this); + }); + + $polygon = new Polygon([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + new Point(2, 178), + new Point(3, 177), + new Point(0, 180), + ]), + ]); + + // @phpstan-ignore-next-line + expect($polygon->getName())->toBe('Polygon'); +}); From 7f39b0e72cae09a70a411cff1f20cf3d82d9f0eb Mon Sep 17 00:00:00 2001 From: Matan Yadaev Date: Thu, 6 Oct 2022 23:48:46 +0300 Subject: [PATCH 2/5] Implementation --- src/Objects/Geometry.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Objects/Geometry.php b/src/Objects/Geometry.php index 45d7738..6e6b438 100644 --- a/src/Objects/Geometry.php +++ b/src/Objects/Geometry.php @@ -9,6 +9,7 @@ use Illuminate\Contracts\Database\Eloquent\CastsAttributes; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; +use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; use JsonException; use JsonSerializable; @@ -19,6 +20,8 @@ abstract class Geometry implements Castable, Arrayable, Jsonable, JsonSerializable, Stringable { + use Macroable; + public int $srid = 0; abstract public function toWkt(): string; From 06db128f06afbf8ab22851fa6dda87ab4efb757d Mon Sep 17 00:00:00 2001 From: Matan Yadaev Date: Thu, 6 Oct 2022 23:55:09 +0300 Subject: [PATCH 3/5] Documentation --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 9c3da68..96996ed 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,33 @@ Place::query()->whereDistance(...); // This is IDE-friendly Place::whereDistance(...); // This is not ``` +## Extension + +You can extend the package by adding macro methods to the `Geometry` class. + +Register a macro in the `boot` method of your service provider: + +```php +class AppServiceProvider extends ServiceProvider +{ + public function boot(): void + { + Geometry::macro('getName', function (): string { + /** @var Geometry $this */ + return class_basename($this); + }); + } +} +``` + +Use the macro in your code: + +```php +$londonEyePoint = new Point(51.5032973, -0.1217424); + +echo $londonEyePoint->getName(); // Point +``` + ## Development * Test: `composer pest` From 6f5a6ded8b3fb7f573e43e5559c0e097306e328e Mon Sep 17 00:00:00 2001 From: Matan Yadaev Date: Fri, 7 Oct 2022 00:07:15 +0300 Subject: [PATCH 4/5] Fix PhpStan --- tests/Objects/GeometryCollectionTest.php | 4 +++- tests/Objects/LineStringTest.php | 4 +++- tests/Objects/MultiLineStringTest.php | 4 +++- tests/Objects/MultiPointTest.php | 4 +++- tests/Objects/MultiPolygonTest.php | 4 +++- tests/Objects/PointTest.php | 5 +++-- tests/Objects/PolygonTest.php | 4 +++- 7 files changed, 21 insertions(+), 8 deletions(-) diff --git a/tests/Objects/GeometryCollectionTest.php b/tests/Objects/GeometryCollectionTest.php index 7a50c6b..2a5eda5 100644 --- a/tests/Objects/GeometryCollectionTest.php +++ b/tests/Objects/GeometryCollectionTest.php @@ -1,11 +1,11 @@ __toString())->toEqual('POINT(180 0)'); }); - it('adds a Macro method to Point', function (): void { Geometry::macro('getName', function (): string { + /** @var Geometry $this */ + // @phpstan-ignore-next-line return class_basename($this); }); diff --git a/tests/Objects/PolygonTest.php b/tests/Objects/PolygonTest.php index 384df05..df953a6 100644 --- a/tests/Objects/PolygonTest.php +++ b/tests/Objects/PolygonTest.php @@ -1,10 +1,10 @@ Date: Fri, 7 Oct 2022 00:19:37 +0300 Subject: [PATCH 5/5] Rename tests --- tests/Objects/GeometryCollectionTest.php | 2 +- tests/Objects/LineStringTest.php | 2 +- tests/Objects/MultiLineStringTest.php | 2 +- tests/Objects/MultiPointTest.php | 2 +- tests/Objects/MultiPolygonTest.php | 2 +- tests/Objects/PointTest.php | 2 +- tests/Objects/PolygonTest.php | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/Objects/GeometryCollectionTest.php b/tests/Objects/GeometryCollectionTest.php index 2a5eda5..8f7a9e8 100644 --- a/tests/Objects/GeometryCollectionTest.php +++ b/tests/Objects/GeometryCollectionTest.php @@ -367,7 +367,7 @@ expect($geometryCollection->__toString())->toEqual('GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))'); }); -it('adds a Macro method to GeometryCollection', function (): void { +it('adds a macro toGeometryCollection', function (): void { Geometry::macro('getName', function (): string { /** @var Geometry $this */ // @phpstan-ignore-next-line diff --git a/tests/Objects/LineStringTest.php b/tests/Objects/LineStringTest.php index 7c39a16..70912fe 100644 --- a/tests/Objects/LineStringTest.php +++ b/tests/Objects/LineStringTest.php @@ -162,7 +162,7 @@ expect($lineString->__toString())->toEqual('LINESTRING(180 0, 179 1)'); }); -it('adds a Macro method to LineString', function (): void { +it('adds a macro toLineString', function (): void { Geometry::macro('getName', function (): string { /** @var Geometry $this */ // @phpstan-ignore-next-line diff --git a/tests/Objects/MultiLineStringTest.php b/tests/Objects/MultiLineStringTest.php index ff27b09..e917f91 100644 --- a/tests/Objects/MultiLineStringTest.php +++ b/tests/Objects/MultiLineStringTest.php @@ -184,7 +184,7 @@ expect($multiLineString->__toString())->toEqual('MULTILINESTRING((180 0, 179 1))'); }); -it('adds a Macro method to MultiLineString', function (): void { +it('adds a macro toMultiLineString', function (): void { Geometry::macro('getName', function (): string { /** @var Geometry $this */ // @phpstan-ignore-next-line diff --git a/tests/Objects/MultiPointTest.php b/tests/Objects/MultiPointTest.php index b0c4faf..a68fdd2 100644 --- a/tests/Objects/MultiPointTest.php +++ b/tests/Objects/MultiPointTest.php @@ -148,7 +148,7 @@ expect($multiPoint->__toString())->toEqual('MULTIPOINT(180 0)'); }); -it('adds a Macro method to MultiPoint', function (): void { +it('adds a macro toMultiPoint', function (): void { Geometry::macro('getName', function (): string { /** @var Geometry $this */ // @phpstan-ignore-next-line diff --git a/tests/Objects/MultiPolygonTest.php b/tests/Objects/MultiPolygonTest.php index 8b8be8b..3ce3a6f 100644 --- a/tests/Objects/MultiPolygonTest.php +++ b/tests/Objects/MultiPolygonTest.php @@ -245,7 +245,7 @@ expect($multiPolygon->__toString())->toEqual('MULTIPOLYGON(((180 0, 179 1, 178 2, 177 3, 180 0)))'); }); -it('adds a Macro method to MultiPolygon', function (): void { +it('adds a macro toMultiPolygon', function (): void { Geometry::macro('getName', function (): string { /** @var Geometry $this */ // @phpstan-ignore-next-line diff --git a/tests/Objects/PointTest.php b/tests/Objects/PointTest.php index 3c9dbf8..da8c749 100644 --- a/tests/Objects/PointTest.php +++ b/tests/Objects/PointTest.php @@ -104,7 +104,7 @@ expect($point->__toString())->toEqual('POINT(180 0)'); }); -it('adds a Macro method to Point', function (): void { +it('adds a macro toPoint', function (): void { Geometry::macro('getName', function (): string { /** @var Geometry $this */ // @phpstan-ignore-next-line diff --git a/tests/Objects/PolygonTest.php b/tests/Objects/PolygonTest.php index df953a6..76c131e 100644 --- a/tests/Objects/PolygonTest.php +++ b/tests/Objects/PolygonTest.php @@ -220,7 +220,7 @@ expect($polygon->__toString())->toEqual('POLYGON((180 0, 179 1, 178 2, 177 3, 180 0))'); }); -it('adds a Macro method to Polygon', function (): void { +it('adds a macro toPolygon', function (): void { Geometry::macro('getName', function (): string { /** @var Geometry $this */ // @phpstan-ignore-next-line