1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CloudyCity\TencentMarketingSDK\Kernel\Http; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use GuzzleHttp\Psr7\Response as GuzzleResponse; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Response from easywechat. |
11
|
|
|
*/ |
12
|
|
|
class Response extends GuzzleResponse |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @return string |
16
|
|
|
*/ |
17
|
|
|
public function getBodyContents() |
18
|
|
|
{ |
19
|
|
|
$this->getBody()->rewind(); |
20
|
|
|
$contents = $this->getBody()->getContents(); |
21
|
|
|
$this->getBody()->rewind(); |
22
|
|
|
|
23
|
|
|
return $contents; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
28
|
|
|
* |
29
|
|
|
* @return Response |
30
|
|
|
*/ |
31
|
|
|
public static function buildFromPsrResponse(ResponseInterface $response) |
32
|
|
|
{ |
33
|
|
|
return new static( |
34
|
|
|
$response->getStatusCode(), |
35
|
|
|
$response->getHeaders(), |
36
|
|
|
$response->getBody(), |
37
|
|
|
$response->getProtocolVersion(), |
38
|
|
|
$response->getReasonPhrase() |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Build to json. |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function toJson() |
48
|
|
|
{ |
49
|
|
|
return json_encode($this->toArray()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Build to array. |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function toArray() |
58
|
|
|
{ |
59
|
|
|
$content = $this->getBodyContents(); |
60
|
|
|
|
61
|
|
|
$array = json_decode($content, true, 512, JSON_BIGINT_AS_STRING); |
62
|
|
|
|
63
|
|
|
if (JSON_ERROR_NONE === json_last_error()) { |
64
|
|
|
return (array) $array; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return []; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get collection data. |
72
|
|
|
* |
73
|
|
|
* @return ArrayCollection |
74
|
|
|
*/ |
75
|
|
|
public function toCollection() |
76
|
|
|
{ |
77
|
|
|
return new ArrayCollection($this->toArray()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return object |
82
|
|
|
*/ |
83
|
|
|
public function toObject() |
84
|
|
|
{ |
85
|
|
|
return json_decode($this->toJson()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return bool|string |
90
|
|
|
*/ |
91
|
|
|
public function __toString() |
92
|
|
|
{ |
93
|
|
|
return $this->getBodyContents(); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|