Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
Json | |
100.00% |
14 / 14 |
|
100.00% |
3 / 3 |
5 | |
100.00% |
1 / 1 |
encode | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
decode | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
_detectError | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 |
1 | <?php declare(strict_types=1); |
2 | /** |
3 | * PrivateBin |
4 | * |
5 | * a zero-knowledge paste bin |
6 | * |
7 | * @link https://github.com/PrivateBin/PrivateBin |
8 | * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net) |
9 | * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License |
10 | */ |
11 | |
12 | namespace PrivateBin; |
13 | |
14 | use Exception; |
15 | |
16 | /** |
17 | * Json |
18 | * |
19 | * Provides JSON functions in an object oriented way. |
20 | */ |
21 | class Json |
22 | { |
23 | /** |
24 | * Returns a string containing the JSON representation of the given input |
25 | * |
26 | * @access public |
27 | * @static |
28 | * @param mixed $input |
29 | * @throws Exception |
30 | * @return string |
31 | */ |
32 | public static function encode($input) |
33 | { |
34 | $jsonString = json_encode($input); |
35 | self::_detectError(); |
36 | return $jsonString; |
37 | } |
38 | |
39 | /** |
40 | * Returns an array with the contents as described in the given JSON input |
41 | * |
42 | * @access public |
43 | * @static |
44 | * @param string $input |
45 | * @throws Exception |
46 | * @return mixed |
47 | */ |
48 | public static function decode($input) |
49 | { |
50 | $output = json_decode($input, true); |
51 | self::_detectError(); |
52 | return $output; |
53 | } |
54 | |
55 | /** |
56 | * Detects JSON errors and raises an exception if one is found |
57 | * |
58 | * @access private |
59 | * @static |
60 | * @throws Exception |
61 | * @return void |
62 | */ |
63 | private static function _detectError() |
64 | { |
65 | $errorCode = json_last_error(); |
66 | if ($errorCode === JSON_ERROR_NONE) { |
67 | return; |
68 | } |
69 | |
70 | $message = 'A JSON error occurred'; |
71 | if (function_exists('json_last_error_msg')) { |
72 | $message .= ': ' . json_last_error_msg(); |
73 | } |
74 | $message .= ' (' . $errorCode . ')'; |
75 | throw new Exception($message, 90); |
76 | } |
77 | } |