Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
14 / 14 |
Json | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
14 / 14 |
encode | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
decode | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
_detectError | |
100.00% |
1 / 1 |
3 | |
100.00% |
8 / 8 |
<?php | |
/** | |
* PrivateBin | |
* | |
* a zero-knowledge paste bin | |
* | |
* @link https://github.com/PrivateBin/PrivateBin | |
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net) | |
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License | |
* @version 1.4.0 | |
*/ | |
namespace PrivateBin; | |
use Exception; | |
/** | |
* Json | |
* | |
* Provides JSON functions in an object oriented way. | |
*/ | |
class Json | |
{ | |
/** | |
* Returns a string containing the JSON representation of the given input | |
* | |
* @access public | |
* @static | |
* @param mixed $input | |
* @throws Exception | |
* @return string | |
*/ | |
public static function encode($input) | |
{ | |
$jsonString = json_encode($input); | |
self::_detectError(); | |
return $jsonString; | |
} | |
/** | |
* Returns an array with the contents as described in the given JSON input | |
* | |
* @access public | |
* @static | |
* @param string $input | |
* @throws Exception | |
* @return mixed | |
*/ | |
public static function decode($input) | |
{ | |
$output = json_decode($input, true); | |
self::_detectError(); | |
return $output; | |
} | |
/** | |
* Detects JSON errors and raises an exception if one is found | |
* | |
* @access private | |
* @static | |
* @throws Exception | |
* @return void | |
*/ | |
private static function _detectError() | |
{ | |
$errorCode = json_last_error(); | |
if ($errorCode === JSON_ERROR_NONE) { | |
return; | |
} | |
$message = 'A JSON error occurred'; | |
if (function_exists('json_last_error_msg')) { | |
$message .= ': ' . json_last_error_msg(); | |
} | |
$message .= ' (' . $errorCode . ')'; | |
throw new Exception($message, 90); | |
} | |
} |