From 2f3403928212c326f4eea5702b79dc31ce12cda3 Mon Sep 17 00:00:00 2001 From: Philippe Damen Date: Thu, 18 Jan 2024 14:37:49 +0100 Subject: [PATCH] Allow srid enum as srid property aside from integer (#103) * Allow srid enum as srid property aside from integer * Updated class signatures in API.md * Update to API.md + changes api function signature to allow srid enum * fix linting --------- Co-authored-by: Philippe Damen --- API.md | 24 ++++++++++++------------ src/Objects/Geometry.php | 9 +++++---- src/Objects/GeometryCollection.php | 5 +++-- src/Objects/MultiLineString.php | 5 +++-- src/Objects/MultiPolygon.php | 5 +++-- src/Objects/Point.php | 6 ++++-- src/Objects/PointCollection.php | 5 +++-- tests/Objects/GeometryCollectionTest.php | 22 +++++++++++++++++++++- tests/Objects/LineStringTest.php | 14 +++++++++++++- tests/Objects/MultiLineStringTest.php | 16 +++++++++++++++- tests/Objects/MultiPointTest.php | 13 ++++++++++++- tests/Objects/MultiPolygonTest.php | 21 ++++++++++++++++++++- tests/Objects/PointTest.php | 11 ++++++++++- tests/Objects/PolygonTest.php | 19 ++++++++++++++++++- 14 files changed, 142 insertions(+), 33 deletions(-) diff --git a/API.md b/API.md index 37a6598..be90f03 100644 --- a/API.md +++ b/API.md @@ -2,20 +2,20 @@ ## Available geometry classes -* `Point(float $latitude, float $longitude, int $srid = 0)` - [MySQL Point](https://dev.mysql.com/doc/refman/8.0/en/gis-class-point.html) -* `MultiPoint(Point[] | Collection $geometries, int $srid = 0)` - [MySQL MultiPoint](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipoint.html) -* `LineString(Point[] | Collection $geometries, int $srid = 0)` - [MySQL LineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-linestring.html) -* `MultiLineString(LineString[] | Collection $geometries, int $srid = 0)` - [MySQL MultiLineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multilinestring.html) -* `Polygon(LineString[] | Collection $geometries, int $srid = 0)` - [MySQL Polygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-polygon.html) -* `MultiPolygon(Polygon[] | Collection $geometries, int $srid = 0)` - [MySQL MultiPolygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipolygon.html) -* `GeometryCollection(Geometry[] | Collection $geometries, int $srid = 0)` - [MySQL GeometryCollection](https://dev.mysql.com/doc/refman/8.0/en/gis-class-geometrycollection.html) +* `Point(float $latitude, float $longitude, int|Srid $srid = 0)` - [MySQL Point](https://dev.mysql.com/doc/refman/8.0/en/gis-class-point.html) +* `MultiPoint(Point[] | Collection $geometries, int|Srid $srid = 0)` - [MySQL MultiPoint](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipoint.html) +* `LineString(Point[] | Collection $geometries, int|Srid $srid = 0)` - [MySQL LineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-linestring.html) +* `MultiLineString(LineString[] | Collection $geometries, int|Srid $srid = 0)` - [MySQL MultiLineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multilinestring.html) +* `Polygon(LineString[] | Collection $geometries, int|Srid $srid = 0)` - [MySQL Polygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-polygon.html) +* `MultiPolygon(Polygon[] | Collection $geometries, int|Srid $srid = 0)` - [MySQL MultiPolygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipolygon.html) +* `GeometryCollection(Geometry[] | Collection $geometries, int|Srid $srid = 0)` - [MySQL GeometryCollection](https://dev.mysql.com/doc/refman/8.0/en/gis-class-geometrycollection.html) Geometry classes can be also created by these static methods: * `fromArray(array $geometry)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) array. -* `fromJson(string $geoJson, int $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) string. -* `fromWkt(string $wkt, int $srid = 0)` - Creates a geometry object from a [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry). -* `fromWkb(string $wkb, int $srid = 0)` - Creates a geometry object from a [WKB](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary). +* `fromJson(string $geoJson, int|Srid $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) string. +* `fromWkt(string $wkt, int|Srid $srid = 0)` - Creates a geometry object from a [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry). +* `fromWkb(string $wkb)` - Creates a geometry object from a [WKB](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary). ## Available geometry class methods @@ -58,8 +58,8 @@ An enum is provided with the following values: | Identifier | Value | Description | |----------------------|--------|-------------------------------------------------------------------------------------| -| `Srid::WGS84` | `4326` | [Geographic coordinate system](https://epsg.org/crs_4326/WGS-84.html) | -| `Srid::WEB_MERCATOR` | `3857` | [Mercator coordinate system](https://epsg.org/crs_3857/WGS-84-Pseudo-Mercator.html) | +| `Srid::WGS84` | `4326` | [Geographic coordinate system](https://epsg.org/crs_4326/WGS-84.html) | +| `Srid::WEB_MERCATOR` | `3857` | [Mercator coordinate system](https://epsg.org/crs_3857/WGS-84-Pseudo-Mercator.html) | ## Available spatial scopes diff --git a/src/Objects/Geometry.php b/src/Objects/Geometry.php index aebde61..37f24b7 100644 --- a/src/Objects/Geometry.php +++ b/src/Objects/Geometry.php @@ -17,6 +17,7 @@ use JsonException; use JsonSerializable; use MatanYadaev\EloquentSpatial\AxisOrder; +use MatanYadaev\EloquentSpatial\Enums\Srid; use MatanYadaev\EloquentSpatial\Factory; use MatanYadaev\EloquentSpatial\GeometryCast; use Stringable; @@ -96,10 +97,10 @@ public static function fromWkb(string $wkb): static * * @throws InvalidArgumentException */ - public static function fromWkt(string $wkt, int $srid = 0): static + public static function fromWkt(string $wkt, int|Srid $srid = 0): static { $geometry = Factory::parse($wkt); - $geometry->srid = $srid; + $geometry->srid = $srid instanceof Srid ? $srid->value : $srid; if (! ($geometry instanceof static)) { throw new InvalidArgumentException( @@ -117,10 +118,10 @@ public static function fromWkt(string $wkt, int $srid = 0): static * * @throws InvalidArgumentException */ - public static function fromJson(string $geoJson, int $srid = 0): static + public static function fromJson(string $geoJson, int|Srid $srid = 0): static { $geometry = Factory::parse($geoJson); - $geometry->srid = $srid; + $geometry->srid = $srid instanceof Srid ? $srid->value : $srid; if (! ($geometry instanceof static)) { throw new InvalidArgumentException( diff --git a/src/Objects/GeometryCollection.php b/src/Objects/GeometryCollection.php index 09559ad..a46bf56 100644 --- a/src/Objects/GeometryCollection.php +++ b/src/Objects/GeometryCollection.php @@ -8,6 +8,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Str; use InvalidArgumentException; +use MatanYadaev\EloquentSpatial\Enums\Srid; class GeometryCollection extends Geometry implements ArrayAccess { @@ -24,14 +25,14 @@ class GeometryCollection extends Geometry implements ArrayAccess * * @throws InvalidArgumentException */ - public function __construct(Collection|array $geometries, int $srid = 0) + public function __construct(Collection|array $geometries, int|Srid $srid = 0) { if (is_array($geometries)) { $geometries = collect($geometries); } $this->geometries = $geometries; - $this->srid = $srid; + $this->srid = $srid instanceof Srid ? $srid->value : $srid; $this->validateGeometriesType(); $this->validateGeometriesCount(); diff --git a/src/Objects/MultiLineString.php b/src/Objects/MultiLineString.php index ee72e15..c5ed380 100644 --- a/src/Objects/MultiLineString.php +++ b/src/Objects/MultiLineString.php @@ -6,6 +6,7 @@ use Illuminate\Support\Collection; use InvalidArgumentException; +use MatanYadaev\EloquentSpatial\Enums\Srid; /** * @property Collection $geometries @@ -26,10 +27,10 @@ class MultiLineString extends GeometryCollection * * @throws InvalidArgumentException */ - public function __construct(Collection|array $geometries, int $srid = 0) + public function __construct(Collection|array $geometries, int|Srid $srid = 0) { // @phpstan-ignore-next-line - parent::__construct($geometries, $srid); + parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid); } public function toWkt(): string diff --git a/src/Objects/MultiPolygon.php b/src/Objects/MultiPolygon.php index 7ecdac0..66d5cd5 100644 --- a/src/Objects/MultiPolygon.php +++ b/src/Objects/MultiPolygon.php @@ -6,6 +6,7 @@ use Illuminate\Support\Collection; use InvalidArgumentException; +use MatanYadaev\EloquentSpatial\Enums\Srid; /** * @property Collection $geometries @@ -26,10 +27,10 @@ class MultiPolygon extends GeometryCollection * * @throws InvalidArgumentException */ - public function __construct(Collection|array $geometries, int $srid = 0) + public function __construct(Collection|array $geometries, int|Srid $srid = 0) { // @phpstan-ignore-next-line - parent::__construct($geometries, $srid); + parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid); } public function toWkt(): string diff --git a/src/Objects/Point.php b/src/Objects/Point.php index f79447a..da34591 100644 --- a/src/Objects/Point.php +++ b/src/Objects/Point.php @@ -4,17 +4,19 @@ namespace MatanYadaev\EloquentSpatial\Objects; +use MatanYadaev\EloquentSpatial\Enums\Srid; + class Point extends Geometry { public float $latitude; public float $longitude; - public function __construct(float $latitude, float $longitude, int $srid = 0) + public function __construct(float $latitude, float $longitude, int|Srid $srid = 0) { $this->latitude = $latitude; $this->longitude = $longitude; - $this->srid = $srid; + $this->srid = $srid instanceof Srid ? $srid->value : $srid; } public function toWkt(): string diff --git a/src/Objects/PointCollection.php b/src/Objects/PointCollection.php index 71a807a..5190bb6 100644 --- a/src/Objects/PointCollection.php +++ b/src/Objects/PointCollection.php @@ -6,6 +6,7 @@ use Illuminate\Support\Collection; use InvalidArgumentException; +use MatanYadaev\EloquentSpatial\Enums\Srid; /** * @property Collection $geometries @@ -24,9 +25,9 @@ abstract class PointCollection extends GeometryCollection * * @throws InvalidArgumentException */ - public function __construct(Collection|array $geometries, int $srid = 0) + public function __construct(Collection|array $geometries, int|Srid $srid = 0) { // @phpstan-ignore-next-line - parent::__construct($geometries, $srid); + parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid); } } diff --git a/tests/Objects/GeometryCollectionTest.php b/tests/Objects/GeometryCollectionTest.php index 9bde61b..c32bc81 100644 --- a/tests/Objects/GeometryCollectionTest.php +++ b/tests/Objects/GeometryCollectionTest.php @@ -32,7 +32,7 @@ expect($testPlace->geometry_collection)->toEqual($geometryCollection); }); -it('creates a model record with geometry collection with SRID', function (): void { +it('creates a model record with geometry collection with SRID integer', function (): void { $geometryCollection = new GeometryCollection([ new Polygon([ new LineString([ @@ -52,6 +52,26 @@ expect($testPlace->geometry_collection->srid)->toBe(Srid::WGS84->value); }); +it('creates a model record with geometry collection with SRID enum', function (): void { + $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), + ], Srid::WGS84); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['geometry_collection' => $geometryCollection]); + + expect($testPlace->geometry_collection->srid)->toBe(Srid::WGS84->value); +}); + it('creates geometry collection from JSON', function (): void { $geometryCollection = new GeometryCollection([ new Polygon([ diff --git a/tests/Objects/LineStringTest.php b/tests/Objects/LineStringTest.php index 2e41e37..13668b2 100644 --- a/tests/Objects/LineStringTest.php +++ b/tests/Objects/LineStringTest.php @@ -23,7 +23,7 @@ expect($testPlace->line_string)->toEqual($lineString); }); -it('creates a model record with line string with SRID', function (): void { +it('creates a model record with line string with SRID integer', function (): void { $lineString = new LineString([ new Point(0, 180), new Point(1, 179), @@ -35,6 +35,18 @@ expect($testPlace->line_string->srid)->toBe(Srid::WGS84->value); }); +it('creates a model record with line string with SRID enum', function (): void { + $lineString = new LineString([ + new Point(0, 180), + new Point(1, 179), + ], Srid::WGS84); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['line_string' => $lineString]); + + expect($testPlace->line_string->srid)->toBe(Srid::WGS84->value); +}); + it('creates line string from JSON', function (): void { $lineString = new LineString([ new Point(0, 180), diff --git a/tests/Objects/MultiLineStringTest.php b/tests/Objects/MultiLineStringTest.php index 1d25b88..44c432e 100644 --- a/tests/Objects/MultiLineStringTest.php +++ b/tests/Objects/MultiLineStringTest.php @@ -25,7 +25,7 @@ expect($testPlace->multi_line_string)->toEqual($multiLineString); }); -it('creates a model record with multi line string with SRID', function (): void { +it('creates a model record with multi line string with SRID integer', function (): void { $multiLineString = new MultiLineString([ new LineString([ new Point(0, 180), @@ -39,6 +39,20 @@ expect($testPlace->multi_line_string->srid)->toBe(Srid::WGS84->value); }); +it('creates a model record with multi line string with SRID enum', function (): void { + $multiLineString = new MultiLineString([ + new LineString([ + new Point(0, 180), + new Point(1, 179), + ]), + ], Srid::WGS84); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['multi_line_string' => $multiLineString]); + + expect($testPlace->multi_line_string->srid)->toBe(Srid::WGS84->value); +}); + it('creates multi line string from JSON', function (): void { $multiLineString = new MultiLineString([ new LineString([ diff --git a/tests/Objects/MultiPointTest.php b/tests/Objects/MultiPointTest.php index 2b4a7b1..3d64434 100644 --- a/tests/Objects/MultiPointTest.php +++ b/tests/Objects/MultiPointTest.php @@ -22,7 +22,7 @@ expect($testPlace->multi_point)->toEqual($multiPoint); }); -it('creates a model record with multi point with SRID', function (): void { +it('creates a model record with multi point with SRID integer', function (): void { $multiPoint = new MultiPoint([ new Point(0, 180), ], Srid::WGS84->value); @@ -33,6 +33,17 @@ expect($testPlace->multi_point->srid)->toBe(Srid::WGS84->value); }); +it('creates a model record with multi point with SRID enum', function (): void { + $multiPoint = new MultiPoint([ + new Point(0, 180), + ], Srid::WGS84); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['multi_point' => $multiPoint]); + + expect($testPlace->multi_point->srid)->toBe(Srid::WGS84->value); +}); + it('creates multi point from JSON', function (): void { $multiPoint = new MultiPoint([ new Point(0, 180), diff --git a/tests/Objects/MultiPolygonTest.php b/tests/Objects/MultiPolygonTest.php index d5ced39..7e5faf8 100644 --- a/tests/Objects/MultiPolygonTest.php +++ b/tests/Objects/MultiPolygonTest.php @@ -31,7 +31,7 @@ expect($testPlace->multi_polygon)->toEqual($multiPolygon); }); -it('creates a model record with multi polygon with SRID', function (): void { +it('creates a model record with multi polygon with SRID integer', function (): void { $multiPolygon = new MultiPolygon([ new Polygon([ new LineString([ @@ -50,6 +50,25 @@ expect($testPlace->multi_polygon->srid)->toBe(Srid::WGS84->value); }); +it('creates a model record with multi polygon with SRID enum', function (): void { + $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), + ]), + ]), + ], Srid::WGS84); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['multi_polygon' => $multiPolygon]); + + expect($testPlace->multi_polygon->srid)->toBe(Srid::WGS84->value); +}); + it('creates multi polygon from JSON', function (): void { $multiPolygon = new MultiPolygon([ new Polygon([ diff --git a/tests/Objects/PointTest.php b/tests/Objects/PointTest.php index 78f4741..f9fd94c 100644 --- a/tests/Objects/PointTest.php +++ b/tests/Objects/PointTest.php @@ -18,7 +18,7 @@ expect($testPlace->point)->toEqual($point); }); -it('creates a model record with point with SRID', function (): void { +it('creates a model record with point with SRID integer', function (): void { $point = new Point(0, 180, Srid::WGS84->value); /** @var TestPlace $testPlace */ @@ -27,6 +27,15 @@ expect($testPlace->point->srid)->toBe(Srid::WGS84->value); }); +it('creates a model record with point with SRID enum', function (): void { + $point = new Point(0, 180, Srid::WGS84); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['point' => $point]); + + expect($testPlace->point->srid)->toBe(Srid::WGS84->value); +}); + it('creates point from JSON', function (): void { $point = new Point(0, 180); diff --git a/tests/Objects/PolygonTest.php b/tests/Objects/PolygonTest.php index b48481f..a91ae43 100644 --- a/tests/Objects/PolygonTest.php +++ b/tests/Objects/PolygonTest.php @@ -28,7 +28,7 @@ expect($testPlace->polygon)->toEqual($polygon); }); -it('creates a model record with polygon with SRID', function (): void { +it('creates a model record with polygon with SRID integer', function (): void { $polygon = new Polygon([ new LineString([ new Point(0, 180), @@ -45,6 +45,23 @@ expect($testPlace->polygon->srid)->toBe(Srid::WGS84->value); }); +it('creates a model record with polygon with SRID enum', function (): void { + $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), + ]), + ], Srid::WGS84); + + /** @var TestPlace $testPlace */ + $testPlace = TestPlace::factory()->create(['polygon' => $polygon]); + + expect($testPlace->polygon->srid)->toBe(Srid::WGS84->value); +}); + it('creates polygon from JSON', function (): void { $polygon = new Polygon([ new LineString([