|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace CdekSDK2\Http; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ApiResponse |
|
11
|
|
|
* @package CdekSDK2\Http |
|
12
|
|
|
*/ |
|
13
|
|
|
class ApiResponse |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Тело ответа |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $body; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Массив ошибок |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $errors = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* HTTP-статус ответа |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
private $status; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* ApiResponse constructor. |
|
35
|
|
|
* @param ResponseInterface|null $response |
|
36
|
|
|
*/ |
|
37
|
9 |
|
public function __construct(ResponseInterface $response = null) |
|
38
|
|
|
{ |
|
39
|
9 |
|
if ($response === null) { |
|
40
|
4 |
|
$this->status = 500; |
|
41
|
4 |
|
$this->body = ''; |
|
42
|
|
|
|
|
43
|
4 |
|
$this->errors[] = [ |
|
44
|
4 |
|
'code' => 'internal_error', |
|
45
|
4 |
|
'message' => 'Internal Server Error' |
|
46
|
4 |
|
]; |
|
47
|
|
|
} else { |
|
48
|
5 |
|
$this->status = $response->getStatusCode(); |
|
49
|
|
|
if ( |
|
50
|
5 |
|
$response->hasHeader('Content-type') && |
|
51
|
5 |
|
strpos(implode(',', $response->getHeader('Content-type')), 'json') !== false |
|
52
|
|
|
) { |
|
53
|
|
|
$this->body = $response->getBody()->getContents(); |
|
54
|
|
|
} else { |
|
55
|
5 |
|
$this->body = (string)$response->getBody(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
5 |
|
$decode_body = json_decode($this->body, true); |
|
60
|
5 |
|
$this->addErrorsFromArrayIfExists($decode_body); |
|
61
|
5 |
|
if (isset($decode_body['requests']) && is_array($decode_body['requests'])) { |
|
62
|
|
|
foreach ($decode_body['requests'] as $request) { |
|
63
|
|
|
$this->addErrorsFromArrayIfExists($request); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
5 |
|
private function addErrorsFromArrayIfExists(?array $array): void |
|
70
|
|
|
{ |
|
71
|
5 |
|
if (isset($array['error'])) { |
|
72
|
2 |
|
$this->errors[] = [ |
|
73
|
2 |
|
'code' => $array['error'], |
|
74
|
2 |
|
'message' => $array['error_description'] ?? $array['message'] ?? 'unknown_error' |
|
75
|
2 |
|
]; |
|
76
|
3 |
|
} elseif (isset($array['errors'])) { |
|
77
|
|
|
$this->errors = array_merge($this->errors, $array['errors']); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Проверка корректности выполненного запроса |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
6 |
|
public function isOk(): bool |
|
86
|
|
|
{ |
|
87
|
6 |
|
return $this->status >= 200 && $this->status <= 299; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Проверка наличия ошибок в запросе |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
2 |
|
public function hasErrors(): bool |
|
95
|
|
|
{ |
|
96
|
2 |
|
return count($this->errors) > 0; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
3 |
|
public function getBody(): string |
|
103
|
|
|
{ |
|
104
|
3 |
|
return $this->body; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function getErrors(): array |
|
111
|
|
|
{ |
|
112
|
1 |
|
return $this->errors; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return int |
|
117
|
|
|
*/ |
|
118
|
4 |
|
public function getStatus(): int |
|
119
|
|
|
{ |
|
120
|
4 |
|
return $this->status; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|