1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EasyIM\Kernel\Http; |
4
|
|
|
|
5
|
|
|
use EasyIM\Kernel\Support\Collection; |
6
|
|
|
use EasyIM\Kernel\Support\XML; |
7
|
|
|
use GuzzleHttp\Psr7\Response as GuzzleResponse; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Response. |
12
|
|
|
*/ |
13
|
|
|
class Response extends GuzzleResponse |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
6 |
|
public function getBodyContents() |
19
|
|
|
{ |
20
|
6 |
|
$this->getBody()->rewind(); |
21
|
6 |
|
$contents = $this->getBody()->getContents(); |
22
|
6 |
|
$this->getBody()->rewind(); |
23
|
|
|
|
24
|
6 |
|
return $contents; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
29
|
|
|
* |
30
|
|
|
* @return \EasyIM\Kernel\Http\Response |
31
|
|
|
*/ |
32
|
4 |
|
public static function buildFromPsrResponse(ResponseInterface $response) |
33
|
|
|
{ |
34
|
4 |
|
return new static( |
35
|
4 |
|
$response->getStatusCode(), |
36
|
4 |
|
$response->getHeaders(), |
37
|
4 |
|
$response->getBody(), |
38
|
4 |
|
$response->getProtocolVersion(), |
39
|
4 |
|
$response->getReasonPhrase() |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Build to json. |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
3 |
|
public function toJson() |
49
|
|
|
{ |
50
|
3 |
|
return json_encode($this->toArray()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Build to array. |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
6 |
|
public function toArray() |
59
|
|
|
{ |
60
|
6 |
|
$content = $this->removeControlCharacters($this->getBodyContents()); |
61
|
|
|
|
62
|
6 |
|
if (false !== stripos($this->getHeaderLine('Content-Type'), 'xml') || 0 === stripos($content, '<xml')) { |
63
|
1 |
|
return XML::parse($content); |
64
|
|
|
} |
65
|
|
|
|
66
|
5 |
|
$array = json_decode($content, true, 512, JSON_BIGINT_AS_STRING); |
67
|
|
|
|
68
|
5 |
|
if (JSON_ERROR_NONE === json_last_error()) { |
69
|
5 |
|
return (array) $array; |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
return []; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get collection data. |
77
|
|
|
* |
78
|
|
|
* @return \EasyIM\Kernel\Support\Collection |
79
|
|
|
*/ |
80
|
3 |
|
public function toCollection() |
81
|
|
|
{ |
82
|
3 |
|
return new Collection($this->toArray()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return object |
87
|
|
|
*/ |
88
|
3 |
|
public function toObject() |
89
|
|
|
{ |
90
|
3 |
|
return json_decode($this->toJson()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return bool|string |
95
|
|
|
*/ |
96
|
1 |
|
public function __toString() |
97
|
|
|
{ |
98
|
1 |
|
return $this->getBodyContents(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $content |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
6 |
|
protected function removeControlCharacters(string $content) |
107
|
|
|
{ |
108
|
6 |
|
return \preg_replace('/[\x00-\x1F\x80-\x9F]/u', '', $content); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|