AbstractError::setLink()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Happyr\JsonApiResponseFactory\Model;
6
7
use Happyr\JsonApiResponseFactory\ResponseModelInterface;
8
9
/**
10
 * @see https://jsonapi.org/format/#error-objects
11
 *
12
 * @author Tobias Nyholm <[email protected]>
13
 */
14
abstract class AbstractError implements ResponseModelInterface
15
{
16
    private $id;
17
    private $link;
18
    private $httpStatusCode;
19
    private $code;
20
    private $title;
21
    private $description;
22
    private $source;
23
    private $meta;
24
25
    /**
26
     * @param string $title          a short, human-readable summary of the problem that SHOULD NOT change from occurrence
27
     *                               to occurrence of the problem, except for purposes of localization
28
     * @param int    $httpStatusCode the HTTP status code applicable to this problem, expressed as a string value
29
     */
30 9
    public function __construct(string $title, int $httpStatusCode)
31
    {
32 9
        $this->title = $title;
33 9
        $this->httpStatusCode = $httpStatusCode;
34 9
    }
35
36
    public function getHttpStatusCode(): int
37
    {
38
        return $this->httpStatusCode;
39
    }
40
41
    /**
42
     * A unique identifier for this particular occurrence of the problem.
43
     */
44
    public function setId(string $id): void
45
    {
46
        $this->id = $id;
47
    }
48
49
    /**
50
     * A link that leads to further details about this particular occurrence of the problem.
51
     */
52
    public function setLink(string $link): void
53
    {
54
        $this->link = $link;
55
    }
56
57
    /**
58
     * An application-specific error code, expressed as a string value.
59
     */
60
    public function setCode(string $code): void
61
    {
62
        $this->code = $code;
63
    }
64
65
    /**
66
     * A human-readable explanation specific to this occurrence of the problem. Like title, this field’s value can be localized.
67
     */
68
    public function setDescription(string $description): void
69
    {
70
        $this->description = $description;
71
    }
72
73
    /**
74
     * An object containing references to the source of the error.
75
     *
76
     * May include the following keys:
77
     *  - parameter: a string indicating which URI query parameter caused the error.
78
     *  - pointer: a JSON Pointer [RFC6901] to the associated entity in the request document [e.g. "/data" for a primary
79
     *             data object, or "/data/attributes/title" for a specific attribute].
80
     */
81 1
    public function setSource(array $source): void
82
    {
83 1
        $this->source = $source;
84 1
    }
85
86
    /**
87
     * a meta object containing non-standard meta-information about the error.
88
     *
89
     * @see https://jsonapi.org/format/#document-meta
90
     */
91
    public function setMeta(array $meta): void
92
    {
93
        $this->meta = $meta;
94
    }
95
96
    public function getPayload(): array
97
    {
98
        return [
99
            'errors' => [$this->getErrorData()],
100
        ];
101
    }
102
103
    /**
104
     * Get the error object.
105
     */
106 7
    public function getErrorData(): array
107
    {
108
        $error = [
109 7
            'status' => (string) $this->httpStatusCode,
110 7
            'title' => $this->title,
111
        ];
112
113 7
        if (null !== $this->id) {
114
            $error['id'] = $this->id;
115
        }
116
117 7
        if (null !== $this->code) {
118
            $error['code'] = $this->code;
119
        }
120
121 7
        if (null !== $this->description) {
122
            $error['detail'] = $this->description;
123
        }
124
125 7
        if (null !== $this->source) {
126 1
            $error['source'] = $this->source;
127
        }
128
129 7
        if (null !== $this->meta) {
130
            $error['meta'] = $this->meta;
131
        }
132
133 7
        if (null !== $this->link) {
134
            $error['links'] = ['about' => $this->link];
135
        }
136
137 7
        return $error;
138
    }
139
}
140