Skip to content
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
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
3 changes: 3 additions & 0 deletions src/Objects/Geometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,6 +20,8 @@

abstract class Geometry implements Castable, Arrayable, Jsonable, JsonSerializable, Stringable
{
use Macroable;

public int $srid = 0;

abstract public function toWkt(): string;
Expand Down
25 changes: 25 additions & 0 deletions tests/Objects/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Foundation\Testing\DatabaseMigrations;
use MatanYadaev\EloquentSpatial\Objects\Geometry;
use MatanYadaev\EloquentSpatial\Objects\GeometryCollection;
use MatanYadaev\EloquentSpatial\Objects\LineString;
use MatanYadaev\EloquentSpatial\Objects\Point;
Expand Down Expand Up @@ -365,3 +366,27 @@

expect($geometryCollection->__toString())->toEqual('GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))');
});

it('adds a macro toGeometryCollection', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
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');
});
17 changes: 17 additions & 0 deletions tests/Objects/LineStringTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Foundation\Testing\DatabaseMigrations;
use MatanYadaev\EloquentSpatial\Objects\Geometry;
use MatanYadaev\EloquentSpatial\Objects\LineString;
use MatanYadaev\EloquentSpatial\Objects\Point;
use MatanYadaev\EloquentSpatial\Objects\Polygon;
Expand Down Expand Up @@ -160,3 +161,19 @@

expect($lineString->__toString())->toEqual('LINESTRING(180 0, 179 1)');
});

it('adds a macro toLineString', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
return class_basename($this);
});

$lineString = new LineString([
new Point(0, 180),
new Point(1, 179),
]);

// @phpstan-ignore-next-line
expect($lineString->getName())->toBe('LineString');
});
19 changes: 19 additions & 0 deletions tests/Objects/MultiLineStringTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Foundation\Testing\DatabaseMigrations;
use MatanYadaev\EloquentSpatial\Objects\Geometry;
use MatanYadaev\EloquentSpatial\Objects\LineString;
use MatanYadaev\EloquentSpatial\Objects\MultiLineString;
use MatanYadaev\EloquentSpatial\Objects\Point;
Expand Down Expand Up @@ -182,3 +183,21 @@

expect($multiLineString->__toString())->toEqual('MULTILINESTRING((180 0, 179 1))');
});

it('adds a macro toMultiLineString', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
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');
});
16 changes: 16 additions & 0 deletions tests/Objects/MultiPointTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Foundation\Testing\DatabaseMigrations;
use MatanYadaev\EloquentSpatial\Objects\Geometry;
use MatanYadaev\EloquentSpatial\Objects\MultiPoint;
use MatanYadaev\EloquentSpatial\Objects\Point;
use MatanYadaev\EloquentSpatial\Objects\Polygon;
Expand Down Expand Up @@ -146,3 +147,18 @@

expect($multiPoint->__toString())->toEqual('MULTIPOINT(180 0)');
});

it('adds a macro toMultiPoint', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
return class_basename($this);
});

$multiPoint = new MultiPoint([
new Point(0, 180),
]);

// @phpstan-ignore-next-line
expect($multiPoint->getName())->toBe('MultiPoint');
});
26 changes: 25 additions & 1 deletion tests/Objects/MultiPolygonTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Foundation\Testing\DatabaseMigrations;
use MatanYadaev\EloquentSpatial\Objects\Geometry;
use MatanYadaev\EloquentSpatial\Objects\LineString;
use MatanYadaev\EloquentSpatial\Objects\MultiPolygon;
use MatanYadaev\EloquentSpatial\Objects\Point;
Expand Down Expand Up @@ -239,7 +240,30 @@
new Point(0, 180),
]),
]),
], 4326);
]);

expect($multiPolygon->__toString())->toEqual('MULTIPOLYGON(((180 0, 179 1, 178 2, 177 3, 180 0)))');
});

it('adds a macro toMultiPolygon', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
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');
});
14 changes: 14 additions & 0 deletions tests/Objects/PointTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Foundation\Testing\DatabaseMigrations;
use MatanYadaev\EloquentSpatial\Objects\Geometry;
use MatanYadaev\EloquentSpatial\Objects\Point;
use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace;

Expand Down Expand Up @@ -102,3 +103,16 @@

expect($point->__toString())->toEqual('POINT(180 0)');
});

it('adds a macro toPoint', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
return class_basename($this);
});

$point = new Point(0, 180, 4326);

// @phpstan-ignore-next-line
expect($point->getName())->toBe('Point');
});
24 changes: 23 additions & 1 deletion tests/Objects/PolygonTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Foundation\Testing\DatabaseMigrations;
use MatanYadaev\EloquentSpatial\Objects\Geometry;
use MatanYadaev\EloquentSpatial\Objects\LineString;
use MatanYadaev\EloquentSpatial\Objects\Point;
use MatanYadaev\EloquentSpatial\Objects\Polygon;
Expand Down Expand Up @@ -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),
Expand All @@ -218,3 +219,24 @@

expect($polygon->__toString())->toEqual('POLYGON((180 0, 179 1, 178 2, 177 3, 180 0))');
});

it('adds a macro toPolygon', function (): void {
Geometry::macro('getName', function (): string {
/** @var Geometry $this */
// @phpstan-ignore-next-line
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');
});