Skip to content
This repository was archived by the owner on Jun 28, 2021. It is now read-only.

Commit 21e6e85

Browse files
committed
Geometry and Geography implements JsonSerializable
creof#194
1 parent 55bef51 commit 21e6e85

File tree

8 files changed

+75
-7
lines changed

8 files changed

+75
-7
lines changed

lib/CrEOF/Spatial/PHP/Types/AbstractGeometry.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626

2727
use CrEOF\Spatial\Exception\InvalidValueException;
2828
use CrEOF\Spatial\PHP\Types\Geometry\GeometryInterface;
29+
use JsonSerializable;
2930

3031
/**
3132
* Abstract geometry object for spatial types.
3233
*
3334
* @author Derek J. Lambert <[email protected]>
3435
* @license https://dlambert.mit-license.org MIT
3536
*/
36-
abstract class AbstractGeometry implements GeometryInterface
37+
abstract class AbstractGeometry implements GeometryInterface, JsonSerializable
3738
{
3839
/**
3940
* Spatial Reference System Identifier.
@@ -52,6 +53,26 @@ public function getSrid()
5253
return $this->srid;
5354
}
5455

56+
/**
57+
* Specify data which should be serialized to JSON.
58+
*
59+
* @see https://php.net/manual/en/jsonserializable.jsonserialize.php
60+
* @see https://github.com/creof/doctrine2-spatial/issues/140
61+
*
62+
* @return mixed data which can be serialized by <b>json_encode</b>,
63+
* which is a value of any type other than a resource
64+
*
65+
* @since 2.0.0.rc-1
66+
*/
67+
public function jsonSerialize()
68+
{
69+
return [
70+
'type' => $this->getType(),
71+
'coordinates' => $this->toArray(),
72+
'srid' => $this->getSrid(),
73+
];
74+
}
75+
5576
/**
5677
* Spatial Reference System Identifier fluent setter.
5778
*
@@ -85,6 +106,7 @@ public function toJson()
85106
$json = [];
86107
$json['type'] = $this->getType();
87108
$json['coordinates'] = $this->toArray();
109+
$json['srid'] = $this->getSrid();
88110

89111
return json_encode($json);
90112
}

tests/CrEOF/Spatial/Tests/PHP/Types/Geography/PointTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,21 @@ public function testGoodNumericPoint()
214214
public function testGoodStringPoints()
215215
{
216216
$point = new Point('79:56:55W', '40:26:46N');
217+
$expected = '{"type":"Point","coordinates":[-79.9486111111111,40.44611111111111],"srid":null}';
217218

218219
static::assertEquals(40.446111111111, $point->getLatitude());
219220
static::assertEquals(-79.948611111111, $point->getLongitude());
221+
static::assertEquals($expected, $point->toJson());
222+
static::assertEquals($expected, json_encode($point));
220223

221224
$point = new Point('79°56\'55"W', '40°26\'46"N');
225+
$point->setSrid(4326);
226+
$expected = '{"type":"Point","coordinates":[-79.9486111111111,40.44611111111111],"srid":4326}';
222227

223228
static::assertEquals(40.446111111111, $point->getLatitude());
224229
static::assertEquals(-79.948611111111, $point->getLongitude());
230+
static::assertEquals($expected, $point->toJson());
231+
static::assertEquals($expected, json_encode($point));
225232

226233
$point = new Point('79° 56\' 55" W', '40° 26\' 46" N');
227234

tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/LineStringTest.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function testEmptyLineString()
6565
*/
6666
public function testJson()
6767
{
68-
$expected = '{"type":"LineString","coordinates":[[0,0],[0,5],[5,0],[0,0]]}';
68+
$expected = '{"type":"LineString","coordinates":[[0,0],[0,5],[5,0],[0,0]],"srid":null}';
6969

7070
$lineString = new LineString(
7171
[
@@ -76,6 +76,11 @@ public function testJson()
7676
]
7777
);
7878
static::assertEquals($expected, $lineString->toJson());
79+
80+
$expected = '{"type":"LineString","coordinates":[[0,0],[0,5],[5,0],[0,0]],"srid":4326}';
81+
$lineString->setSrid(4326);
82+
static::assertEquals($expected, $lineString->toJson());
83+
static::assertEquals($expected, json_encode($lineString));
7984
}
8085

8186
/**

tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiLineStringTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function testEmptyMultiLineString()
6060
public function testJson()
6161
{
6262
// phpcs:disable Generic.Files.LineLength.MaxExceeded
63-
$expected = '{"type":"MultiLineString","coordinates":[[[0,0],[10,0],[10,10],[0,10],[0,0]],[[0,0],[10,0],[10,10],[0,10],[0,0]]]}';
63+
$expected = '{"type":"MultiLineString","coordinates":[[[0,0],[10,0],[10,10],[0,10],[0,0]],[[0,0],[10,0],[10,10],[0,10],[0,0]]],"srid":null}';
6464
// phpcs:enable
6565
$lineStrings = [
6666
[
@@ -81,6 +81,13 @@ public function testJson()
8181
$multiLineString = new MultiLineString($lineStrings);
8282

8383
static::assertEquals($expected, $multiLineString->toJson());
84+
static::assertEquals($expected, json_encode($multiLineString));
85+
// phpcs:disable Generic.Files.LineLength.MaxExceeded
86+
$expected = '{"type":"MultiLineString","coordinates":[[[0,0],[10,0],[10,10],[0,10],[0,0]],[[0,0],[10,0],[10,10],[0,10],[0,0]]],"srid":4326}';
87+
// phpcs:enable
88+
$multiLineString->setSrid(4326);
89+
static::assertEquals($expected, $multiLineString->toJson());
90+
static::assertEquals($expected, json_encode($multiLineString));
8491
}
8592

8693
/**

tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiPointTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function testEmptyMultiPoint()
7474
*/
7575
public function testJson()
7676
{
77-
$expected = '{"type":"MultiPoint","coordinates":[[0,0],[0,5],[5,0],[0,0]]}';
77+
$expected = '{"type":"MultiPoint","coordinates":[[0,0],[0,5],[5,0],[0,0]],"srid":null}';
7878
$multiPoint = new MultiPoint(
7979
[
8080
[0, 0],
@@ -85,6 +85,12 @@ public function testJson()
8585
);
8686

8787
static::assertEquals($expected, $multiPoint->toJson());
88+
static::assertEquals($expected, json_encode($multiPoint));
89+
90+
$expected = '{"type":"MultiPoint","coordinates":[[0,0],[0,5],[5,0],[0,0]],"srid":4326}';
91+
$multiPoint->setSrid(4326);
92+
static::assertEquals($expected, $multiPoint->toJson());
93+
static::assertEquals($expected, json_encode($multiPoint));
8894
}
8995

9096
/**

tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/MultiPolygonTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function testEmptyMultiPolygon()
6464
public function testJson()
6565
{
6666
// phpcs:disable Generic.Files.LineLength.MaxExceeded
67-
$expected = '{"type":"MultiPolygon","coordinates":[[[[0,0],[10,0],[10,10],[0,10],[0,0]]],[[[5,5],[7,5],[7,7],[5,7],[5,5]]]]}';
67+
$expected = '{"type":"MultiPolygon","coordinates":[[[[0,0],[10,0],[10,10],[0,10],[0,0]]],[[[5,5],[7,5],[7,7],[5,7],[5,5]]]],"srid":null}';
6868
// phpcs:enable
6969
$polygons = [
7070
[
@@ -89,6 +89,14 @@ public function testJson()
8989
$multiPolygon = new MultiPolygon($polygons);
9090

9191
static::assertEquals($expected, $multiPolygon->toJson());
92+
static::assertEquals($expected, json_encode($multiPolygon));
93+
94+
// phpcs:disable Generic.Files.LineLength.MaxExceeded
95+
$expected = '{"type":"MultiPolygon","coordinates":[[[[0,0],[10,0],[10,10],[0,10],[0,0]]],[[[5,5],[7,5],[7,7],[5,7],[5,5]]]],"srid":4326}';
96+
// phpcs:enable
97+
$multiPolygon->setSrid(4326);
98+
static::assertEquals($expected, $multiPolygon->toJson());
99+
static::assertEquals($expected, json_encode($multiPolygon));
92100
}
93101

94102
/**

tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/PointTest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,16 @@ public function testGoodStringPoints()
206206
*/
207207
public function testJson()
208208
{
209-
$expected = '{"type":"Point","coordinates":[5,5]}';
209+
$expected = '{"type":"Point","coordinates":[5,5],"srid":null}';
210210
$point = new Point([5, 5]);
211211

212212
static::assertEquals($expected, $point->toJson());
213+
static::assertEquals($expected, json_encode($point));
214+
215+
$point->setSrid(4326);
216+
$expected = '{"type":"Point","coordinates":[5,5],"srid":4326}';
217+
static::assertEquals($expected, $point->toJson());
218+
static::assertEquals($expected, json_encode($point));
213219
}
214220

215221
/**

tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/PolygonTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function testEmptyPolygon()
5656
public function testJson()
5757
{
5858
// phpcs:disable Generic.Files.LineLength.MaxExceeded
59-
$expected = '{"type":"Polygon","coordinates":[[[0,0],[10,0],[10,10],[0,10],[0,0]],[[0,0],[10,0],[10,10],[0,10],[0,0]]]}';
59+
$expected = '{"type":"Polygon","coordinates":[[[0,0],[10,0],[10,10],[0,10],[0,0]],[[0,0],[10,0],[10,10],[0,10],[0,0]]],"srid":null}';
6060
// phpcs:enable
6161
$rings = [
6262
[
@@ -75,8 +75,15 @@ public function testJson()
7575
],
7676
];
7777
$polygon = new Polygon($rings);
78+
static::assertEquals($expected, $polygon->toJson());
79+
static::assertEquals($expected, json_encode($polygon));
7880

81+
// phpcs:disable Generic.Files.LineLength.MaxExceeded
82+
$expected = '{"type":"Polygon","coordinates":[[[0,0],[10,0],[10,10],[0,10],[0,0]],[[0,0],[10,0],[10,10],[0,10],[0,0]]],"srid":4326}';
83+
// phpcs:enable
84+
$polygon->setSrid(4326);
7985
static::assertEquals($expected, $polygon->toJson());
86+
static::assertEquals($expected, json_encode($polygon));
8087
}
8188

8289
/**

0 commit comments

Comments
 (0)