Skip to content

Commit

Permalink
Collections should require array fields during unserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikola committed Nov 26, 2013
1 parent 3fac53b commit abfd370
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/GeoJson/GeoJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions tests/GeoJson/Tests/Feature/FeatureCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
18 changes: 18 additions & 0 deletions tests/GeoJson/Tests/Geometry/GeometryCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit abfd370

Please sign in to comment.