diff --git a/src/GeoJson/GeoJson.php b/src/GeoJson/GeoJson.php index ba4997b..91a4768 100644 --- a/src/GeoJson/GeoJson.php +++ b/src/GeoJson/GeoJson.php @@ -130,23 +130,27 @@ final public static function jsonUnserialize($json) break; case 'FeatureCollection': - $features = isset($json['features']) ? $json['features'] : array(); + if ( ! $json->offsetExists('features')) { + throw UnserializationException::missingProperty($type, 'features', 'array'); + } - if ( ! is_array($features)) { - throw UnserializationException::invalidProperty($type, 'features', $features, 'array'); + if ( ! is_array($json['features'])) { + throw UnserializationException::invalidProperty($type, 'features', $json['features'], 'array'); } - $args[] = array_map(array('self', 'jsonUnserialize'), $features); + $args[] = array_map(array('self', 'jsonUnserialize'), $json['features']); break; case 'GeometryCollection': - $geometries = isset($json['geometries']) ? $json['geometries'] : array(); + if ( ! $json->offsetExists('geometries')) { + throw UnserializationException::missingProperty($type, 'geometries', 'array'); + } - if ( ! is_array($geometries)) { - throw UnserializationException::invalidProperty($type, 'geometries', $geometries, 'array'); + if ( ! is_array($json['geometries'])) { + throw UnserializationException::invalidProperty($type, 'geometries', $json['geometries'], 'array'); } - $args[] = array_map(array('self', 'jsonUnserialize'), $geometries); + $args[] = array_map(array('self', 'jsonUnserialize'), $json['geometries']); break; default: diff --git a/tests/GeoJson/Tests/Feature/FeatureCollectionTest.php b/tests/GeoJson/Tests/Feature/FeatureCollectionTest.php index db655f7..2531301 100644 --- a/tests/GeoJson/Tests/Feature/FeatureCollectionTest.php +++ b/tests/GeoJson/Tests/Feature/FeatureCollectionTest.php @@ -148,4 +148,22 @@ public function provideJsonDecodeAssocOptions() 'assoc=false' => array(false), ); } + + /** + * @expectedException GeoJson\Exception\UnserializationException + * @expectedExceptionMessage FeatureCollection expected "features" property of type array, none given + */ + public function testUnserializationShouldRequireFeaturesProperty() + { + GeoJson::jsonUnserialize(array('type' => 'FeatureCollection')); + } + + /** + * @expectedException GeoJson\Exception\UnserializationException + * @expectedExceptionMessage FeatureCollection expected "features" property of type array + */ + public function testUnserializationShouldRequireFeaturesArray() + { + GeoJson::jsonUnserialize(array('type' => 'FeatureCollection', 'features' => null)); + } } diff --git a/tests/GeoJson/Tests/Geometry/GeometryCollectionTest.php b/tests/GeoJson/Tests/Geometry/GeometryCollectionTest.php index 7701eeb..701e4f1 100644 --- a/tests/GeoJson/Tests/Geometry/GeometryCollectionTest.php +++ b/tests/GeoJson/Tests/Geometry/GeometryCollectionTest.php @@ -136,4 +136,22 @@ public function provideJsonDecodeAssocOptions() 'assoc=false' => array(false), ); } + + /** + * @expectedException GeoJson\Exception\UnserializationException + * @expectedExceptionMessage GeometryCollection expected "geometries" property of type array, none given + */ + public function testUnserializationShouldRequireGeometriesProperty() + { + GeoJson::jsonUnserialize(array('type' => 'GeometryCollection')); + } + + /** + * @expectedException GeoJson\Exception\UnserializationException + * @expectedExceptionMessage GeometryCollection expected "geometries" property of type array + */ + public function testUnserializationShouldRequireGeometriesArray() + { + GeoJson::jsonUnserialize(array('type' => 'GeometryCollection', 'geometries' => null)); + } }