Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GeoJSON type constants #42

Merged
merged 2 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/Feature/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class Feature extends GeoJson
{
protected string $type = 'Feature';
protected string $type = self::TYPE_FEATURE;

protected ?Geometry $geometry;

Expand Down
2 changes: 1 addition & 1 deletion src/Feature/FeatureCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
class FeatureCollection extends GeoJson implements Countable, IteratorAggregate
{
protected string $type = 'FeatureCollection';
protected string $type = self::TYPE_FEATURE_COLLECTION;

/**
* @var array<Feature>
Expand Down
30 changes: 20 additions & 10 deletions src/GeoJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@
*/
abstract class GeoJson implements JsonSerializable, JsonUnserializable
{
public const TYPE_LINESTRING = 'LineString';
jmikola marked this conversation as resolved.
Show resolved Hide resolved
public const TYPE_MULTI_LINE_STRING = 'MultiLineString';
public const TYPE_MULTI_POINT = 'MultiPoint';
public const TYPE_MULTI_POLYGON = 'MultiPolygon';
public const TYPE_POINT = 'Point';
public const TYPE_POLYGON = 'Polygon';
public const TYPE_FEATURE = 'Feature';
public const TYPE_FEATURE_COLLECTION = 'FeatureCollection';
public const TYPE_GEOMETRY_COLLECTION = 'GeometryCollection';

protected ?BoundingBox $boundingBox = null;

protected ?CoordinateReferenceSystem $crs = null;
Expand Down Expand Up @@ -87,12 +97,12 @@ final public static function jsonUnserialize($json): self
$args = [];

switch ($type) {
case 'LineString':
case 'MultiLineString':
case 'MultiPoint':
case 'MultiPolygon':
case 'Point':
case 'Polygon':
case self::TYPE_LINESTRING:
jmikola marked this conversation as resolved.
Show resolved Hide resolved
case self::TYPE_MULTI_LINE_STRING:
case self::TYPE_MULTI_POINT:
case self::TYPE_MULTI_POLYGON:
case self::TYPE_POINT:
case self::TYPE_POLYGON:
if (! $json->offsetExists('coordinates')) {
throw UnserializationException::missingProperty($type, 'coordinates', 'array');
}
Expand All @@ -104,7 +114,7 @@ final public static function jsonUnserialize($json): self
$args[] = $json['coordinates'];
break;

case 'Feature':
case self::TYPE_FEATURE:
$geometry = $json['geometry'] ?? null;
$properties = $json['properties'] ?? null;
$id = $json['id'] ?? null;
Expand All @@ -124,7 +134,7 @@ final public static function jsonUnserialize($json): self
$args[] = $id;
break;

case 'FeatureCollection':
case self::TYPE_FEATURE_COLLECTION:
if (! $json->offsetExists('features')) {
throw UnserializationException::missingProperty($type, 'features', 'array');
}
Expand All @@ -136,7 +146,7 @@ final public static function jsonUnserialize($json): self
$args[] = array_map([self::class, 'jsonUnserialize'], $json['features']);
break;

case 'GeometryCollection':
case self::TYPE_GEOMETRY_COLLECTION:
if (! $json->offsetExists('geometries')) {
throw UnserializationException::missingProperty($type, 'geometries', 'array');
}
Expand All @@ -160,7 +170,7 @@ final public static function jsonUnserialize($json): self
$args[] = CoordinateReferenceSystem::jsonUnserialize($json['crs']);
}

$class = sprintf('GeoJson\%s\%s', (strncmp('Feature', $type, 7) === 0 ? 'Feature' : 'Geometry'), $type);
$class = sprintf('GeoJson\%s\%s', (strncmp(self::TYPE_FEATURE, $type, 7) === 0 ? self::TYPE_FEATURE : 'Geometry'), $type);
jmikola marked this conversation as resolved.
Show resolved Hide resolved

return new $class(... $args);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/GeometryCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
class GeometryCollection extends Geometry implements Countable, IteratorAggregate
{
protected string $type = 'GeometryCollection';
protected string $type = self::TYPE_GEOMETRY_COLLECTION;

/**
* @var array<Geometry>
Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/LineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*/
class LineString extends MultiPoint
{
protected string $type = 'LineString';
protected string $type = self::TYPE_LINESTRING;
jmikola marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param array<Point|array<float|int>> $positions
Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/MultiLineString.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
class MultiLineString extends Geometry
{
protected string $type = 'MultiLineString';
protected string $type = self::TYPE_MULTI_LINE_STRING;

/**
* @param array<LineString|array<Point|array<int|float>>> $lineStrings
Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/MultiPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
class MultiPoint extends Geometry
{
protected string $type = 'MultiPoint';
protected string $type = self::TYPE_MULTI_POINT;

/**
* @param array<Point|array<float|int>> $positions
Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/MultiPolygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*/
class MultiPolygon extends Geometry
{
protected string $type = 'MultiPolygon';
protected string $type = self::TYPE_MULTI_POLYGON;

/**
* @param array<Polygon|array<LinearRing|array<Point|array<int|float>>>> $polygons
Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
class Point extends Geometry
{
protected string $type = 'Point';
protected string $type = self::TYPE_POINT;

/**
* @param array<float|int> $position
Expand Down
2 changes: 1 addition & 1 deletion src/Geometry/Polygon.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
class Polygon extends Geometry
{
protected string $type = 'Polygon';
protected string $type = self::TYPE_POLYGON;

/**
* @param array<LinearRing|array<Point|array<int|float>>> $linearRings
Expand Down
14 changes: 7 additions & 7 deletions tests/Feature/FeatureCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ public function testSerialization(): void
$collection = new FeatureCollection($features);

$expected = [
'type' => 'FeatureCollection',
'type' => GeoJson::TYPE_FEATURE_COLLECTION,
'features' => [['feature1'], ['feature2']],
];

$this->assertSame('FeatureCollection', $collection->getType());
$this->assertSame(GeoJson::TYPE_FEATURE_COLLECTION, $collection->getType());
$this->assertSame($features, $collection->getFeatures());
$this->assertSame($expected, $collection->jsonSerialize());
}
Expand Down Expand Up @@ -127,21 +127,21 @@ public function testUnserialization($assoc): void
$collection = GeoJson::jsonUnserialize($json);

$this->assertInstanceOf(FeatureCollection::class, $collection);
$this->assertSame('FeatureCollection', $collection->getType());
$this->assertSame(GeoJson::TYPE_FEATURE_COLLECTION, $collection->getType());
$this->assertCount(1, $collection);

$features = iterator_to_array($collection);
$feature = $features[0];

$this->assertInstanceOf(Feature::class, $feature);
$this->assertSame('Feature', $feature->getType());
$this->assertSame(GeoJson::TYPE_FEATURE, $feature->getType());
$this->assertSame('test.feature.1', $feature->getId());
$this->assertNull($feature->getProperties());

$geometry = $feature->getGeometry();

$this->assertInstanceOf(Point::class, $geometry);
$this->assertSame('Point', $geometry->getType());
$this->assertSame(GeoJson::TYPE_POINT, $geometry->getType());
$this->assertSame([1, 1], $geometry->getCoordinates());
}

Expand All @@ -158,14 +158,14 @@ public function testUnserializationShouldRequireFeaturesProperty(): void
$this->expectException(UnserializationException::class);
$this->expectExceptionMessage('FeatureCollection expected "features" property of type array, none given');

GeoJson::jsonUnserialize(['type' => 'FeatureCollection']);
GeoJson::jsonUnserialize(['type' => GeoJson::TYPE_FEATURE_COLLECTION]);
}

public function testUnserializationShouldRequireFeaturesArray(): void
{
$this->expectException(UnserializationException::class);
$this->expectExceptionMessage('FeatureCollection expected "features" property of type array');

GeoJson::jsonUnserialize(['type' => 'FeatureCollection', 'features' => null]);
GeoJson::jsonUnserialize(['type' => GeoJson::TYPE_FEATURE_COLLECTION, 'features' => null]);
}
}
12 changes: 6 additions & 6 deletions tests/Feature/FeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ public function testSerialization(): void
$feature = new Feature($geometry, $properties, $id);

$expected = [
'type' => 'Feature',
'type' => GeoJson::TYPE_FEATURE,
'geometry' => ['geometry'],
'properties' => $properties,
'id' => 'identifier',
];

$this->assertSame('Feature', $feature->getType());
$this->assertSame(GeoJson::TYPE_FEATURE, $feature->getType());
$this->assertSame($geometry, $feature->getGeometry());
$this->assertSame($id, $feature->getId());
$this->assertSame($properties, $feature->getProperties());
Expand All @@ -55,7 +55,7 @@ public function testSerializationWithNullConstructorArguments(): void
$feature = new Feature();

$expected = [
'type' => 'Feature',
'type' => GeoJson::TYPE_FEATURE,
'geometry' => null,
'properties' => null,
];
Expand All @@ -68,7 +68,7 @@ public function testSerializationShouldConvertEmptyPropertiesArrayToObject(): vo
$feature = new Feature(null, []);

$expected = [
'type' => 'Feature',
'type' => GeoJson::TYPE_FEATURE,
'geometry' => null,
'properties' => new stdClass(),
];
Expand Down Expand Up @@ -100,14 +100,14 @@ public function testUnserialization($assoc): void
$feature = GeoJson::jsonUnserialize($json);

$this->assertInstanceOf(Feature::class, $feature);
$this->assertSame('Feature', $feature->getType());
$this->assertSame(GeoJson::TYPE_FEATURE, $feature->getType());
$this->assertSame('test.feature.1', $feature->getId());
$this->assertSame(['key' => 'value'], $feature->getProperties());

$geometry = $feature->getGeometry();

$this->assertInstanceOf(Point::class, $geometry);
$this->assertSame('Point', $geometry->getType());
$this->assertSame(GeoJson::TYPE_POINT, $geometry->getType());
$this->assertSame([1, 1], $geometry->getCoordinates());
}

Expand Down
22 changes: 11 additions & 11 deletions tests/GeoJsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testUnserializationWithBoundingBox($assoc): void
$point = GeoJson::jsonUnserialize($json);

$this->assertInstanceOf(Point::class, $point);
$this->assertSame('Point', $point->getType());
$this->assertSame(GeoJson::TYPE_POINT, $point->getType());
$this->assertSame([1, 1], $point->getCoordinates());

$boundingBox = $point->getBoundingBox();
Expand Down Expand Up @@ -80,7 +80,7 @@ public function testUnserializationWithCrs($assoc): void
$point = GeoJson::jsonUnserialize($json);

$this->assertInstanceOf(Point::class, $point);
$this->assertSame('Point', $point->getType());
$this->assertSame(GeoJson::TYPE_POINT, $point->getType());
$this->assertSame([1, 1], $point->getCoordinates());

$crs = $point->getCrs();
Expand Down Expand Up @@ -142,7 +142,7 @@ public function testUnserializationWithInvalidCoordinates($value): void
$this->expectExceptionMessage('Point expected "coordinates" property of type array, ' . $valueType . ' given');

GeoJson::jsonUnserialize([
'type' => 'Point',
'type' => GeoJson::TYPE_POINT,
'coordinates' => $value,
]);
}
Expand All @@ -153,7 +153,7 @@ public function testFeatureUnserializationWithInvalidGeometry(): void
$this->expectExceptionMessage('Feature expected "geometry" property of type array or object, string given');

GeoJson::jsonUnserialize([
'type' => 'Feature',
'type' => GeoJson::TYPE_FEATURE,
'geometry' => 'must be array or object, but this is a string',
]);
}
Expand All @@ -164,7 +164,7 @@ public function testFeatureUnserializationWithInvalidProperties(): void
$this->expectExceptionMessage('Feature expected "properties" property of type array or object, string given');

GeoJson::jsonUnserialize([
'type' => 'Feature',
'type' => GeoJson::TYPE_FEATURE,
'properties' => 'must be array or object, but this is a string',
]);
}
Expand All @@ -180,12 +180,12 @@ public function provideJsonDecodeAssocOptions()
public function provideGeoJsonTypesWithCoordinates()
{
return [
'LineString' => ['LineString'],
'MultiLineString' => ['MultiLineString'],
'MultiPoint' => ['MultiPoint'],
'MultiPolygon' => ['MultiPolygon'],
'Point' => ['Point'],
'Polygon' => ['Polygon'],
GeoJson::TYPE_LINESTRING => [GeoJson::TYPE_LINESTRING],
jmikola marked this conversation as resolved.
Show resolved Hide resolved
GeoJson::TYPE_MULTI_LINE_STRING => [GeoJson::TYPE_MULTI_LINE_STRING],
GeoJson::TYPE_MULTI_POINT => [GeoJson::TYPE_MULTI_POINT],
GeoJson::TYPE_MULTI_POLYGON => [GeoJson::TYPE_MULTI_POLYGON],
GeoJson::TYPE_POINT => [GeoJson::TYPE_POINT],
GeoJson::TYPE_POLYGON => [GeoJson::TYPE_POLYGON],
];
}

Expand Down
12 changes: 6 additions & 6 deletions tests/Geometry/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ public function testSerialization(): void
$collection = new GeometryCollection($geometries);

$expected = [
'type' => 'GeometryCollection',
'type' => GeoJson::TYPE_GEOMETRY_COLLECTION,
'geometries' => [['geometry1'], ['geometry2']],
];

$this->assertSame('GeometryCollection', $collection->getType());
$this->assertSame(GeoJson::TYPE_GEOMETRY_COLLECTION, $collection->getType());
$this->assertSame($geometries, $collection->getGeometries());
$this->assertSame($expected, $collection->jsonSerialize());
}
Expand All @@ -121,14 +121,14 @@ public function testUnserialization($assoc): void
$collection = GeoJson::jsonUnserialize($json);

$this->assertInstanceOf(GeometryCollection::class, $collection);
$this->assertSame('GeometryCollection', $collection->getType());
$this->assertSame(GeoJson::TYPE_GEOMETRY_COLLECTION, $collection->getType());
$this->assertCount(1, $collection);

$geometries = iterator_to_array($collection);
$geometry = $geometries[0];

$this->assertInstanceOf(Point::class, $geometry);
$this->assertSame('Point', $geometry->getType());
$this->assertSame(GeoJson::TYPE_POINT, $geometry->getType());
$this->assertSame([1, 1], $geometry->getCoordinates());
}

Expand All @@ -145,14 +145,14 @@ public function testUnserializationShouldRequireGeometriesProperty(): void
$this->expectException(UnserializationException::class);
$this->expectExceptionMessage('GeometryCollection expected "geometries" property of type array, none given');

GeoJson::jsonUnserialize(['type' => 'GeometryCollection']);
GeoJson::jsonUnserialize(['type' => GeoJson::TYPE_GEOMETRY_COLLECTION]);
}

public function testUnserializationShouldRequireGeometriesArray(): void
{
$this->expectException(UnserializationException::class);
$this->expectExceptionMessage('GeometryCollection expected "geometries" property of type array');

GeoJson::jsonUnserialize(['type' => 'GeometryCollection', 'geometries' => null]);
GeoJson::jsonUnserialize(['type' => GeoJson::TYPE_GEOMETRY_COLLECTION, 'geometries' => null]);
}
}
6 changes: 3 additions & 3 deletions tests/Geometry/LineStringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public function testSerialization(): void
$lineString = new LineString($coordinates);

$expected = [
'type' => 'LineString',
'type' => GeoJson::TYPE_LINESTRING,
jmikola marked this conversation as resolved.
Show resolved Hide resolved
'coordinates' => $coordinates,
];

$this->assertSame('LineString', $lineString->getType());
$this->assertSame(GeoJson::TYPE_LINESTRING, $lineString->getType());
jmikola marked this conversation as resolved.
Show resolved Hide resolved
$this->assertSame($coordinates, $lineString->getCoordinates());
$this->assertSame($expected, $lineString->jsonSerialize());
}
Expand All @@ -73,7 +73,7 @@ public function testUnserialization($assoc): void
$expectedCoordinates = [[1, 1], [2, 2]];

$this->assertInstanceOf(LineString::class, $lineString);
$this->assertSame('LineString', $lineString->getType());
$this->assertSame(GeoJson::TYPE_LINESTRING, $lineString->getType());
jmikola marked this conversation as resolved.
Show resolved Hide resolved
$this->assertSame($expectedCoordinates, $lineString->getCoordinates());
}

Expand Down
Loading