Skip to content

Commit abfd370

Browse files
committed
Collections should require array fields during unserialization
1 parent 3fac53b commit abfd370

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/GeoJson/GeoJson.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,27 @@ final public static function jsonUnserialize($json)
130130
break;
131131

132132
case 'FeatureCollection':
133-
$features = isset($json['features']) ? $json['features'] : array();
133+
if ( ! $json->offsetExists('features')) {
134+
throw UnserializationException::missingProperty($type, 'features', 'array');
135+
}
134136

135-
if ( ! is_array($features)) {
136-
throw UnserializationException::invalidProperty($type, 'features', $features, 'array');
137+
if ( ! is_array($json['features'])) {
138+
throw UnserializationException::invalidProperty($type, 'features', $json['features'], 'array');
137139
}
138140

139-
$args[] = array_map(array('self', 'jsonUnserialize'), $features);
141+
$args[] = array_map(array('self', 'jsonUnserialize'), $json['features']);
140142
break;
141143

142144
case 'GeometryCollection':
143-
$geometries = isset($json['geometries']) ? $json['geometries'] : array();
145+
if ( ! $json->offsetExists('geometries')) {
146+
throw UnserializationException::missingProperty($type, 'geometries', 'array');
147+
}
144148

145-
if ( ! is_array($geometries)) {
146-
throw UnserializationException::invalidProperty($type, 'geometries', $geometries, 'array');
149+
if ( ! is_array($json['geometries'])) {
150+
throw UnserializationException::invalidProperty($type, 'geometries', $json['geometries'], 'array');
147151
}
148152

149-
$args[] = array_map(array('self', 'jsonUnserialize'), $geometries);
153+
$args[] = array_map(array('self', 'jsonUnserialize'), $json['geometries']);
150154
break;
151155

152156
default:

tests/GeoJson/Tests/Feature/FeatureCollectionTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,22 @@ public function provideJsonDecodeAssocOptions()
148148
'assoc=false' => array(false),
149149
);
150150
}
151+
152+
/**
153+
* @expectedException GeoJson\Exception\UnserializationException
154+
* @expectedExceptionMessage FeatureCollection expected "features" property of type array, none given
155+
*/
156+
public function testUnserializationShouldRequireFeaturesProperty()
157+
{
158+
GeoJson::jsonUnserialize(array('type' => 'FeatureCollection'));
159+
}
160+
161+
/**
162+
* @expectedException GeoJson\Exception\UnserializationException
163+
* @expectedExceptionMessage FeatureCollection expected "features" property of type array
164+
*/
165+
public function testUnserializationShouldRequireFeaturesArray()
166+
{
167+
GeoJson::jsonUnserialize(array('type' => 'FeatureCollection', 'features' => null));
168+
}
151169
}

tests/GeoJson/Tests/Geometry/GeometryCollectionTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,22 @@ public function provideJsonDecodeAssocOptions()
136136
'assoc=false' => array(false),
137137
);
138138
}
139+
140+
/**
141+
* @expectedException GeoJson\Exception\UnserializationException
142+
* @expectedExceptionMessage GeometryCollection expected "geometries" property of type array, none given
143+
*/
144+
public function testUnserializationShouldRequireGeometriesProperty()
145+
{
146+
GeoJson::jsonUnserialize(array('type' => 'GeometryCollection'));
147+
}
148+
149+
/**
150+
* @expectedException GeoJson\Exception\UnserializationException
151+
* @expectedExceptionMessage GeometryCollection expected "geometries" property of type array
152+
*/
153+
public function testUnserializationShouldRequireGeometriesArray()
154+
{
155+
GeoJson::jsonUnserialize(array('type' => 'GeometryCollection', 'geometries' => null));
156+
}
139157
}

0 commit comments

Comments
 (0)