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