Skip to content

Commit 19dd241

Browse files
MatanYadaevMatan Yadaev
andauthored
Macroable geometry classes (#66)
* Add tests * Implementation * Documentation * Fix PhpStan * Rename tests Co-authored-by: Matan Yadaev <[email protected]>
1 parent 13ee58a commit 19dd241

File tree

9 files changed

+169
-2
lines changed

9 files changed

+169
-2
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,33 @@ Place::query()->whereDistance(...); // This is IDE-friendly
176176
Place::whereDistance(...); // This is not
177177
```
178178

179+
## Extension
180+
181+
You can extend the package by adding macro methods to the `Geometry` class.
182+
183+
Register a macro in the `boot` method of your service provider:
184+
185+
```php
186+
class AppServiceProvider extends ServiceProvider
187+
{
188+
public function boot(): void
189+
{
190+
Geometry::macro('getName', function (): string {
191+
/** @var Geometry $this */
192+
return class_basename($this);
193+
});
194+
}
195+
}
196+
```
197+
198+
Use the macro in your code:
199+
200+
```php
201+
$londonEyePoint = new Point(51.5032973, -0.1217424);
202+
203+
echo $londonEyePoint->getName(); // Point
204+
```
205+
179206
## Development
180207

181208
* Test: `composer pest`

src/Objects/Geometry.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
1010
use Illuminate\Contracts\Support\Arrayable;
1111
use Illuminate\Contracts\Support\Jsonable;
12+
use Illuminate\Support\Traits\Macroable;
1213
use InvalidArgumentException;
1314
use JsonException;
1415
use JsonSerializable;
@@ -19,6 +20,8 @@
1920

2021
abstract class Geometry implements Castable, Arrayable, Jsonable, JsonSerializable, Stringable
2122
{
23+
use Macroable;
24+
2225
public int $srid = 0;
2326

2427
abstract public function toWkt(): string;

tests/Objects/GeometryCollectionTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Foundation\Testing\DatabaseMigrations;
4+
use MatanYadaev\EloquentSpatial\Objects\Geometry;
45
use MatanYadaev\EloquentSpatial\Objects\GeometryCollection;
56
use MatanYadaev\EloquentSpatial\Objects\LineString;
67
use MatanYadaev\EloquentSpatial\Objects\Point;
@@ -365,3 +366,27 @@
365366

366367
expect($geometryCollection->__toString())->toEqual('GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))');
367368
});
369+
370+
it('adds a macro toGeometryCollection', function (): void {
371+
Geometry::macro('getName', function (): string {
372+
/** @var Geometry $this */
373+
// @phpstan-ignore-next-line
374+
return class_basename($this);
375+
});
376+
377+
$geometryCollection = new GeometryCollection([
378+
new Polygon([
379+
new LineString([
380+
new Point(0, 180),
381+
new Point(1, 179),
382+
new Point(2, 178),
383+
new Point(3, 177),
384+
new Point(0, 180),
385+
]),
386+
]),
387+
new Point(0, 180),
388+
]);
389+
390+
// @phpstan-ignore-next-line
391+
expect($geometryCollection->getName())->toBe('GeometryCollection');
392+
});

tests/Objects/LineStringTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Foundation\Testing\DatabaseMigrations;
4+
use MatanYadaev\EloquentSpatial\Objects\Geometry;
45
use MatanYadaev\EloquentSpatial\Objects\LineString;
56
use MatanYadaev\EloquentSpatial\Objects\Point;
67
use MatanYadaev\EloquentSpatial\Objects\Polygon;
@@ -160,3 +161,19 @@
160161

161162
expect($lineString->__toString())->toEqual('LINESTRING(180 0, 179 1)');
162163
});
164+
165+
it('adds a macro toLineString', function (): void {
166+
Geometry::macro('getName', function (): string {
167+
/** @var Geometry $this */
168+
// @phpstan-ignore-next-line
169+
return class_basename($this);
170+
});
171+
172+
$lineString = new LineString([
173+
new Point(0, 180),
174+
new Point(1, 179),
175+
]);
176+
177+
// @phpstan-ignore-next-line
178+
expect($lineString->getName())->toBe('LineString');
179+
});

tests/Objects/MultiLineStringTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Foundation\Testing\DatabaseMigrations;
4+
use MatanYadaev\EloquentSpatial\Objects\Geometry;
45
use MatanYadaev\EloquentSpatial\Objects\LineString;
56
use MatanYadaev\EloquentSpatial\Objects\MultiLineString;
67
use MatanYadaev\EloquentSpatial\Objects\Point;
@@ -182,3 +183,21 @@
182183

183184
expect($multiLineString->__toString())->toEqual('MULTILINESTRING((180 0, 179 1))');
184185
});
186+
187+
it('adds a macro toMultiLineString', function (): void {
188+
Geometry::macro('getName', function (): string {
189+
/** @var Geometry $this */
190+
// @phpstan-ignore-next-line
191+
return class_basename($this);
192+
});
193+
194+
$multiLineString = new MultiLineString([
195+
new LineString([
196+
new Point(0, 180),
197+
new Point(1, 179),
198+
]),
199+
]);
200+
201+
// @phpstan-ignore-next-line
202+
expect($multiLineString->getName())->toBe('MultiLineString');
203+
});

tests/Objects/MultiPointTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Foundation\Testing\DatabaseMigrations;
4+
use MatanYadaev\EloquentSpatial\Objects\Geometry;
45
use MatanYadaev\EloquentSpatial\Objects\MultiPoint;
56
use MatanYadaev\EloquentSpatial\Objects\Point;
67
use MatanYadaev\EloquentSpatial\Objects\Polygon;
@@ -146,3 +147,18 @@
146147

147148
expect($multiPoint->__toString())->toEqual('MULTIPOINT(180 0)');
148149
});
150+
151+
it('adds a macro toMultiPoint', function (): void {
152+
Geometry::macro('getName', function (): string {
153+
/** @var Geometry $this */
154+
// @phpstan-ignore-next-line
155+
return class_basename($this);
156+
});
157+
158+
$multiPoint = new MultiPoint([
159+
new Point(0, 180),
160+
]);
161+
162+
// @phpstan-ignore-next-line
163+
expect($multiPoint->getName())->toBe('MultiPoint');
164+
});

tests/Objects/MultiPolygonTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Foundation\Testing\DatabaseMigrations;
4+
use MatanYadaev\EloquentSpatial\Objects\Geometry;
45
use MatanYadaev\EloquentSpatial\Objects\LineString;
56
use MatanYadaev\EloquentSpatial\Objects\MultiPolygon;
67
use MatanYadaev\EloquentSpatial\Objects\Point;
@@ -239,7 +240,30 @@
239240
new Point(0, 180),
240241
]),
241242
]),
242-
], 4326);
243+
]);
243244

244245
expect($multiPolygon->__toString())->toEqual('MULTIPOLYGON(((180 0, 179 1, 178 2, 177 3, 180 0)))');
245246
});
247+
248+
it('adds a macro toMultiPolygon', function (): void {
249+
Geometry::macro('getName', function (): string {
250+
/** @var Geometry $this */
251+
// @phpstan-ignore-next-line
252+
return class_basename($this);
253+
});
254+
255+
$multiPolygon = new MultiPolygon([
256+
new Polygon([
257+
new LineString([
258+
new Point(0, 180),
259+
new Point(1, 179),
260+
new Point(2, 178),
261+
new Point(3, 177),
262+
new Point(0, 180),
263+
]),
264+
]),
265+
]);
266+
267+
// @phpstan-ignore-next-line
268+
expect($multiPolygon->getName())->toBe('MultiPolygon');
269+
});

tests/Objects/PointTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Foundation\Testing\DatabaseMigrations;
4+
use MatanYadaev\EloquentSpatial\Objects\Geometry;
45
use MatanYadaev\EloquentSpatial\Objects\Point;
56
use MatanYadaev\EloquentSpatial\Tests\TestModels\TestPlace;
67

@@ -102,3 +103,16 @@
102103

103104
expect($point->__toString())->toEqual('POINT(180 0)');
104105
});
106+
107+
it('adds a macro toPoint', function (): void {
108+
Geometry::macro('getName', function (): string {
109+
/** @var Geometry $this */
110+
// @phpstan-ignore-next-line
111+
return class_basename($this);
112+
});
113+
114+
$point = new Point(0, 180, 4326);
115+
116+
// @phpstan-ignore-next-line
117+
expect($point->getName())->toBe('Point');
118+
});

tests/Objects/PolygonTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Illuminate\Foundation\Testing\DatabaseMigrations;
4+
use MatanYadaev\EloquentSpatial\Objects\Geometry;
45
use MatanYadaev\EloquentSpatial\Objects\LineString;
56
use MatanYadaev\EloquentSpatial\Objects\Point;
67
use MatanYadaev\EloquentSpatial\Objects\Polygon;
@@ -206,7 +207,7 @@
206207
});
207208

208209
it('casts a Polygon to a string', function (): void {
209-
$polygon = $polygon = new Polygon([
210+
$polygon = new Polygon([
210211
new LineString([
211212
new Point(0, 180),
212213
new Point(1, 179),
@@ -218,3 +219,24 @@
218219

219220
expect($polygon->__toString())->toEqual('POLYGON((180 0, 179 1, 178 2, 177 3, 180 0))');
220221
});
222+
223+
it('adds a macro toPolygon', function (): void {
224+
Geometry::macro('getName', function (): string {
225+
/** @var Geometry $this */
226+
// @phpstan-ignore-next-line
227+
return class_basename($this);
228+
});
229+
230+
$polygon = new Polygon([
231+
new LineString([
232+
new Point(0, 180),
233+
new Point(1, 179),
234+
new Point(2, 178),
235+
new Point(3, 177),
236+
new Point(0, 180),
237+
]),
238+
]);
239+
240+
// @phpstan-ignore-next-line
241+
expect($polygon->getName())->toBe('Polygon');
242+
});

0 commit comments

Comments
 (0)