Skip to content

Instantly share code, notes, and snippets.

@motin
Created December 19, 2017 14:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motin/dc3762a64c7daadaa724d674b3e1d5f3 to your computer and use it in GitHub Desktop.
Save motin/dc3762a64c7daadaa724d674b3e1d5f3 to your computer and use it in GitHub Desktop.
Handle JSON decoding/encoding app-wide in a specific manner
<?php
/**
* Handle JSON decoding/encoding app-wide in a specific manner
*
* Class AppJson
*/
class AppJson
{
static $jsonErrors = [
JSON_ERROR_NONE => 'JSON_ERROR_NONE - No error has occurred',
JSON_ERROR_DEPTH => 'JSON_ERROR_DEPTH - The maximum stack depth has been exceeded',
JSON_ERROR_STATE_MISMATCH => 'JSON_ERROR_STATE_MISMATCH - Invalid or malformed JSON',
JSON_ERROR_CTRL_CHAR => 'JSON_ERROR_CTRL_CHAR - Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'JSON_ERROR_SYNTAX - Syntax error',
JSON_ERROR_UTF8 => 'JSON_ERROR_UTF8 - Malformed UTF-8 characters, possibly incorrectly encoded',
JSON_ERROR_RECURSION => 'JSON_ERROR_RECURSION - One or more recursive references in the value to be encoded',
JSON_ERROR_INF_OR_NAN => 'JSON_ERROR_INF_OR_NAN - One or more NAN or INF values in the value to be encoded',
JSON_ERROR_UNSUPPORTED_TYPE => 'JSON_ERROR_UNSUPPORTED_TYPE - A value of a type that cannot be encoded was given',
JSON_ERROR_INVALID_PROPERTY_NAME => 'JSON_ERROR_INVALID_PROPERTY_NAME - A property name that cannot be encoded was given',
JSON_ERROR_UTF16 => 'JSON_ERROR_UTF16 - Malformed UTF-16 characters, possibly incorrectly encoded',
];
static public function decode($presumedValidJsonContents, $assoc = false, $depth = 512, $options = 0)
{
if ($presumedValidJsonContents === '') {
throw new AppJsonDecodeException("The presumed to be valid JSON contents is just an empty string");
}
if ($presumedValidJsonContents === null) {
throw new AppJsonDecodeException("The presumed to be valid JSON contents is null");
}
$data = \json_decode($presumedValidJsonContents, $assoc, $depth, $options);
if (JSON_ERROR_NONE !== json_last_error()) {
if (function_exists('codecept_debug')) {
codecept_debug('$presumedValidJsonContents:');
codecept_debug($presumedValidJsonContents);
}
$last = json_last_error();
throw new AppJsonDecodeException(
'Unable to parse JSON data: '
. (isset(static::$jsonErrors[$last])
? static::$jsonErrors[$last]
: 'Unknown error')
);
}
return $data;
}
static public function encode($object, $options = 0, $depth = 512)
{
$json = json_encode($object, $options, $depth);
if (JSON_ERROR_NONE !== json_last_error()) {
$last = json_last_error();
if (function_exists('codecept_debug')) {
codecept_debug('JSON encoding failed for: ' . var_export($object, true));
}
throw new AppJsonEncodeException(
'Unable to encode object to JSON: '
. (isset(static::$jsonErrors[$last])
? static::$jsonErrors[$last]
: 'Unknown error')
);
}
if ($json === false) {
throw new AppJsonDecodeException("The JSON encoding resulting in a false return value");
}
return $json;
}
}
class AppJsonDecodeException extends Exception
{
}
class AppJsonEncodeException extends Exception
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment