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 1 commit
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
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