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