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

Commit

Permalink
Geometry and Geography implements JsonSerializable
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre-T committed Mar 28, 2020
1 parent 55bef51 commit 21e6e85
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 7 deletions.
24 changes: 23 additions & 1 deletion lib/CrEOF/Spatial/PHP/Types/AbstractGeometry.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@

use CrEOF\Spatial\Exception\InvalidValueException;
use CrEOF\Spatial\PHP\Types\Geometry\GeometryInterface;
use JsonSerializable;

/**
* Abstract geometry object for spatial types.
*
* @author Derek J. Lambert <dlambert@dereklambert.com>
* @license https://dlambert.mit-license.org MIT
*/
abstract class AbstractGeometry implements GeometryInterface
abstract class AbstractGeometry implements GeometryInterface, JsonSerializable
{
/**
* Spatial Reference System Identifier.
Expand All @@ -52,6 +53,26 @@ public function getSrid()
return $this->srid;
}

/**
* Specify data which should be serialized to JSON.
*
* @see https://php.net/manual/en/jsonserializable.jsonserialize.php
* @see https://github.com/creof/doctrine2-spatial/issues/140
*
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource
*
* @since 2.0.0.rc-1
*/
public function jsonSerialize()
{
return [
'type' => $this->getType(),
'coordinates' => $this->toArray(),
'srid' => $this->getSrid(),
];
}

/**
* Spatial Reference System Identifier fluent setter.
*
Expand Down Expand Up @@ -85,6 +106,7 @@ public function toJson()
$json = [];
$json['type'] = $this->getType();
$json['coordinates'] = $this->toArray();
$json['srid'] = $this->getSrid();

return json_encode($json);
}
Expand Down
7 changes: 7 additions & 0 deletions tests/CrEOF/Spatial/Tests/PHP/Types/Geography/PointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,21 @@ public function testGoodNumericPoint()
public function testGoodStringPoints()
{
$point = new Point('79:56:55W', '40:26:46N');
$expected = '{"type":"Point","coordinates":[-79.9486111111111,40.44611111111111],"srid":null}';

static::assertEquals(40.446111111111, $point->getLatitude());
static::assertEquals(-79.948611111111, $point->getLongitude());
static::assertEquals($expected, $point->toJson());
static::assertEquals($expected, json_encode($point));

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

static::assertEquals(40.446111111111, $point->getLatitude());
static::assertEquals(-79.948611111111, $point->getLongitude());
static::assertEquals($expected, $point->toJson());
static::assertEquals($expected, json_encode($point));

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testEmptyLineString()
*/
public function testJson()
{
$expected = '{"type":"LineString","coordinates":[[0,0],[0,5],[5,0],[0,0]]}';
$expected = '{"type":"LineString","coordinates":[[0,0],[0,5],[5,0],[0,0]],"srid":null}';

$lineString = new LineString(
[
Expand All @@ -76,6 +76,11 @@ public function testJson()
]
);
static::assertEquals($expected, $lineString->toJson());

$expected = '{"type":"LineString","coordinates":[[0,0],[0,5],[5,0],[0,0]],"srid":4326}';
$lineString->setSrid(4326);
static::assertEquals($expected, $lineString->toJson());
static::assertEquals($expected, json_encode($lineString));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testEmptyMultiLineString()
public function testJson()
{
// phpcs:disable Generic.Files.LineLength.MaxExceeded
$expected = '{"type":"MultiLineString","coordinates":[[[0,0],[10,0],[10,10],[0,10],[0,0]],[[0,0],[10,0],[10,10],[0,10],[0,0]]]}';
$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}';
// phpcs:enable
$lineStrings = [
[
Expand All @@ -81,6 +81,13 @@ public function testJson()
$multiLineString = new MultiLineString($lineStrings);

static::assertEquals($expected, $multiLineString->toJson());
static::assertEquals($expected, json_encode($multiLineString));
// phpcs:disable Generic.Files.LineLength.MaxExceeded
$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}';
// phpcs:enable
$multiLineString->setSrid(4326);
static::assertEquals($expected, $multiLineString->toJson());
static::assertEquals($expected, json_encode($multiLineString));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testEmptyMultiPoint()
*/
public function testJson()
{
$expected = '{"type":"MultiPoint","coordinates":[[0,0],[0,5],[5,0],[0,0]]}';
$expected = '{"type":"MultiPoint","coordinates":[[0,0],[0,5],[5,0],[0,0]],"srid":null}';
$multiPoint = new MultiPoint(
[
[0, 0],
Expand All @@ -85,6 +85,12 @@ public function testJson()
);

static::assertEquals($expected, $multiPoint->toJson());
static::assertEquals($expected, json_encode($multiPoint));

$expected = '{"type":"MultiPoint","coordinates":[[0,0],[0,5],[5,0],[0,0]],"srid":4326}';
$multiPoint->setSrid(4326);
static::assertEquals($expected, $multiPoint->toJson());
static::assertEquals($expected, json_encode($multiPoint));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function testEmptyMultiPolygon()
public function testJson()
{
// phpcs:disable Generic.Files.LineLength.MaxExceeded
$expected = '{"type":"MultiPolygon","coordinates":[[[[0,0],[10,0],[10,10],[0,10],[0,0]]],[[[5,5],[7,5],[7,7],[5,7],[5,5]]]]}';
$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}';
// phpcs:enable
$polygons = [
[
Expand All @@ -89,6 +89,14 @@ public function testJson()
$multiPolygon = new MultiPolygon($polygons);

static::assertEquals($expected, $multiPolygon->toJson());
static::assertEquals($expected, json_encode($multiPolygon));

// phpcs:disable Generic.Files.LineLength.MaxExceeded
$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}';
// phpcs:enable
$multiPolygon->setSrid(4326);
static::assertEquals($expected, $multiPolygon->toJson());
static::assertEquals($expected, json_encode($multiPolygon));
}

/**
Expand Down
8 changes: 7 additions & 1 deletion tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/PointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,16 @@ public function testGoodStringPoints()
*/
public function testJson()
{
$expected = '{"type":"Point","coordinates":[5,5]}';
$expected = '{"type":"Point","coordinates":[5,5],"srid":null}';
$point = new Point([5, 5]);

static::assertEquals($expected, $point->toJson());
static::assertEquals($expected, json_encode($point));

$point->setSrid(4326);
$expected = '{"type":"Point","coordinates":[5,5],"srid":4326}';
static::assertEquals($expected, $point->toJson());
static::assertEquals($expected, json_encode($point));
}

/**
Expand Down
9 changes: 8 additions & 1 deletion tests/CrEOF/Spatial/Tests/PHP/Types/Geometry/PolygonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function testEmptyPolygon()
public function testJson()
{
// phpcs:disable Generic.Files.LineLength.MaxExceeded
$expected = '{"type":"Polygon","coordinates":[[[0,0],[10,0],[10,10],[0,10],[0,0]],[[0,0],[10,0],[10,10],[0,10],[0,0]]]}';
$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}';
// phpcs:enable
$rings = [
[
Expand All @@ -75,8 +75,15 @@ public function testJson()
],
];
$polygon = new Polygon($rings);
static::assertEquals($expected, $polygon->toJson());
static::assertEquals($expected, json_encode($polygon));

// phpcs:disable Generic.Files.LineLength.MaxExceeded
$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}';
// phpcs:enable
$polygon->setSrid(4326);
static::assertEquals($expected, $polygon->toJson());
static::assertEquals($expected, json_encode($polygon));
}

/**
Expand Down

0 comments on commit 21e6e85

Please sign in to comment.