1
|
|
|
<?php |
2
|
|
|
namespace Loevgaard\Consignor\ShipmentServer\Response; |
3
|
|
|
|
4
|
|
|
use Loevgaard\Consignor\ShipmentServer\Exception\InvalidJsonException; |
5
|
|
|
use Loevgaard\Consignor\ShipmentServer\Request\RequestInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface as PsrResponseInterface; |
7
|
|
|
use function Loevgaard\Consignor\ShipmentServer\decodeJson; |
8
|
|
|
|
9
|
|
|
class Response implements ResponseInterface, \ArrayAccess |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var PsrResponseInterface |
13
|
|
|
*/ |
14
|
|
|
protected $response; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var RequestInterface |
18
|
|
|
*/ |
19
|
|
|
protected $request; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $data; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param PsrResponseInterface $response |
28
|
|
|
* @param RequestInterface $request |
29
|
|
|
* @throws InvalidJsonException |
30
|
|
|
*/ |
31
|
1 |
|
public function __construct(PsrResponseInterface $response, RequestInterface $request) |
32
|
|
|
{ |
33
|
1 |
|
$this->response = $response; |
34
|
1 |
|
$this->request = $request; |
35
|
1 |
|
$this->data = decodeJson((string)$this->response->getBody()); |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
public function __toString() |
39
|
|
|
{ |
40
|
|
|
return (string)$this->response->getBody(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function wasSuccessful() : bool |
44
|
|
|
{ |
45
|
|
|
return !isset($this->data['ErrorMessages']) && $this->response->getStatusCode() >= 200 && $this->response->getStatusCode() < 300; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function offsetExists($offset) |
49
|
|
|
{ |
50
|
|
|
return isset($this->data[$offset]); |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
public function offsetGet($offset) |
54
|
|
|
{ |
55
|
1 |
|
return $this->data[$offset] ?? null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function offsetSet($offset, $value) |
59
|
|
|
{ |
60
|
|
|
throw new \BadMethodCallException('The response data can not be manipulated'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function offsetUnset($offset) |
64
|
|
|
{ |
65
|
|
|
throw new \BadMethodCallException('The response data can not be manipulated'); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|