1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BuzzinaSocial\Traits; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use BuzzinaSocial\Traits\Exceptions\JsonException; |
8
|
|
|
|
9
|
|
|
use function json_decode; |
10
|
|
|
use function json_last_error; |
11
|
|
|
use function json_last_error_msg; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait JsonRender. |
15
|
|
|
* |
16
|
|
|
* @package BuzzinaSocial\Traits |
17
|
|
|
*/ |
18
|
|
|
trait JsonRender |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Wrapper for json_decode that throws when an error occurs. |
22
|
|
|
* |
23
|
|
|
* @param string $json JSON data to parse |
24
|
|
|
* @param bool $assoc When true, returned objects will be converted into associative arrays. |
25
|
|
|
* @param int $depth User specified recursion depth. |
26
|
|
|
* @param int $options Bitmask of JSON decode options. |
27
|
|
|
* |
28
|
|
|
* @return mixed |
29
|
|
|
* @throws InvalidArgumentException if the JSON cannot be decoded. |
30
|
|
|
* |
31
|
|
|
* @link http://www.php.net/manual/en/function.json-decode.php |
32
|
|
|
*/ |
33
|
|
|
function jsonDecode(string $json, bool $assoc = false, int $depth = 512, int $options = 0) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$data = json_decode($json, $assoc, $depth, $options); |
36
|
|
|
|
37
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
38
|
|
|
throw new JsonException('json_decode error: ' . json_last_error_msg()); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $data; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Wrapper for JSON encoding that throws when an error occurs. |
47
|
|
|
* |
48
|
|
|
* @param mixed $value The value being encoded |
49
|
|
|
* @param int $options JSON encode option bitmask |
50
|
|
|
* @param int $depth Set the maximum depth. Must be greater than zero. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
* @throws InvalidArgumentException if the JSON cannot be encoded. |
54
|
|
|
* @link http://www.php.net/manual/en/function.json-encode.php |
55
|
|
|
*/ |
56
|
|
|
function jsonEncode($value, int $options = 0, int $depth = 512): string |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$json = json_encode($value, $options, $depth); |
59
|
|
|
|
60
|
|
|
if (JSON_ERROR_NONE !== json_last_error()) { |
61
|
|
|
throw new JsonException('json_encode error: ' . json_last_error_msg()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $json; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Convert to object. |
69
|
|
|
* |
70
|
|
|
* @param mixed $string |
71
|
|
|
* |
72
|
|
|
* @return stdClass |
73
|
|
|
*/ |
74
|
|
|
public function toObject($string): stdClass |
75
|
|
|
{ |
76
|
|
|
if (is_string($string)) { |
77
|
|
|
return $this->jsonDecode($string); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->jsonDecode($this->jsonEncode($string)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Convert to array. |
85
|
|
|
* |
86
|
|
|
* @param mixed $paramns |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function toArray($paramns): array |
91
|
|
|
{ |
92
|
|
|
if (is_string($paramns)) { |
93
|
|
|
return $this->jsonDecode($paramns, true); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->jsonDecode($this->jsonEncode($paramns), true); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.