Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (success)
80.00%
12 / 15
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ProblemDetails
80.00% covered (success)
80.00%
12 / 15
50.00% covered (danger)
50.00%
1 / 2
5.20
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toArray
78.57% covered (warning)
78.57%
11 / 14
0.00% covered (danger)
0.00%
0 / 1
4.16
1<?php
2
3/**
4 *  The ProblemDetails class for API error responses
5 *
6 *  This Source Code Form is subject to the terms of the Mozilla Public License,
7 *  v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 *  obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 *  @package   phpMyFAQ
11 *  @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 *  @copyright 2026 phpMyFAQ Team
13 *  @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 *  @link      https://www.phpmyfaq.de
15 *  @since     2026-01-03
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Api;
21
22final readonly class ProblemDetails
23{
24    /* @mago-expect lint:excessive-parameter-list - mirrors the RFC 7807 problem-details fields */
25    public function __construct(
26        public string $type,
27        public string $title,
28        public int $status,
29        public string $detail,
30        public string $instance,
31        public ?string $code = null,
32        public ?array $errors = null, // field-level errors, optional
33        public ?string $traceId = null, // correlation id, optional
34    ) {
35    }
36
37    public function toArray(): array
38    {
39        $data = [
40            'type' => $this->type,
41            'title' => $this->title,
42            'status' => $this->status,
43            'detail' => $this->detail,
44            'instance' => $this->instance,
45        ];
46
47        if ($this->code !== null) {
48            $data['code'] = $this->code;
49        }
50
51        if ($this->errors !== null) {
52            $data['errors'] = $this->errors;
53        }
54
55        if ($this->traceId !== null) {
56            $data['traceId'] = $this->traceId;
57        }
58
59        return $data;
60    }
61}