|
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 getErrors() : array |
|
49
|
|
|
{ |
|
50
|
|
|
if(!isset($this->data['ErrorMessages'])) { |
|
51
|
|
|
return []; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $this->data['ErrorMessages']; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function offsetExists($offset) |
|
58
|
|
|
{ |
|
59
|
|
|
return isset($this->data[$offset]); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
public function offsetGet($offset) |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->data[$offset] ?? null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function offsetSet($offset, $value) |
|
68
|
|
|
{ |
|
69
|
|
|
throw new \BadMethodCallException('The response data can not be manipulated'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function offsetUnset($offset) |
|
73
|
|
|
{ |
|
74
|
|
|
throw new \BadMethodCallException('The response data can not be manipulated'); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|