Skip to content

Commit

Permalink
add strict mode with exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
chbach committed Mar 27, 2024
1 parent baba437 commit 20b6962
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 13 deletions.
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
colors="true"
displayDetailsOnTestsThatTriggerWarnings="true">
<testsuites>
<testsuite name="Biigle\IfdoParser Test Suite">
<directory>tests</directory>
Expand Down
57 changes: 46 additions & 11 deletions src/Ifdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,35 @@

class Ifdo
{
private $jsonStr = "";
private $jsonArr = [];
private $validator;
private $debug = false;

const AVAILABLE_VERSIONS = ['v2.0.0', 'v2.0.1', 'v2.1.0'];

public static function fromFile($path): Ifdo
public static function fromFile($path, $strict = false): Ifdo
{
$data = file_get_contents($path);

return Ifdo::fromString($data);
}

public static function fromString($data): Ifdo
public static function fromString($data, $strict = false): Ifdo
{
return new Ifdo($data, true);
}

public function __construct($json)
public function __construct($json, $strict = false)
{
$this->jsonStr = $json;
$this->jsonArr = json_decode($json, true);

if ($strict)
{
if ( ! $this->isValid())
{
throw new \Exception("Malformed document. See \$obj->getErrors() for more details.");
}
}
}

public function getValidator()
Expand All @@ -37,11 +43,11 @@ public function getValidator()
return $this->validator;
}

public function revalidate()
public function revalidate(): void
{
$this->validator = new \JsonSchema\Validator;
$version = $this->getIfdoVersion();
$decoded = json_decode($this->jsonStr);
$decoded = json_decode($this->toString());
$this->validator->validate($decoded, (object) ['$ref' => 'file://' . realpath("assets/ifdo-$version.json")]);

if ($this->getDebug() && ! empty($this->getErrors()))
Expand Down Expand Up @@ -76,17 +82,41 @@ public function getJsonData()

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

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

public function getIfdoVersion(): string
public function getIfdoVersion(): String
{
$dataVersion = $this->getImageSetHeader()['image-set-ifdo-version'];
if (array_key_exists('image-set-items', $this->getImageSetHeader()))
{
$dataVersion = $this->getImageSetHeader()['image-set-ifdo-version'];
}
else
{
$dataVersion = "v2.1.0";
}

if ( ! in_array($dataVersion, self::AVAILABLE_VERSIONS))
{
throw new \Exception("Unsupported iFDO version `$dataVersion`");
Expand All @@ -102,4 +132,9 @@ public function printErrors(): void
printf("[%s] %s\n", $error['property'], $error['message']);
}
}

public function toString(): String
{
return json_encode($this->getJsonData());
}
}
11 changes: 10 additions & 1 deletion tests/IfdoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ class IfdoTest extends TestCase
{
public function testReadFromFile()
{
$obj = Ifdo::fromFile(__DIR__ . '/fixtures/ifdo-test-v2.0.0.json');
$path = __DIR__ . '/fixtures/ifdo-test-v2.0.0.json';
$obj = Ifdo::fromFile($path);
$obj->setDebug(true);
$this->assertEquals(true, $obj->isValid());

$this->assertEquals(json_encode(json_decode(file_get_contents($path))), $obj->toString());
}

public function testImageExample()
Expand All @@ -26,4 +29,10 @@ public function testVideoExample()
$obj->setDebug(true);
$this->assertEquals(true, $obj->isValid());
}

public function testStrictMode()
{
$this->expectException(\Exception::class);
Ifdo::fromString('{"some": "json"}', true);
}
}

0 comments on commit 20b6962

Please sign in to comment.