Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Rogers committed May 14, 2018
1 parent 5357cf7 commit 6e01057
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
49 changes: 34 additions & 15 deletions src/JsonParse.php
Expand Up @@ -15,19 +15,6 @@

class JsonParse
{
protected static $exceptionClasses = [
// 'JSON_ERROR_NONE' => 'No error has occurred',
JSON_ERROR_DEPTH => MaximumStackDepthExceededException::class,
JSON_ERROR_STATE_MISMATCH => InvalidOrMalformedJsonException::class,
JSON_ERROR_CTRL_CHAR => ControlCharacterException::class,
JSON_ERROR_SYNTAX => SyntaxErrorException::class,
JSON_ERROR_UTF8 => MalformedUtf8CharactersException::class,
JSON_ERROR_RECURSION => RecursiveReferencesException::class,
JSON_ERROR_INF_OR_NAN => InfiniteNumberException::class,
JSON_ERROR_UNSUPPORTED_TYPE => UnsupportedTypeException::class,
JSON_ERROR_INVALID_PROPERTY_NAME => InvalidPropertyName::class,
JSON_ERROR_UTF16 => MalformedUtf16CharactersException::class,
];

/**
* @param $value
Expand Down Expand Up @@ -67,15 +54,47 @@ public static function decode($json, $assoc = false, $depth = 512, $options = 0)
return static::throwException(json_last_error());
}

protected static function exceptionClasses()
{
$exceptionClasses = [
JSON_ERROR_DEPTH => MaximumStackDepthExceededException::class,
JSON_ERROR_STATE_MISMATCH => InvalidOrMalformedJsonException::class,
JSON_ERROR_CTRL_CHAR => ControlCharacterException::class,
JSON_ERROR_SYNTAX => SyntaxErrorException::class,
];

if (phpversion() >= '5.3.3') {
$exceptionClasses = array_merge($exceptionClasses, [
JSON_ERROR_UTF8 => MalformedUtf8CharactersException::class,
]);
}
if (phpversion() >= '5.5.0') {
$exceptionClasses = array_merge($exceptionClasses, [
JSON_ERROR_RECURSION => RecursiveReferencesException::class,
JSON_ERROR_INF_OR_NAN => InfiniteNumberException::class,
JSON_ERROR_UNSUPPORTED_TYPE => UnsupportedTypeException::class,
]);
}

if (phpversion() >= '7.0.0') {
$exceptionClasses = array_merge($exceptionClasses, [
JSON_ERROR_INVALID_PROPERTY_NAME => InvalidPropertyName::class,
JSON_ERROR_UTF16 => MalformedUtf16CharactersException::class,
]);
}

return $exceptionClasses;
}

/**
* Throw Exception based on Error Type
*
* @param $error
*/
protected static function throwException($error)
{
if (array_key_exists($error, static::$exceptionClasses)) {
$class = static::$exceptionClasses[$error];
if (array_key_exists($error, static::exceptionClasses())) {
$class = static::exceptionClasses()[$error];
throw new $class;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/JsonParseTest.php
Expand Up @@ -60,7 +60,7 @@ public function it_can_throw_generic_error_on_decode()

/**
* @test
* @expectedException Midnite81\JsonParser\Exceptions\ControlCharacterException
* @expectedException Midnite81\JsonParser\Exceptions\SyntaxErrorException
*/
public function it_can_throw_error_on_decode()
{
Expand Down

0 comments on commit 6e01057

Please sign in to comment.