|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jenky\Hermes; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayAccess; |
|
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
7
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
|
8
|
|
|
use JsonSerializable; |
|
9
|
|
|
|
|
10
|
|
|
class JsonResponse extends Response implements Parsable, ArrayAccess, Arrayable, Jsonable, JsonSerializable |
|
11
|
|
|
{ |
|
12
|
|
|
use Concerns\InteractsWithResponse; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Parse the response body to array. |
|
16
|
|
|
* |
|
17
|
|
|
* @return void |
|
18
|
|
|
*/ |
|
19
|
|
|
public function parse() |
|
20
|
|
|
{ |
|
21
|
|
|
$this->data = json_decode( |
|
22
|
|
|
$this->getBody()->__toString(), true |
|
23
|
|
|
) ?: []; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get the instance as an array. |
|
28
|
|
|
* |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
|
|
public function toArray() |
|
32
|
|
|
{ |
|
33
|
|
|
return (array) $this->data; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Convert the object into something JSON serializable. |
|
38
|
|
|
* |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function jsonSerialize() |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->toArray(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Convert the fluent instance to JSON. |
|
48
|
|
|
* |
|
49
|
|
|
* @param int $options |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public function toJson($options = 0) |
|
53
|
|
|
{ |
|
54
|
|
|
return json_encode($this->jsonSerialize(), $options); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Determine if the given offset exists. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $offset |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
|
|
public function offsetExists($offset) |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->exists($offset); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get the value for a given offset. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $offset |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
*/ |
|
74
|
|
|
public function offsetGet($offset) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->get($offset); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set the value at the given offset. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $offset |
|
83
|
|
|
* @param mixed $value |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
public function offsetSet($offset, $value) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->set($offset, $value); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Unset the value at the given offset. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $offset |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
|
|
public function offsetUnset($offset) |
|
98
|
|
|
{ |
|
99
|
|
|
unset($this->data[$offset]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Dynamically unset an attribute. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $key |
|
106
|
|
|
* @return void |
|
107
|
|
|
*/ |
|
108
|
|
|
public function __unset($key) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->offsetUnset($key); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|