Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow srid enum as srid property aside from integer #103

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Point> $geometries, int $srid = 0)` - [MySQL MultiPoint](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipoint.html)
* `LineString(Point[] | Collection<Point> $geometries, int $srid = 0)` - [MySQL LineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-linestring.html)
* `MultiLineString(LineString[] | Collection<LineString> $geometries, int $srid = 0)` - [MySQL MultiLineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multilinestring.html)
* `Polygon(LineString[] | Collection<LineString> $geometries, int $srid = 0)` - [MySQL Polygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-polygon.html)
* `MultiPolygon(Polygon[] | Collection<Polygon> $geometries, int $srid = 0)` - [MySQL MultiPolygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipolygon.html)
* `GeometryCollection(Geometry[] | Collection<Geometry> $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<Point> $geometries, int|Srid $srid = 0)` - [MySQL MultiPoint](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipoint.html)
* `LineString(Point[] | Collection<Point> $geometries, int|Srid $srid = 0)` - [MySQL LineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-linestring.html)
* `MultiLineString(LineString[] | Collection<LineString> $geometries, int|Srid $srid = 0)` - [MySQL MultiLineString](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multilinestring.html)
* `Polygon(LineString[] | Collection<LineString> $geometries, int|Srid $srid = 0)` - [MySQL Polygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-polygon.html)
* `MultiPolygon(Polygon[] | Collection<Polygon> $geometries, int|Srid $srid = 0)` - [MySQL MultiPolygon](https://dev.mysql.com/doc/refman/8.0/en/gis-class-multipolygon.html)
* `GeometryCollection(Geometry[] | Collection<Geometry> $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

Expand Down Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions src/Objects/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
5 changes: 3 additions & 2 deletions src/Objects/GeometryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions src/Objects/MultiLineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Support\Collection;
use InvalidArgumentException;
use MatanYadaev\EloquentSpatial\Enums\Srid;

/**
* @property Collection<int, LineString> $geometries
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Objects/MultiPolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Support\Collection;
use InvalidArgumentException;
use MatanYadaev\EloquentSpatial\Enums\Srid;

/**
* @property Collection<int, Polygon> $geometries
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/Objects/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/Objects/PointCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Support\Collection;
use InvalidArgumentException;
use MatanYadaev\EloquentSpatial\Enums\Srid;

/**
* @property Collection<int, Point> $geometries
Expand All @@ -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);
}
}
22 changes: 21 additions & 1 deletion tests/Objects/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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([
Expand Down
14 changes: 13 additions & 1 deletion tests/Objects/LineStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down
16 changes: 15 additions & 1 deletion tests/Objects/MultiLineStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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([
Expand Down
13 changes: 12 additions & 1 deletion tests/Objects/MultiPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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),
Expand Down
21 changes: 20 additions & 1 deletion tests/Objects/MultiPolygonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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([
Expand Down
11 changes: 10 additions & 1 deletion tests/Objects/PointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);

Expand Down
19 changes: 18 additions & 1 deletion tests/Objects/PolygonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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([
Expand Down
Loading