|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TraderInteractive\Api; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
7
|
|
|
use UnexpectedValueException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Immutable representation of a response to an API request. |
|
11
|
|
|
*/ |
|
12
|
|
|
final class Response |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var integer |
|
16
|
|
|
*/ |
|
17
|
|
|
private $httpCode; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $headers; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
private $body; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Create a new instance of Response |
|
31
|
|
|
* |
|
32
|
|
|
* @param int $httpCode The http status of the response. |
|
33
|
|
|
* @param array $headers An array where each header key has an array of values |
|
34
|
|
|
* @param array $body The response from the API |
|
35
|
|
|
* |
|
36
|
|
|
* @throws InvalidArgumentException Throw if $httpCode is not an integer between 100 and 600 |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(int $httpCode = 300, array $headers = [], array $body = []) |
|
39
|
|
|
{ |
|
40
|
|
|
if ($httpCode < 100 || $httpCode > 600) { |
|
41
|
|
|
throw new InvalidArgumentException('$httpCode must be an integer >= 100 and <= 600'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$this->httpCode = $httpCode; |
|
45
|
|
|
$this->headers = $headers; |
|
46
|
|
|
$this->body = $body; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns the HTTP status code of the response. |
|
51
|
|
|
* |
|
52
|
|
|
* @return integer |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getHttpCode() : int |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->httpCode; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns an array representing the response from the API. |
|
61
|
|
|
* |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getResponse() : array |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->body; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns the parsed response headers from the API. |
|
71
|
|
|
* |
|
72
|
|
|
* @return array Array where each header key has an array of values. |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getResponseHeaders() : array |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->headers; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public static function fromPsr7Response(ResponseInterface $response) : Response |
|
80
|
|
|
{ |
|
81
|
|
|
return new self( |
|
82
|
|
|
$response->getStatusCode(), |
|
83
|
|
|
$response->getHeaders(), |
|
84
|
|
|
self::decodeBody($response->getBody()) |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private static function decodeBody(string $json) : array |
|
89
|
|
|
{ |
|
90
|
|
|
if (trim($json) == '') { |
|
91
|
|
|
return []; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
try { |
|
95
|
|
|
return json_decode($json, true); |
|
96
|
|
|
} finally { |
|
97
|
|
|
self::ensureJson(); |
|
98
|
|
|
} |
|
99
|
|
|
}//@codeCoverageIgnore |
|
100
|
|
|
|
|
101
|
|
|
private static function ensureJson() |
|
102
|
|
|
{ |
|
103
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
|
104
|
|
|
throw new UnexpectedValueException( |
|
105
|
|
|
sprintf( |
|
106
|
|
|
'Could not parse response body. Error: %s', |
|
107
|
|
|
json_last_error_msg() |
|
108
|
|
|
) |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|