Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6fb3089

Browse files
authoredSep 24, 2022
Make Geomtry objects stringable (#58)
* added __toString magic method I needed a way to cast the geometry to a string. * implement Stringable and return toWkt * Object cast to string unit tests
1 parent c7c6410 commit 6fb3089

File tree

8 files changed

+91
-1
lines changed

8 files changed

+91
-1
lines changed
 

‎src/Objects/Geometry.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,25 @@
1414
use JsonSerializable;
1515
use MatanYadaev\EloquentSpatial\Factory;
1616
use MatanYadaev\EloquentSpatial\GeometryCast;
17+
use Stringable;
1718
use WKB as geoPHPWkb;
1819

19-
abstract class Geometry implements Castable, Arrayable, Jsonable, JsonSerializable
20+
abstract class Geometry implements Castable, Arrayable, Jsonable, JsonSerializable, Stringable
2021
{
2122
public int $srid = 0;
2223

2324
abstract public function toWkt(): string;
2425

2526
abstract public function getWktData(): string;
2627

28+
/**
29+
* @return string
30+
*/
31+
public function __toString(): string
32+
{
33+
return $this->toWkt();
34+
}
35+
2736
/**
2837
* @param int $options
2938
* @return string

‎tests/Objects/GeometryCollectionTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,20 @@
348348
$polygon[1] = new Point(0, 180);
349349
})->toThrow(InvalidArgumentException::class);
350350
});
351+
352+
it('casts a GeometryCollection to a string', function (): void {
353+
$geometryCollection = new GeometryCollection([
354+
new Polygon([
355+
new LineString([
356+
new Point(0, 180),
357+
new Point(1, 179),
358+
new Point(2, 178),
359+
new Point(3, 177),
360+
new Point(0, 180),
361+
]),
362+
]),
363+
new Point(0, 180),
364+
]);
365+
366+
expect($geometryCollection->__toString())->toEqual('GEOMETRYCOLLECTION(POLYGON((180 0, 179 1, 178 2, 177 3, 180 0)), POINT(180 0))');
367+
});

‎tests/Objects/LineStringTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,12 @@
151151
]);
152152
})->toThrow(InvalidArgumentException::class);
153153
});
154+
155+
it('casts a LineString to a string', function (): void {
156+
$lineString = new LineString([
157+
new Point(0, 180),
158+
new Point(1, 179),
159+
]);
160+
161+
expect($lineString->__toString())->toEqual('LINESTRING(180 0, 179 1)');
162+
});

‎tests/Objects/MultiLineStringTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,14 @@
171171
]);
172172
})->toThrow(InvalidArgumentException::class);
173173
});
174+
175+
it('casts a MultiLineString to a string', function (): void {
176+
$multiLineString = new MultiLineString([
177+
new LineString([
178+
new Point(0, 180),
179+
new Point(1, 179),
180+
]),
181+
]);
182+
183+
expect($multiLineString->__toString())->toEqual('MULTILINESTRING((180 0, 179 1))');
184+
});

‎tests/Objects/MultiPointTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,11 @@
138138
]);
139139
})->toThrow(InvalidArgumentException::class);
140140
});
141+
142+
it('casts a MultiPoint to a string', function (): void {
143+
$multiPoint = new MultiPoint([
144+
new Point(0, 180),
145+
]);
146+
147+
expect($multiPoint->__toString())->toEqual('MULTIPOINT(180 0)');
148+
});

‎tests/Objects/MultiPolygonTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,19 @@
227227
]);
228228
})->toThrow(InvalidArgumentException::class);
229229
});
230+
231+
it('casts a MultiPolygon to a string', function (): void {
232+
$multiPolygon = new MultiPolygon([
233+
new Polygon([
234+
new LineString([
235+
new Point(0, 180),
236+
new Point(1, 179),
237+
new Point(2, 178),
238+
new Point(3, 177),
239+
new Point(0, 180),
240+
]),
241+
]),
242+
], 4326);
243+
244+
expect($multiPolygon->__toString())->toEqual('MULTIPOLYGON(((180 0, 179 1, 178 2, 177 3, 180 0)))');
245+
});

‎tests/Objects/PointTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,9 @@
9696

9797
expect($pointFromWkb)->toEqual($point);
9898
});
99+
100+
it('casts a Point to a string', function (): void {
101+
$point = new Point(0, 180, 4326);
102+
103+
expect($point->__toString())->toEqual('POINT(180 0)');
104+
});

‎tests/Objects/PolygonTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,17 @@
204204
]);
205205
})->toThrow(InvalidArgumentException::class);
206206
});
207+
208+
it('casts a Polygon to a string', function (): void {
209+
$polygon = $polygon = new Polygon([
210+
new LineString([
211+
new Point(0, 180),
212+
new Point(1, 179),
213+
new Point(2, 178),
214+
new Point(3, 177),
215+
new Point(0, 180),
216+
]),
217+
]);
218+
219+
expect($polygon->__toString())->toEqual('POLYGON((180 0, 179 1, 178 2, 177 3, 180 0))');
220+
});

0 commit comments

Comments
 (0)