1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace FRZB\Component\RequestMapper\Data; |
6
|
|
|
|
7
|
|
|
use JetBrains\PhpStorm\ArrayShape; |
8
|
|
|
use JetBrains\PhpStorm\Immutable; |
9
|
|
|
|
10
|
|
|
#[Immutable] |
11
|
|
|
final class FormattedError implements ErrorContract |
12
|
|
|
{ |
13
|
|
|
public function __construct( |
14
|
|
|
private string $message, |
15
|
|
|
private int $status, |
16
|
|
|
private array $errors = [], |
17
|
|
|
private array $trace = [], |
18
|
|
|
) { |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function __toString(): string |
22
|
|
|
{ |
23
|
|
|
$errors = array_map( |
24
|
|
|
static fn (string $value, string $field) => sprintf('%s: [%s]', $field, $value), |
25
|
|
|
array_values($this->errors), |
26
|
|
|
array_keys($this->errors) |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
return sprintf('message: %s, status: %s, errors: %s', $this->message, $this->status, implode(', ', $errors)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function getMessage(): string |
33
|
|
|
{ |
34
|
|
|
return $this->message; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getStatus(): int |
38
|
|
|
{ |
39
|
|
|
return $this->status; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getErrors(): array |
43
|
|
|
{ |
44
|
|
|
return $this->errors; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getTrace(): array |
48
|
|
|
{ |
49
|
|
|
return $this->trace; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
#[ArrayShape(['message' => 'string', 'status' => 'int', 'errors' => 'array'])] |
53
|
|
|
public function jsonSerialize(): array |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'message' => $this->message, |
57
|
|
|
'status' => $this->status, |
58
|
|
|
'errors' => $this->errors, |
59
|
|
|
]; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|