Skip to content

Commit a91fbcb

Browse files
authored
Merge branch 'master' into add-same-error-interface
2 parents f6d0846 + 845c70b commit a91fbcb

21 files changed

+79
-68
lines changed

src/Feature/Feature.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
class Feature extends GeoJson
2020
{
21-
protected string $type = 'Feature';
21+
protected string $type = self::TYPE_FEATURE;
2222

2323
protected ?Geometry $geometry;
2424

src/Feature/FeatureCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*/
2727
class FeatureCollection extends GeoJson implements Countable, IteratorAggregate
2828
{
29-
protected string $type = 'FeatureCollection';
29+
protected string $type = self::TYPE_FEATURE_COLLECTION;
3030

3131
/**
3232
* @var array<Feature>

src/GeoJson.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@
2323
*/
2424
abstract class GeoJson implements JsonSerializable, JsonUnserializable
2525
{
26+
public const TYPE_LINE_STRING = 'LineString';
27+
public const TYPE_MULTI_LINE_STRING = 'MultiLineString';
28+
public const TYPE_MULTI_POINT = 'MultiPoint';
29+
public const TYPE_MULTI_POLYGON = 'MultiPolygon';
30+
public const TYPE_POINT = 'Point';
31+
public const TYPE_POLYGON = 'Polygon';
32+
public const TYPE_FEATURE = 'Feature';
33+
public const TYPE_FEATURE_COLLECTION = 'FeatureCollection';
34+
public const TYPE_GEOMETRY_COLLECTION = 'GeometryCollection';
35+
2636
protected ?BoundingBox $boundingBox = null;
2737

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

8999
switch ($type) {
90-
case 'LineString':
91-
case 'MultiLineString':
92-
case 'MultiPoint':
93-
case 'MultiPolygon':
94-
case 'Point':
95-
case 'Polygon':
100+
case self::TYPE_LINE_STRING:
101+
case self::TYPE_MULTI_LINE_STRING:
102+
case self::TYPE_MULTI_POINT:
103+
case self::TYPE_MULTI_POLYGON:
104+
case self::TYPE_POINT:
105+
case self::TYPE_POLYGON:
96106
if (! $json->offsetExists('coordinates')) {
97107
throw UnserializationException::missingProperty($type, 'coordinates', 'array');
98108
}
@@ -104,7 +114,7 @@ final public static function jsonUnserialize($json): self
104114
$args[] = $json['coordinates'];
105115
break;
106116

107-
case 'Feature':
117+
case self::TYPE_FEATURE:
108118
$geometry = $json['geometry'] ?? null;
109119
$properties = $json['properties'] ?? null;
110120
$id = $json['id'] ?? null;
@@ -124,7 +134,7 @@ final public static function jsonUnserialize($json): self
124134
$args[] = $id;
125135
break;
126136

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

139-
case 'GeometryCollection':
149+
case self::TYPE_GEOMETRY_COLLECTION:
140150
if (! $json->offsetExists('geometries')) {
141151
throw UnserializationException::missingProperty($type, 'geometries', 'array');
142152
}

src/Geometry/GeometryCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
class GeometryCollection extends Geometry implements Countable, IteratorAggregate
2727
{
28-
protected string $type = 'GeometryCollection';
28+
protected string $type = self::TYPE_GEOMETRY_COLLECTION;
2929

3030
/**
3131
* @var array<Geometry>

src/Geometry/LineString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
class LineString extends MultiPoint
2222
{
23-
protected string $type = 'LineString';
23+
protected string $type = self::TYPE_LINE_STRING;
2424

2525
/**
2626
* @param array<Point|array<float|int>> $positions

src/Geometry/MultiLineString.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
class MultiLineString extends Geometry
2121
{
22-
protected string $type = 'MultiLineString';
22+
protected string $type = self::TYPE_MULTI_LINE_STRING;
2323

2424
/**
2525
* @param array<LineString|array<Point|array<int|float>>> $lineStrings

src/Geometry/MultiPoint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
class MultiPoint extends Geometry
2121
{
22-
protected string $type = 'MultiPoint';
22+
protected string $type = self::TYPE_MULTI_POINT;
2323

2424
/**
2525
* @param array<Point|array<float|int>> $positions

src/Geometry/MultiPolygon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020
class MultiPolygon extends Geometry
2121
{
22-
protected string $type = 'MultiPolygon';
22+
protected string $type = self::TYPE_MULTI_POLYGON;
2323

2424
/**
2525
* @param array<Polygon|array<LinearRing|array<Point|array<int|float>>>> $polygons

src/Geometry/Point.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
class Point extends Geometry
2424
{
25-
protected string $type = 'Point';
25+
protected string $type = self::TYPE_POINT;
2626

2727
/**
2828
* @param array<float|int> $position

src/Geometry/Polygon.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
class Polygon extends Geometry
1919
{
20-
protected string $type = 'Polygon';
20+
protected string $type = self::TYPE_POLYGON;
2121

2222
/**
2323
* @param array<LinearRing|array<Point|array<int|float>>> $linearRings

tests/Feature/FeatureCollectionTest.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ public function testSerialization(): void
9292
$collection = new FeatureCollection($features);
9393

9494
$expected = [
95-
'type' => 'FeatureCollection',
95+
'type' => GeoJson::TYPE_FEATURE_COLLECTION,
9696
'features' => [['feature1'], ['feature2']],
9797
];
9898

99-
$this->assertSame('FeatureCollection', $collection->getType());
99+
$this->assertSame(GeoJson::TYPE_FEATURE_COLLECTION, $collection->getType());
100100
$this->assertSame($features, $collection->getFeatures());
101101
$this->assertSame($expected, $collection->jsonSerialize());
102102
}
@@ -127,21 +127,21 @@ public function testUnserialization($assoc): void
127127
$collection = GeoJson::jsonUnserialize($json);
128128

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

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

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

141141
$geometry = $feature->getGeometry();
142142

143143
$this->assertInstanceOf(Point::class, $geometry);
144-
$this->assertSame('Point', $geometry->getType());
144+
$this->assertSame(GeoJson::TYPE_POINT, $geometry->getType());
145145
$this->assertSame([1, 1], $geometry->getCoordinates());
146146
}
147147

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

161-
GeoJson::jsonUnserialize(['type' => 'FeatureCollection']);
161+
GeoJson::jsonUnserialize(['type' => GeoJson::TYPE_FEATURE_COLLECTION]);
162162
}
163163

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

169-
GeoJson::jsonUnserialize(['type' => 'FeatureCollection', 'features' => null]);
169+
GeoJson::jsonUnserialize(['type' => GeoJson::TYPE_FEATURE_COLLECTION, 'features' => null]);
170170
}
171171
}

tests/Feature/FeatureTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ public function testSerialization(): void
3737
$feature = new Feature($geometry, $properties, $id);
3838

3939
$expected = [
40-
'type' => 'Feature',
40+
'type' => GeoJson::TYPE_FEATURE,
4141
'geometry' => ['geometry'],
4242
'properties' => $properties,
4343
'id' => 'identifier',
4444
];
4545

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

5757
$expected = [
58-
'type' => 'Feature',
58+
'type' => GeoJson::TYPE_FEATURE,
5959
'geometry' => null,
6060
'properties' => null,
6161
];
@@ -68,7 +68,7 @@ public function testSerializationShouldConvertEmptyPropertiesArrayToObject(): vo
6868
$feature = new Feature(null, []);
6969

7070
$expected = [
71-
'type' => 'Feature',
71+
'type' => GeoJson::TYPE_FEATURE,
7272
'geometry' => null,
7373
'properties' => new stdClass(),
7474
];
@@ -100,14 +100,14 @@ public function testUnserialization($assoc): void
100100
$feature = GeoJson::jsonUnserialize($json);
101101

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

107107
$geometry = $feature->getGeometry();
108108

109109
$this->assertInstanceOf(Point::class, $geometry);
110-
$this->assertSame('Point', $geometry->getType());
110+
$this->assertSame(GeoJson::TYPE_POINT, $geometry->getType());
111111
$this->assertSame([1, 1], $geometry->getCoordinates());
112112
}
113113

tests/GeoJsonTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function testUnserializationWithBoundingBox($assoc): void
4848
$point = GeoJson::jsonUnserialize($json);
4949

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

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

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

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

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

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

166166
GeoJson::jsonUnserialize([
167-
'type' => 'Feature',
167+
'type' => GeoJson::TYPE_FEATURE,
168168
'properties' => 'must be array or object, but this is a string',
169169
]);
170170
}
@@ -180,12 +180,12 @@ public function provideJsonDecodeAssocOptions()
180180
public function provideGeoJsonTypesWithCoordinates()
181181
{
182182
return [
183-
'LineString' => ['LineString'],
184-
'MultiLineString' => ['MultiLineString'],
185-
'MultiPoint' => ['MultiPoint'],
186-
'MultiPolygon' => ['MultiPolygon'],
187-
'Point' => ['Point'],
188-
'Polygon' => ['Polygon'],
183+
GeoJson::TYPE_LINE_STRING => [GeoJson::TYPE_LINE_STRING],
184+
GeoJson::TYPE_MULTI_LINE_STRING => [GeoJson::TYPE_MULTI_LINE_STRING],
185+
GeoJson::TYPE_MULTI_POINT => [GeoJson::TYPE_MULTI_POINT],
186+
GeoJson::TYPE_MULTI_POLYGON => [GeoJson::TYPE_MULTI_POLYGON],
187+
GeoJson::TYPE_POINT => [GeoJson::TYPE_POINT],
188+
GeoJson::TYPE_POLYGON => [GeoJson::TYPE_POLYGON],
189189
];
190190
}
191191

tests/Geometry/GeometryCollectionTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ public function testSerialization(): void
9090
$collection = new GeometryCollection($geometries);
9191

9292
$expected = [
93-
'type' => 'GeometryCollection',
93+
'type' => GeoJson::TYPE_GEOMETRY_COLLECTION,
9494
'geometries' => [['geometry1'], ['geometry2']],
9595
];
9696

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

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

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

130130
$this->assertInstanceOf(Point::class, $geometry);
131-
$this->assertSame('Point', $geometry->getType());
131+
$this->assertSame(GeoJson::TYPE_POINT, $geometry->getType());
132132
$this->assertSame([1, 1], $geometry->getCoordinates());
133133
}
134134

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

148-
GeoJson::jsonUnserialize(['type' => 'GeometryCollection']);
148+
GeoJson::jsonUnserialize(['type' => GeoJson::TYPE_GEOMETRY_COLLECTION]);
149149
}
150150

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

156-
GeoJson::jsonUnserialize(['type' => 'GeometryCollection', 'geometries' => null]);
156+
GeoJson::jsonUnserialize(['type' => GeoJson::TYPE_GEOMETRY_COLLECTION, 'geometries' => null]);
157157
}
158158
}

tests/Geometry/LineStringTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public function testSerialization(): void
4242
$lineString = new LineString($coordinates);
4343

4444
$expected = [
45-
'type' => 'LineString',
45+
'type' => GeoJson::TYPE_LINE_STRING,
4646
'coordinates' => $coordinates,
4747
];
4848

49-
$this->assertSame('LineString', $lineString->getType());
49+
$this->assertSame(GeoJson::TYPE_LINE_STRING, $lineString->getType());
5050
$this->assertSame($coordinates, $lineString->getCoordinates());
5151
$this->assertSame($expected, $lineString->jsonSerialize());
5252
}
@@ -73,7 +73,7 @@ public function testUnserialization($assoc): void
7373
$expectedCoordinates = [[1, 1], [2, 2]];
7474

7575
$this->assertInstanceOf(LineString::class, $lineString);
76-
$this->assertSame('LineString', $lineString->getType());
76+
$this->assertSame(GeoJson::TYPE_LINE_STRING, $lineString->getType());
7777
$this->assertSame($expectedCoordinates, $lineString->getCoordinates());
7878
}
7979

0 commit comments

Comments
 (0)