|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebservicesNl\Utils; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class JsonUtils. |
|
7
|
|
|
* |
|
8
|
|
|
* Wrapper around JsonUtils Encode and Decode. Throws the appropriate error message when encoding/decoding fails. |
|
9
|
|
|
*/ |
|
10
|
|
|
class JsonUtils |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array JSON error messages |
|
14
|
|
|
*/ |
|
15
|
|
|
protected static $errorMessages = [ |
|
16
|
|
|
JSON_ERROR_NONE => 'No error has occurred', |
|
17
|
|
|
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', |
|
18
|
|
|
JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON', |
|
19
|
|
|
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', |
|
20
|
|
|
JSON_ERROR_SYNTAX => 'Syntax error', |
|
21
|
|
|
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', |
|
22
|
|
|
JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded', |
|
23
|
|
|
JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded', |
|
24
|
|
|
JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given', |
|
25
|
|
|
]; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Decode a JSON string into a php representation. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $json string to convert |
|
31
|
|
|
* @param bool $assoc return as associative array |
|
32
|
|
|
* @param int $options JSON options |
|
33
|
|
|
* |
|
34
|
|
|
* @throws \InvalidArgumentException |
|
35
|
|
|
* @throws \RuntimeException |
|
36
|
|
|
* |
|
37
|
|
|
* @return mixed |
|
38
|
|
|
*/ |
|
39
|
4 |
|
public static function decode($json, $assoc = false, $options = 0) |
|
40
|
|
|
{ |
|
41
|
4 |
|
if (!is_string($json)) { |
|
42
|
1 |
|
throw new \InvalidArgumentException('Argument json is not a string'); |
|
43
|
|
|
} |
|
44
|
3 |
|
$result = json_decode($json, (bool) $assoc, 512, (int) $options); |
|
45
|
3 |
|
if ($result === null) { |
|
46
|
2 |
|
throw new \RuntimeException(static::$errorMessages[json_last_error()]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
return $result; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Encode a PHP value into a JSON representation. |
|
54
|
|
|
* |
|
55
|
|
|
* @param mixed $value |
|
56
|
|
|
* @param int $options [optional] |
|
57
|
|
|
* |
|
58
|
|
|
* @throws \RuntimeException |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
* |
|
62
|
|
|
* @link http://php.net/manual/en/function.json-encode.php |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public static function encode($value, $options = 0) |
|
65
|
|
|
{ |
|
66
|
1 |
|
$result = json_encode($value, (int) $options); |
|
67
|
|
|
// @codeCoverageIgnoreStart |
|
68
|
|
|
if ($result === false) { |
|
69
|
|
|
throw new \RuntimeException(static::$errorMessages[json_last_error()]); |
|
70
|
|
|
} |
|
71
|
|
|
// @codeCoverageIgnoreEnd |
|
72
|
|
|
|
|
73
|
1 |
|
return $result; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|