|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Inspirum\Balikobot\Exceptions; |
|
4
|
|
|
|
|
5
|
|
|
use Inspirum\Balikobot\Contracts\ExceptionInterface; |
|
6
|
|
|
use Inspirum\Balikobot\Definitions\Response; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Throwable; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractException extends RuntimeException implements ExceptionInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Response data |
|
14
|
|
|
* |
|
15
|
|
|
* @var array<mixed> |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $response; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Response HTTP status code |
|
21
|
|
|
* |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $statusCode; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* API response errors |
|
28
|
|
|
* |
|
29
|
|
|
* @var array<array<string,string>> |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $errors = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* AbstractException constructor |
|
35
|
|
|
* |
|
36
|
|
|
* @param array<mixed> $response |
|
37
|
|
|
* @param int $statusCode |
|
38
|
|
|
* @param \Throwable|null $previous |
|
39
|
|
|
* @param string|null $message |
|
40
|
|
|
*/ |
|
41
|
137 |
|
public function __construct( |
|
42
|
|
|
array $response = [], |
|
43
|
|
|
int $statusCode = 500, |
|
44
|
|
|
Throwable $previous = null, |
|
45
|
|
|
string $message = null |
|
46
|
|
|
) { |
|
47
|
|
|
// set response data |
|
48
|
137 |
|
$this->response = $response; |
|
49
|
|
|
|
|
50
|
|
|
// overwrite default HTTP status code |
|
51
|
137 |
|
$this->statusCode = $statusCode; |
|
52
|
|
|
|
|
53
|
|
|
// overwrite default message |
|
54
|
137 |
|
if ($message === null) { |
|
55
|
137 |
|
$message = Response::$statusCodesErrors[$statusCode] ?? 'Operace neproběhla v pořádku.'; |
|
56
|
137 |
|
$message = $this->getMessageWithErrors($message); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// construct exception |
|
60
|
137 |
|
parent::__construct($message, $statusCode, $previous); |
|
61
|
137 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get response HTTP status code |
|
65
|
|
|
* |
|
66
|
|
|
* @return int |
|
67
|
|
|
*/ |
|
68
|
13 |
|
public function getStatusCode(): int |
|
69
|
|
|
{ |
|
70
|
13 |
|
return $this->statusCode; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get response as array |
|
75
|
|
|
* |
|
76
|
|
|
* @return array<mixed> |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function getResponse(): array |
|
79
|
|
|
{ |
|
80
|
1 |
|
return $this->response; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get response as string |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function getResponseAsString(): string |
|
89
|
|
|
{ |
|
90
|
1 |
|
return (string) json_encode($this->response); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get response errors |
|
95
|
|
|
* |
|
96
|
|
|
* @return array<array<string,string>> |
|
97
|
|
|
*/ |
|
98
|
137 |
|
public function getErrors(): array |
|
99
|
|
|
{ |
|
100
|
137 |
|
return $this->errors; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get message with errors |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $message |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
137 |
|
private function getMessageWithErrors(string $message): string |
|
111
|
|
|
{ |
|
112
|
137 |
|
$messages = [$message]; |
|
113
|
|
|
|
|
114
|
137 |
|
foreach ($this->getErrors() as $i => $errors) { |
|
115
|
22 |
|
foreach ($errors as $key => $error) { |
|
116
|
22 |
|
$messages[] = sprintf('[%s][%s]: %s', $i, $key, $error); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
137 |
|
return implode("\n", $messages); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|