|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Document; |
|
5
|
|
|
|
|
6
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\LinksAwareInterface; |
|
7
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\LinksContainer; |
|
8
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\MetadataAwareInterface; |
|
9
|
|
|
use Mikemirten\Component\JsonApi\Document\Behaviour\MetadataContainer; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Error object |
|
13
|
|
|
* |
|
14
|
|
|
* @see http://jsonapi.org/format/#errors |
|
15
|
|
|
* |
|
16
|
|
|
* @package Mikemirten\Component\JsonApi\Document |
|
17
|
|
|
*/ |
|
18
|
|
|
class ErrorObject implements LinksAwareInterface, MetadataAwareInterface |
|
19
|
|
|
{ |
|
20
|
|
|
use LinksContainer; |
|
21
|
|
|
use MetadataContainer; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Identifier of occurrence |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $id; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* HTTP status code |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $status; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Application specific code |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $code; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Human-readable summary |
|
46
|
|
|
* |
|
47
|
|
|
* @var string |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $title; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Human-readable explanation |
|
53
|
|
|
* |
|
54
|
|
|
* @var string |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $detail; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* ErrorObject constructor. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $id |
|
62
|
|
|
* @param string $title |
|
63
|
|
|
*/ |
|
64
|
18 |
|
public function __construct(string $id = null, string $title = null) |
|
65
|
|
|
{ |
|
66
|
18 |
|
$this->id = $id; |
|
67
|
18 |
|
$this->title = $title; |
|
68
|
18 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Set ID of occurrence |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $id |
|
74
|
|
|
*/ |
|
75
|
3 |
|
public function setId(string $id) |
|
76
|
|
|
{ |
|
77
|
3 |
|
$this->id = $id; |
|
78
|
3 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Has ID of occurrence ? |
|
82
|
|
|
* |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public function hasId(): bool |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->id !== null; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get ID of occurrence |
|
92
|
|
|
* |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
2 |
|
public function getId(): string |
|
96
|
|
|
{ |
|
97
|
2 |
|
return $this->id; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set HTTP status code |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $status |
|
104
|
|
|
*/ |
|
105
|
3 |
|
public function setStatus(string $status) |
|
106
|
|
|
{ |
|
107
|
3 |
|
$this->status = $status; |
|
108
|
3 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Has HTTP status code ? |
|
112
|
|
|
* |
|
113
|
|
|
* @return bool |
|
114
|
|
|
*/ |
|
115
|
|
|
public function hasStatus(): bool |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->status !== null; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get HTTP status code |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
2 |
|
public function getStatus(): string |
|
126
|
|
|
{ |
|
127
|
2 |
|
return $this->status; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Set application specific code |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $code |
|
134
|
|
|
*/ |
|
135
|
3 |
|
public function setCode(string $code) |
|
136
|
|
|
{ |
|
137
|
3 |
|
$this->code = $code; |
|
138
|
3 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Has application specific code |
|
142
|
|
|
* |
|
143
|
|
|
* @return bool |
|
144
|
|
|
*/ |
|
145
|
|
|
public function hasCode(): bool |
|
146
|
|
|
{ |
|
147
|
|
|
return $this->code !== null; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get application specific code |
|
152
|
|
|
* |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
2 |
|
public function getCode(): string |
|
156
|
|
|
{ |
|
157
|
2 |
|
return $this->code; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Set title |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $title |
|
164
|
|
|
*/ |
|
165
|
3 |
|
public function setTitle(string $title) |
|
166
|
|
|
{ |
|
167
|
3 |
|
$this->title = $title; |
|
168
|
3 |
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Has title |
|
172
|
|
|
* |
|
173
|
|
|
* @return bool |
|
174
|
|
|
*/ |
|
175
|
|
|
public function hasTitle(): bool |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->title !== null; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Get title |
|
182
|
|
|
* |
|
183
|
|
|
* @return string |
|
184
|
|
|
*/ |
|
185
|
2 |
|
public function getTitle(): string |
|
186
|
|
|
{ |
|
187
|
2 |
|
return $this->title; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Set detailed explanation |
|
192
|
|
|
* |
|
193
|
|
|
* @param string $detail |
|
194
|
|
|
*/ |
|
195
|
3 |
|
public function setDetail(string $detail) |
|
196
|
|
|
{ |
|
197
|
3 |
|
$this->detail = $detail; |
|
198
|
3 |
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Has detailed explanation ? |
|
202
|
|
|
* |
|
203
|
|
|
* @return bool |
|
204
|
|
|
*/ |
|
205
|
|
|
public function hasDetail(): bool |
|
206
|
|
|
{ |
|
207
|
|
|
return $this->detail !== null; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Get detailed explanation |
|
212
|
|
|
* |
|
213
|
|
|
* @return string |
|
214
|
|
|
*/ |
|
215
|
2 |
|
public function getDetail(): string |
|
216
|
|
|
{ |
|
217
|
2 |
|
return $this->detail; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Cast to an array |
|
222
|
|
|
* |
|
223
|
|
|
* @return array |
|
224
|
|
|
*/ |
|
225
|
3 |
|
public function toArray(): array |
|
226
|
|
|
{ |
|
227
|
3 |
|
$data = array_filter([ |
|
228
|
3 |
|
'id' => $this->id, |
|
229
|
3 |
|
'status' => $this->status, |
|
230
|
3 |
|
'code' => $this->code, |
|
231
|
3 |
|
'title' => $this->title, |
|
232
|
3 |
|
'detail' => $this->detail |
|
233
|
|
|
]); |
|
234
|
|
|
|
|
235
|
3 |
|
if ($this->hasLinks()) { |
|
236
|
1 |
|
$data['links'] = $this->linksToArray(); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
3 |
|
if ($this->hasMetadata()) { |
|
240
|
1 |
|
$data['meta'] = $this->getMetadata(); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
3 |
|
return $data; |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Cast to a string |
|
248
|
|
|
* |
|
249
|
|
|
* @return string |
|
250
|
|
|
*/ |
|
251
|
5 |
|
public function __toString(): string |
|
252
|
|
|
{ |
|
253
|
|
|
return 'Error-object with ' . ( |
|
254
|
5 |
|
$this->id === null |
|
255
|
5 |
|
? 'no ID' |
|
256
|
5 |
|
: sprintf('ID: "%s"', $this->id) |
|
257
|
|
|
); |
|
258
|
|
|
} |
|
259
|
|
|
} |