-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJson.php
More file actions
155 lines (127 loc) · 4.79 KB
/
Json.php
File metadata and controls
155 lines (127 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
declare(strict_types=1);
namespace Tarampampam\Wrappers;
use Tarampampam\Wrappers\Exceptions\JsonEncodeDecodeException;
/**
* JSON encode/decode wrapper (methods throws exceptions on errors).
*/
class Json
{
/**
* Returns the JSON representation of a value.
*
* @link http://php.net/manual/en/function.json-encode.php
*
* @param mixed $value The value being encoded. Can be any type except a resource
* @param int $options Options bitmask
* @param int $depth Set the maximum depth. Must be greater than zero
*
* @throws JsonEncodeDecodeException
*
* @return string
*/
public static function encode($value, int $options = 0, int $depth = 512): string
{
if ($options & JSON_PRETTY_PRINT_2_SPACES) {
$options |= JSON_PRETTY_PRINT;
}
if ($options & JSON_EMPTY_ARRAYS_TO_OBJECTS) {
$value = static::emptyArraysToObjects($value);
}
$json = \json_encode($value, $options, $depth);
// `is_string()` is required because `json_encode()` can returns `false`
if (($error_code = \json_last_error()) !== JSON_ERROR_NONE || ! \is_string($json)) {
throw static::jsonErrorToException($error_code);
}
if ($options & JSON_PRETTY_PRINT_2_SPACES) {
$json = (string) \preg_replace_callback('/^ +/m', static function ($m) {
return \str_repeat(' ', \mb_strlen($m[0]) / 2);
}, $json);
}
return $json;
}
/**
* Decodes a JSON string.
*
* This method only works with UTF-8 encoded strings.
*
* By default returns data as associative array.
*
* @link http://php.net/manual/en/function.json-decode.php
*
* @param string $json_string The json string being decoded
* @param bool $assoc When TRUE, returned objects will be converted into associative arrays
* @param int $depth User specified recursion depth
* @param int $options Bitmask of JSON decode options
*
* @throws JsonEncodeDecodeException
*
* @return array<mixed>|object|bool|null|string|float|int
*/
public static function decode(string $json_string, bool $assoc = true, int $depth = 512, int $options = 0)
{
$data = \json_decode($json_string, $assoc, $depth, $options);
if (($error_code = \json_last_error()) !== JSON_ERROR_NONE) {
throw static::jsonErrorToException($error_code);
}
return $data;
}
/**
* Converts JSON decode error into exception.
*
* @param int|null $json_error_code
*
* @return JsonEncodeDecodeException
*/
public static function jsonErrorToException(?int $json_error_code): JsonEncodeDecodeException
{
switch ($json_error_code) {
case JSON_ERROR_DEPTH:
return JsonEncodeDecodeException::depth();
case JSON_ERROR_STATE_MISMATCH:
return JsonEncodeDecodeException::stateMismatch();
case JSON_ERROR_CTRL_CHAR:
return JsonEncodeDecodeException::ctrlChar();
case JSON_ERROR_SYNTAX:
return JsonEncodeDecodeException::syntax();
case JSON_ERROR_UTF8:
return JsonEncodeDecodeException::utf8();
case JSON_ERROR_RECURSION:
return JsonEncodeDecodeException::recursion();
case JSON_ERROR_INF_OR_NAN:
return JsonEncodeDecodeException::infOrNan();
case JSON_ERROR_UNSUPPORTED_TYPE:
return JsonEncodeDecodeException::unsupportedType();
case 9: // JSON_ERROR_INVALID_PROPERTY_NAME: // Available since PHP 7.0.0
return JsonEncodeDecodeException::invalidPropertyName();
case 10: // JSON_ERROR_UTF16: // Available since PHP 7.0.0
return JsonEncodeDecodeException::utf16();
}
return JsonEncodeDecodeException::unknown();
}
/**
* @param mixed $value
*
* @return mixed
*/
protected static function emptyArraysToObjects($value)
{
if (\is_array($value)) {
return \count($value) === 0
? (object) $value
: \array_map(static function ($v) {
return self::emptyArraysToObjects($v);
}, $value); // recursive
}
if (\is_object($value)) {
$new_object = new \stdClass;
foreach (\get_object_vars($value) as $object_key => $object_value) {
$new_object->{$object_key} = \is_array($object_value) || \is_object($object_value)
? static::emptyArraysToObjects($object_value)
: $object_value;
}
return $new_object;
}
return $value;
}
}