1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @project Promopult Integra client library |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Promopult\Integra; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Response |
10
|
|
|
* |
11
|
|
|
* @author Dmitry Gladyshev <[email protected]> |
12
|
|
|
* @since 1.0 |
13
|
|
|
*/ |
14
|
|
|
final class Response implements \Promopult\Integra\ResponseInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $version; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
private $hasError; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
private $statusCode; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $statusMessage; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $notices; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var mixed |
43
|
|
|
*/ |
44
|
|
|
private $data; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param \Psr\Http\Message\ResponseInterface $response |
48
|
|
|
* @return static |
49
|
|
|
*/ |
50
|
|
|
public static function fromHttpResponse(\Psr\Http\Message\ResponseInterface $response): self |
51
|
|
|
{ |
52
|
|
|
$bodyData = json_decode($response->getBody()->getContents(), true); |
53
|
|
|
|
54
|
|
|
return new static( |
55
|
|
|
(string)$bodyData['version'], |
56
|
|
|
(bool)$bodyData['error'], |
57
|
|
|
(int)$bodyData['status']['code'], |
58
|
|
|
(string)$bodyData['status']['message'], |
59
|
|
|
(array)$bodyData['notices'], |
60
|
|
|
$bodyData['data'] ?? null |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function __construct( |
65
|
|
|
string $version, |
66
|
|
|
bool $hasError, |
67
|
|
|
int $statusCode, |
68
|
|
|
string $statusMessage, |
69
|
|
|
array $notices, |
70
|
|
|
$data |
71
|
|
|
) { |
72
|
|
|
$this->version = $version; |
73
|
|
|
$this->hasError = $hasError; |
74
|
|
|
$this->statusCode = $statusCode; |
75
|
|
|
$this->statusMessage = $statusMessage; |
76
|
|
|
$this->notices = $notices; |
77
|
|
|
$this->data = $data; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return false|string |
82
|
|
|
*/ |
83
|
|
|
public function __toString() |
84
|
|
|
{ |
85
|
|
|
return json_encode([ |
86
|
|
|
'version' => $this->getVersion(), |
87
|
|
|
'notices' => $this->getNotices(), |
88
|
|
|
'status' => [ |
89
|
|
|
'code' => $this->getStatusCode(), |
90
|
|
|
'message' => $this->getStatusMessage() |
91
|
|
|
], |
92
|
|
|
'error' => $this->hasError(), |
93
|
|
|
'data' => $this->getData(), |
94
|
|
|
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritDoc} |
99
|
|
|
*/ |
100
|
|
|
public function getVersion(): string |
101
|
|
|
{ |
102
|
|
|
return $this->version; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritDoc} |
107
|
|
|
*/ |
108
|
|
|
public function hasError(): bool |
109
|
|
|
{ |
110
|
|
|
return $this->hasError; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritDoc} |
115
|
|
|
*/ |
116
|
|
|
public function getStatusMessage(): string |
117
|
|
|
{ |
118
|
|
|
return $this->statusMessage; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritDoc} |
123
|
|
|
*/ |
124
|
|
|
public function getStatusCode(): int |
125
|
|
|
{ |
126
|
|
|
return $this->statusCode; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritDoc} |
131
|
|
|
*/ |
132
|
|
|
public function getNotices(): array |
133
|
|
|
{ |
134
|
|
|
return $this->notices; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritDoc} |
139
|
|
|
*/ |
140
|
|
|
public function getData() |
141
|
|
|
{ |
142
|
|
|
return $this->data; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|