1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cerbero\JsonApiError\Data; |
6
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Symfony\Component\HttpFoundation\Response; |
10
|
|
|
use Throwable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The JSON:API error data. |
14
|
|
|
* |
15
|
|
|
* @implements Arrayable<string, mixed> |
16
|
|
|
*/ |
17
|
|
|
final class JsonApiErrorData implements Arrayable |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Instantiate the class. |
21
|
|
|
* |
22
|
|
|
* @param array<string, mixed> $meta |
23
|
|
|
*/ |
24
|
11 |
|
public function __construct( |
25
|
|
|
public readonly ?string $detail = null, |
26
|
|
|
public readonly int $status = Response::HTTP_BAD_REQUEST, |
27
|
|
|
public readonly ?string $code = null, |
28
|
|
|
public readonly ?string $title = null, |
29
|
|
|
public readonly mixed $id = null, |
30
|
|
|
public readonly ?string $source = null, |
31
|
|
|
public readonly array $meta = [], |
32
|
11 |
|
) {} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Instantiate the class from the given unexpected throwable. |
36
|
|
|
*/ |
37
|
1 |
|
public static function unexpected(Throwable $e): self |
38
|
|
|
{ |
39
|
1 |
|
return new self(status: Response::HTTP_INTERNAL_SERVER_ERROR, meta: config('app.debug') ? [ |
40
|
1 |
|
'message' => $e->getMessage(), |
41
|
1 |
|
'previous_message' => $e->getPrevious()?->getMessage(), |
42
|
1 |
|
'trace' => $e->getTrace(), |
43
|
1 |
|
'previous_trace' => $e->getPrevious()?->getTrace(), |
44
|
1 |
|
] : []); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Instantiate the class from the given unprocessable entity. |
49
|
|
|
*/ |
50
|
2 |
|
public static function unprocessable(string $detail, ?string $dot = null): self |
51
|
|
|
{ |
52
|
2 |
|
return new self($detail, Response::HTTP_UNPROCESSABLE_ENTITY, source: $dot); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Retrieve the error as an array. |
57
|
|
|
* |
58
|
|
|
* @return array<string, mixed> |
59
|
|
|
*/ |
60
|
11 |
|
public function toArray() |
61
|
|
|
{ |
62
|
11 |
|
return [ |
63
|
11 |
|
'id' => $this->id, |
64
|
11 |
|
'status' => (string) $this->status, |
65
|
11 |
|
'code' => $this->code, |
66
|
11 |
|
'title' => $this->title ?: __("json-api-error::statuses.{$this->status}.title"), |
67
|
11 |
|
'detail' => $this->detail ?: __("json-api-error::statuses.{$this->status}.detail"), |
68
|
11 |
|
'source' => $this->source ? ['pointer' => (new Dot($this->source))->toJsonPointer()] : null, |
69
|
11 |
|
'meta' => $this->meta, |
70
|
11 |
|
]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Merge the given JSON:API error. |
75
|
|
|
*/ |
76
|
1 |
|
public function merge(JsonApiErrorData $data): self |
77
|
|
|
{ |
78
|
1 |
|
$filled = array_filter((array) $this, filled(...)); |
|
|
|
|
79
|
1 |
|
$mergeable = Arr::only((array) $data, ['id', 'code', 'meta']); |
80
|
|
|
|
81
|
1 |
|
return new self(...array_replace_recursive($mergeable, $filled)); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths