Created
December 13, 2016 09:22
-
-
Save runejuhl/59a46f8f072b14c3fb072b24a56873ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class JSONError extends Exception {} | |
class JSONErrorDepth extends JSONError {} | |
class JSONErrorMalformed extends JSONError {} | |
class JSONErrorSyntax extends JSONError {} | |
function json_check_if_error() { | |
$error = json_last_error(); | |
switch ($error) { | |
case JSON_ERROR_NONE: | |
return; | |
case JSON_ERROR_DEPTH: | |
throw new JSONErrorDepth(); | |
break; | |
case JSON_ERROR_STATE_MISMATCH: | |
throw new JSONErrorMalformed(); | |
break; | |
case JSON_ERROR_SYNTAX: | |
throw new JSONErrorSyntax(); | |
break; | |
default: | |
throw new JSONError(); | |
} | |
} | |
function from_json($string) { | |
$json = json_decode($string, true); | |
json_check_if_error(); | |
return $json; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment