Skip to content

Commit 2f34039

Browse files
yinxPhilippe Damen
andauthored
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 <[email protected]>
1 parent 0e83984 commit 2f34039

14 files changed

+142
-33
lines changed

API.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
## Available geometry classes
44

5-
* `Point(float $latitude, float $longitude, int $srid = 0)` - [MySQL Point](https://dev.mysql.com/doc/refman/8.0/en/gis-class-point.html)
6-
* `MultiPoint(Point[] | Collection<Point> $geometries, int $srid = 0)` - [MySQL MultiPoint](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipoint.html)
7-
* `LineString(Point[] | Collection<Point> $geometries, int $srid = 0)` - [MySQL LineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-linestring.html)
8-
* `MultiLineString(LineString[] | Collection<LineString> $geometries, int $srid = 0)` - [MySQL MultiLineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multilinestring.html)
9-
* `Polygon(LineString[] | Collection<LineString> $geometries, int $srid = 0)` - [MySQL Polygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-polygon.html)
10-
* `MultiPolygon(Polygon[] | Collection<Polygon> $geometries, int $srid = 0)` - [MySQL MultiPolygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipolygon.html)
11-
* `GeometryCollection(Geometry[] | Collection<Geometry> $geometries, int $srid = 0)` - [MySQL GeometryCollection](https://dev.mysql.com/doc/refman/8.0/en/gis-class-geometrycollection.html)
5+
* `Point(float $latitude, float $longitude, int|Srid $srid = 0)` - [MySQL Point](https://dev.mysql.com/doc/refman/8.0/en/gis-class-point.html)
6+
* `MultiPoint(Point[] | Collection<Point> $geometries, int|Srid $srid = 0)` - [MySQL MultiPoint](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipoint.html)
7+
* `LineString(Point[] | Collection<Point> $geometries, int|Srid $srid = 0)` - [MySQL LineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-linestring.html)
8+
* `MultiLineString(LineString[] | Collection<LineString> $geometries, int|Srid $srid = 0)` - [MySQL MultiLineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multilinestring.html)
9+
* `Polygon(LineString[] | Collection<LineString> $geometries, int|Srid $srid = 0)` - [MySQL Polygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-polygon.html)
10+
* `MultiPolygon(Polygon[] | Collection<Polygon> $geometries, int|Srid $srid = 0)` - [MySQL MultiPolygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipolygon.html)
11+
* `GeometryCollection(Geometry[] | Collection<Geometry> $geometries, int|Srid $srid = 0)` - [MySQL GeometryCollection](https://dev.mysql.com/doc/refman/8.0/en/gis-class-geometrycollection.html)
1212

1313
Geometry classes can be also created by these static methods:
1414

1515
* `fromArray(array $geometry)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) array.
16-
* `fromJson(string $geoJson, int $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) string.
17-
* `fromWkt(string $wkt, int $srid = 0)` - Creates a geometry object from a [WKT](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry).
18-
* `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).
16+
* `fromJson(string $geoJson, int|Srid $srid = 0)` - Creates a geometry object from a [GeoJSON](https://en.wikipedia.org/wiki/GeoJSON) string.
17+
* `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).
18+
* `fromWkb(string $wkb)` - Creates a geometry object from a [WKB](https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary).
1919

2020
## Available geometry class methods
2121

@@ -58,8 +58,8 @@ An enum is provided with the following values:
5858

5959
| Identifier | Value | Description |
6060
|----------------------|--------|-------------------------------------------------------------------------------------|
61-
| `Srid::WGS84` | `4326` | [Geographic coordinate system](https://epsg.org/crs_4326/WGS-84.html) |
62-
| `Srid::WEB_MERCATOR` | `3857` | [Mercator coordinate system](https://epsg.org/crs_3857/WGS-84-Pseudo-Mercator.html) |
61+
| `Srid::WGS84` | `4326` | [Geographic coordinate system](https://epsg.org/crs_4326/WGS-84.html) |
62+
| `Srid::WEB_MERCATOR` | `3857` | [Mercator coordinate system](https://epsg.org/crs_3857/WGS-84-Pseudo-Mercator.html) |
6363

6464
## Available spatial scopes
6565

src/Objects/Geometry.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use JsonException;
1818
use JsonSerializable;
1919
use MatanYadaev\EloquentSpatial\AxisOrder;
20+
use MatanYadaev\EloquentSpatial\Enums\Srid;
2021
use MatanYadaev\EloquentSpatial\Factory;
2122
use MatanYadaev\EloquentSpatial\GeometryCast;
2223
use Stringable;
@@ -96,10 +97,10 @@ public static function fromWkb(string $wkb): static
9697
*
9798
* @throws InvalidArgumentException
9899
*/
99-
public static function fromWkt(string $wkt, int $srid = 0): static
100+
public static function fromWkt(string $wkt, int|Srid $srid = 0): static
100101
{
101102
$geometry = Factory::parse($wkt);
102-
$geometry->srid = $srid;
103+
$geometry->srid = $srid instanceof Srid ? $srid->value : $srid;
103104

104105
if (! ($geometry instanceof static)) {
105106
throw new InvalidArgumentException(
@@ -117,10 +118,10 @@ public static function fromWkt(string $wkt, int $srid = 0): static
117118
*
118119
* @throws InvalidArgumentException
119120
*/
120-
public static function fromJson(string $geoJson, int $srid = 0): static
121+
public static function fromJson(string $geoJson, int|Srid $srid = 0): static
121122
{
122123
$geometry = Factory::parse($geoJson);
123-
$geometry->srid = $srid;
124+
$geometry->srid = $srid instanceof Srid ? $srid->value : $srid;
124125

125126
if (! ($geometry instanceof static)) {
126127
throw new InvalidArgumentException(

src/Objects/GeometryCollection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Collection;
99
use Illuminate\Support\Str;
1010
use InvalidArgumentException;
11+
use MatanYadaev\EloquentSpatial\Enums\Srid;
1112

1213
class GeometryCollection extends Geometry implements ArrayAccess
1314
{
@@ -24,14 +25,14 @@ class GeometryCollection extends Geometry implements ArrayAccess
2425
*
2526
* @throws InvalidArgumentException
2627
*/
27-
public function __construct(Collection|array $geometries, int $srid = 0)
28+
public function __construct(Collection|array $geometries, int|Srid $srid = 0)
2829
{
2930
if (is_array($geometries)) {
3031
$geometries = collect($geometries);
3132
}
3233

3334
$this->geometries = $geometries;
34-
$this->srid = $srid;
35+
$this->srid = $srid instanceof Srid ? $srid->value : $srid;
3536

3637
$this->validateGeometriesType();
3738
$this->validateGeometriesCount();

src/Objects/MultiLineString.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Support\Collection;
88
use InvalidArgumentException;
9+
use MatanYadaev\EloquentSpatial\Enums\Srid;
910

1011
/**
1112
* @property Collection<int, LineString> $geometries
@@ -26,10 +27,10 @@ class MultiLineString extends GeometryCollection
2627
*
2728
* @throws InvalidArgumentException
2829
*/
29-
public function __construct(Collection|array $geometries, int $srid = 0)
30+
public function __construct(Collection|array $geometries, int|Srid $srid = 0)
3031
{
3132
// @phpstan-ignore-next-line
32-
parent::__construct($geometries, $srid);
33+
parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid);
3334
}
3435

3536
public function toWkt(): string

src/Objects/MultiPolygon.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Support\Collection;
88
use InvalidArgumentException;
9+
use MatanYadaev\EloquentSpatial\Enums\Srid;
910

1011
/**
1112
* @property Collection<int, Polygon> $geometries
@@ -26,10 +27,10 @@ class MultiPolygon extends GeometryCollection
2627
*
2728
* @throws InvalidArgumentException
2829
*/
29-
public function __construct(Collection|array $geometries, int $srid = 0)
30+
public function __construct(Collection|array $geometries, int|Srid $srid = 0)
3031
{
3132
// @phpstan-ignore-next-line
32-
parent::__construct($geometries, $srid);
33+
parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid);
3334
}
3435

3536
public function toWkt(): string

src/Objects/Point.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44

55
namespace MatanYadaev\EloquentSpatial\Objects;
66

7+
use MatanYadaev\EloquentSpatial\Enums\Srid;
8+
79
class Point extends Geometry
810
{
911
public float $latitude;
1012

1113
public float $longitude;
1214

13-
public function __construct(float $latitude, float $longitude, int $srid = 0)
15+
public function __construct(float $latitude, float $longitude, int|Srid $srid = 0)
1416
{
1517
$this->latitude = $latitude;
1618
$this->longitude = $longitude;
17-
$this->srid = $srid;
19+
$this->srid = $srid instanceof Srid ? $srid->value : $srid;
1820
}
1921

2022
public function toWkt(): string

src/Objects/PointCollection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Support\Collection;
88
use InvalidArgumentException;
9+
use MatanYadaev\EloquentSpatial\Enums\Srid;
910

1011
/**
1112
* @property Collection<int, Point> $geometries
@@ -24,9 +25,9 @@ abstract class PointCollection extends GeometryCollection
2425
*
2526
* @throws InvalidArgumentException
2627
*/
27-
public function __construct(Collection|array $geometries, int $srid = 0)
28+
public function __construct(Collection|array $geometries, int|Srid $srid = 0)
2829
{
2930
// @phpstan-ignore-next-line
30-
parent::__construct($geometries, $srid);
31+
parent::__construct($geometries, $this->srid = $srid instanceof Srid ? $srid->value : $srid);
3132
}
3233
}

tests/Objects/GeometryCollectionTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
expect($testPlace->geometry_collection)->toEqual($geometryCollection);
3333
});
3434

35-
it('creates a model record with geometry collection with SRID', function (): void {
35+
it('creates a model record with geometry collection with SRID integer', function (): void {
3636
$geometryCollection = new GeometryCollection([
3737
new Polygon([
3838
new LineString([
@@ -52,6 +52,26 @@
5252
expect($testPlace->geometry_collection->srid)->toBe(Srid::WGS84->value);
5353
});
5454

55+
it('creates a model record with geometry collection with SRID enum', function (): void {
56+
$geometryCollection = new GeometryCollection([
57+
new Polygon([
58+
new LineString([
59+
new Point(0, 180),
60+
new Point(1, 179),
61+
new Point(2, 178),
62+
new Point(3, 177),
63+
new Point(0, 180),
64+
]),
65+
]),
66+
new Point(0, 180),
67+
], Srid::WGS84);
68+
69+
/** @var TestPlace $testPlace */
70+
$testPlace = TestPlace::factory()->create(['geometry_collection' => $geometryCollection]);
71+
72+
expect($testPlace->geometry_collection->srid)->toBe(Srid::WGS84->value);
73+
});
74+
5575
it('creates geometry collection from JSON', function (): void {
5676
$geometryCollection = new GeometryCollection([
5777
new Polygon([

tests/Objects/LineStringTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
expect($testPlace->line_string)->toEqual($lineString);
2424
});
2525

26-
it('creates a model record with line string with SRID', function (): void {
26+
it('creates a model record with line string with SRID integer', function (): void {
2727
$lineString = new LineString([
2828
new Point(0, 180),
2929
new Point(1, 179),
@@ -35,6 +35,18 @@
3535
expect($testPlace->line_string->srid)->toBe(Srid::WGS84->value);
3636
});
3737

38+
it('creates a model record with line string with SRID enum', function (): void {
39+
$lineString = new LineString([
40+
new Point(0, 180),
41+
new Point(1, 179),
42+
], Srid::WGS84);
43+
44+
/** @var TestPlace $testPlace */
45+
$testPlace = TestPlace::factory()->create(['line_string' => $lineString]);
46+
47+
expect($testPlace->line_string->srid)->toBe(Srid::WGS84->value);
48+
});
49+
3850
it('creates line string from JSON', function (): void {
3951
$lineString = new LineString([
4052
new Point(0, 180),

tests/Objects/MultiLineStringTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
expect($testPlace->multi_line_string)->toEqual($multiLineString);
2626
});
2727

28-
it('creates a model record with multi line string with SRID', function (): void {
28+
it('creates a model record with multi line string with SRID integer', function (): void {
2929
$multiLineString = new MultiLineString([
3030
new LineString([
3131
new Point(0, 180),
@@ -39,6 +39,20 @@
3939
expect($testPlace->multi_line_string->srid)->toBe(Srid::WGS84->value);
4040
});
4141

42+
it('creates a model record with multi line string with SRID enum', function (): void {
43+
$multiLineString = new MultiLineString([
44+
new LineString([
45+
new Point(0, 180),
46+
new Point(1, 179),
47+
]),
48+
], Srid::WGS84);
49+
50+
/** @var TestPlace $testPlace */
51+
$testPlace = TestPlace::factory()->create(['multi_line_string' => $multiLineString]);
52+
53+
expect($testPlace->multi_line_string->srid)->toBe(Srid::WGS84->value);
54+
});
55+
4256
it('creates multi line string from JSON', function (): void {
4357
$multiLineString = new MultiLineString([
4458
new LineString([

0 commit comments

Comments
 (0)