Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Throw JsonException on invalid JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Programie committed Feb 4, 2015
1 parent 0b31ae0 commit 053ef16
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/main/php/com/selfcoders/jsonconfig/Config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace com\selfcoders\jsonconfig;

use com\selfcoders\jsonconfig\exception\JsonException;
use com\selfcoders\jsonconfig\exception\UnknownConfigValueException;
use com\selfcoders\jsonconfig\exception\UnsetConfigValueException;

Expand Down Expand Up @@ -37,6 +38,8 @@ public function __construct($configFile, $templateFile)
* Load or reload the configuration from the specified configuration file and template
*
* @param null|string $configFile An optional path to a JSON file which should be loaded instead of the $configFile defined on instance creation
*
* @throws JsonException If the configuration file could not be parsed
*/
public function load($configFile = null)
{
Expand All @@ -50,12 +53,14 @@ public function load($configFile = null)
if (file_exists($configFile))
{
$configData = json_decode(file_get_contents($configFile));
if ($configData)
if (!$configData)
{
throw new JsonException();
}

foreach ($this->configData as $name => $itemData)
{
foreach ($this->configData as $name => $itemData)
{
$itemData->value = ValueByPath::getValueByPath($configData, $name);
}
$itemData->value = ValueByPath::getValueByPath($configData, $name);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace com\selfcoders\jsonconfig\exception;

class JsonException extends \ Exception
{
}
7 changes: 7 additions & 0 deletions src/test/php/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,11 @@ public function testSaveUnset()

unlink($filename);
}

public function testLoadInvalidJson()
{
$this->setExpectedException("com\\selfcoders\\jsonconfig\\exception\\JsonException");

new Config(__DIR__ . "/../resources/invalid.json", __DIR__ . "/../resources/config.template.json");
}
}
3 changes: 3 additions & 0 deletions src/test/resources/invalid.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
error
}

0 comments on commit 053ef16

Please sign in to comment.