|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace FRZB\Component\RequestMapper\ValueObject; |
|
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
|
|
|
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
|
|
|
public function __toString(): string |
|
24
|
|
|
{ |
|
25
|
|
|
$errors = HashMap::collect($this->errors) |
|
26
|
|
|
->map(static fn (Entry $e) => sprintf('%s: [%s]', $e->key, $e->value)) |
|
27
|
|
|
->toAssocArray() |
|
28
|
|
|
->getOrElse([]) |
|
29
|
|
|
; |
|
30
|
|
|
|
|
31
|
|
|
return sprintf('message: %s, status: %s, errors: %s', $this->message, $this->status, implode(', ', $errors)); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getMessage(): string |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->message; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getStatus(): int |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->status; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getErrors(): array |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->errors; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getTrace(): array |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->trace; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
#[ArrayShape(['message' => 'string', 'status' => 'int', 'errors' => 'array'])] |
|
55
|
|
|
public function jsonSerialize(): array |
|
56
|
|
|
{ |
|
57
|
|
|
return [ |
|
58
|
|
|
'message' => $this->message, |
|
59
|
|
|
'status' => $this->status, |
|
60
|
|
|
'errors' => $this->errors, |
|
61
|
|
|
]; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|