Skip to content

Commit 328b829

Browse files
authored
Merge pull request #5 from biigle/patch-1
Make parser more robust against invalid JSON
2 parents 531ba5a + cdd25d2 commit 328b829

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

src/Ifdo.php

+6-12
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,21 @@ public function getJsonData()
8484
public function getImageSetHeader()
8585
{
8686
$arr = $this->getJsonData();
87-
if (array_key_exists('image-set-header', $arr))
88-
{
87+
if (is_array($arr) && array_key_exists('image-set-header', $arr)) {
8988
return $arr['image-set-header'];
9089
}
91-
else
92-
{
93-
return [];
94-
}
90+
91+
return [];
9592
}
9693

9794
public function getImageSetItems()
9895
{
9996
$arr = $this->getJsonData();
100-
if (array_key_exists('image-set-items', $arr))
101-
{
97+
if (is_array($arr) && array_key_exists('image-set-items', $arr)) {
10298
return $arr['image-set-items'];
10399
}
104-
else
105-
{
106-
return [];
107-
}
100+
101+
return [];
108102
}
109103

110104
public function getIfdoVersion(): String

tests/IfdoTest.php

+8
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,12 @@ public function testStrictMode()
3535
$this->expectException(\Exception::class);
3636
Ifdo::fromString('{"some": "json"}', true);
3737
}
38+
39+
public function testInvalidJson()
40+
{
41+
$obj = Ifdo::fromString('{"some": "json",}');
42+
$this->assertSame(null, $obj->getJsonData());
43+
$this->assertSame([], $obj->getImageSetHeader());
44+
$this->assertSame([], $obj->getImageSetItems());
45+
}
3846
}

0 commit comments

Comments
 (0)