Skip to content

Commit

Permalink
Merge pull request #5 from biigle/patch-1
Browse files Browse the repository at this point in the history
Make parser more robust against invalid JSON
  • Loading branch information
mzur authored Sep 4, 2024
2 parents 531ba5a + cdd25d2 commit 328b829
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
18 changes: 6 additions & 12 deletions src/Ifdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,21 @@ public function getJsonData()
public function getImageSetHeader()
{
$arr = $this->getJsonData();
if (array_key_exists('image-set-header', $arr))
{
if (is_array($arr) && array_key_exists('image-set-header', $arr)) {
return $arr['image-set-header'];
}
else
{
return [];
}

return [];
}

public function getImageSetItems()
{
$arr = $this->getJsonData();
if (array_key_exists('image-set-items', $arr))
{
if (is_array($arr) && array_key_exists('image-set-items', $arr)) {
return $arr['image-set-items'];
}
else
{
return [];
}

return [];
}

public function getIfdoVersion(): String
Expand Down
8 changes: 8 additions & 0 deletions tests/IfdoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ public function testStrictMode()
$this->expectException(\Exception::class);
Ifdo::fromString('{"some": "json"}', true);
}

public function testInvalidJson()
{
$obj = Ifdo::fromString('{"some": "json",}');
$this->assertSame(null, $obj->getJsonData());
$this->assertSame([], $obj->getImageSetHeader());
$this->assertSame([], $obj->getImageSetItems());
}
}

0 comments on commit 328b829

Please sign in to comment.