1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Neomerx\JsonApi\Schema; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Neomerx\JsonApi\Contracts\Schema\DocumentInterface; |
22
|
|
|
use Neomerx\JsonApi\Contracts\Schema\ErrorInterface; |
23
|
|
|
use Neomerx\JsonApi\Contracts\Schema\LinkInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @package Neomerx\JsonApi |
27
|
|
|
* |
28
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
29
|
|
|
*/ |
30
|
|
|
class Error implements ErrorInterface |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* @var int|string|null |
34
|
|
|
*/ |
35
|
|
|
private $index; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var null|iterable |
39
|
|
|
*/ |
40
|
|
|
private $links; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var null|iterable |
44
|
|
|
*/ |
45
|
|
|
private $typeLinks; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string|null |
49
|
|
|
*/ |
50
|
|
|
private $status; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string|null |
54
|
|
|
*/ |
55
|
|
|
private $code; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string|null |
59
|
|
|
*/ |
60
|
|
|
private $title; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string|null |
64
|
|
|
*/ |
65
|
|
|
private $detail; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array|null |
69
|
|
|
*/ |
70
|
|
|
private $source; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var bool |
74
|
|
|
*/ |
75
|
|
|
private $hasMeta; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var mixed |
79
|
|
|
*/ |
80
|
|
|
private $meta; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param int|string|null $idx |
84
|
|
|
* @param LinkInterface|null $aboutLink |
85
|
|
|
* @param iterable|null $typeLinks |
86
|
|
|
* @param string|null $status |
87
|
|
|
* @param string|null $code |
88
|
|
|
* @param string|null $title |
89
|
|
|
* @param string|null $detail |
90
|
|
|
* @param array|null $source |
91
|
|
|
* @param bool $hasMeta |
92
|
|
|
* @param mixed $meta |
93
|
|
|
* |
94
|
|
|
* @SuppressWarnings(PHPMD.BooleanArgumentFlag) |
95
|
|
|
* @SuppressWarnings(PHPMD.ExcessiveParameterList) |
96
|
|
|
*/ |
97
|
30 |
|
public function __construct( |
98
|
|
|
$idx = null, |
99
|
|
|
LinkInterface $aboutLink = null, |
100
|
|
|
?iterable $typeLinks = null, |
101
|
|
|
string $status = null, |
102
|
|
|
string $code = null, |
103
|
|
|
string $title = null, |
104
|
|
|
string $detail = null, |
105
|
|
|
array $source = null, |
106
|
|
|
bool $hasMeta = false, |
107
|
|
|
$meta = null |
108
|
|
|
) { |
109
|
|
|
$this |
110
|
30 |
|
->setId($idx) |
111
|
30 |
|
->setLink(DocumentInterface::KEYWORD_ERRORS_ABOUT, $aboutLink) |
112
|
30 |
|
->setTypeLinks($typeLinks) |
113
|
30 |
|
->setStatus($status) |
114
|
30 |
|
->setCode($code) |
115
|
30 |
|
->setTitle($title) |
116
|
30 |
|
->setDetail($detail) |
117
|
30 |
|
->setSource($source); |
118
|
|
|
|
119
|
30 |
|
if (($this->hasMeta = $hasMeta) === true) { |
120
|
4 |
|
$this->setMeta($meta); |
121
|
|
|
} |
122
|
30 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @inheritdoc |
126
|
|
|
*/ |
127
|
6 |
|
public function getId() |
128
|
|
|
{ |
129
|
6 |
|
return $this->index; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string|int|null $index |
134
|
|
|
* |
135
|
|
|
* @return self |
136
|
|
|
*/ |
137
|
30 |
|
public function setId($index): self |
138
|
|
|
{ |
139
|
30 |
|
assert($index === null || is_int($index) === true || is_string($index) === true); |
140
|
|
|
|
141
|
30 |
|
$this->index = $index; |
142
|
|
|
|
143
|
30 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @inheritdoc |
148
|
|
|
*/ |
149
|
6 |
|
public function getLinks(): ?iterable |
150
|
|
|
{ |
151
|
6 |
|
return $this->links; |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritdoc |
156
|
|
|
*/ |
157
|
6 |
|
public function getTypeLinks(): ?iterable |
158
|
|
|
{ |
159
|
6 |
|
return $this->typeLinks; |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param string $name |
164
|
|
|
* @param LinkInterface|null $link |
165
|
|
|
* |
166
|
|
|
* @return self |
167
|
|
|
* |
168
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
169
|
|
|
*/ |
170
|
30 |
|
public function setLink(string $name, ?LinkInterface $link): self |
171
|
|
|
{ |
172
|
30 |
|
if ($link !== null) { |
173
|
4 |
|
$this->links[$name] = $link; |
174
|
|
|
} else { |
175
|
26 |
|
unset($this->links[$name]); |
176
|
|
|
} |
177
|
|
|
|
178
|
30 |
|
return $this; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param iterable|null $typeLinks |
183
|
|
|
* |
184
|
|
|
* @return self |
185
|
|
|
*/ |
186
|
30 |
|
public function setTypeLinks(?iterable $typeLinks): self |
187
|
|
|
{ |
188
|
30 |
|
$this->typeLinks = $typeLinks; |
189
|
|
|
|
190
|
30 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @inheritdoc |
195
|
|
|
*/ |
196
|
6 |
|
public function getStatus(): ?string |
197
|
|
|
{ |
198
|
6 |
|
return $this->status; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string|null $status |
203
|
|
|
* |
204
|
|
|
* @return self |
205
|
|
|
*/ |
206
|
30 |
|
public function setStatus(?string $status): self |
207
|
|
|
{ |
208
|
30 |
|
$this->status = $status; |
209
|
|
|
|
210
|
30 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @inheritdoc |
215
|
|
|
*/ |
216
|
6 |
|
public function getCode(): ?string |
217
|
|
|
{ |
218
|
6 |
|
return $this->code; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param string|null $code |
223
|
|
|
* |
224
|
|
|
* @return self |
225
|
|
|
*/ |
226
|
30 |
|
public function setCode(?string $code): self |
227
|
|
|
{ |
228
|
30 |
|
$this->code = $code; |
229
|
|
|
|
230
|
30 |
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @inheritdoc |
235
|
|
|
*/ |
236
|
7 |
|
public function getTitle(): ?string |
237
|
|
|
{ |
238
|
7 |
|
return $this->title; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param null|string $title |
243
|
|
|
* |
244
|
|
|
* @return self |
245
|
|
|
*/ |
246
|
30 |
|
public function setTitle(?string $title): self |
247
|
|
|
{ |
248
|
30 |
|
$this->title = $title; |
249
|
|
|
|
250
|
30 |
|
return $this; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @inheritdoc |
255
|
|
|
*/ |
256
|
6 |
|
public function getDetail(): ?string |
257
|
|
|
{ |
258
|
6 |
|
return $this->detail; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param null|string $detail |
263
|
|
|
* |
264
|
|
|
* @return self |
265
|
|
|
*/ |
266
|
30 |
|
public function setDetail(?string $detail): self |
267
|
|
|
{ |
268
|
30 |
|
$this->detail = $detail; |
269
|
|
|
|
270
|
30 |
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @inheritdoc |
275
|
|
|
*/ |
276
|
16 |
|
public function getSource(): ?array |
277
|
|
|
{ |
278
|
16 |
|
return $this->source; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* @param array|null $source |
283
|
|
|
* |
284
|
|
|
* @return self |
285
|
|
|
*/ |
286
|
30 |
|
public function setSource(?array $source): self |
287
|
|
|
{ |
288
|
30 |
|
$this->source = $source; |
289
|
|
|
|
290
|
30 |
|
return $this; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @inheritdoc |
295
|
|
|
*/ |
296
|
6 |
|
public function hasMeta(): bool |
297
|
|
|
{ |
298
|
6 |
|
return $this->hasMeta; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @inheritdoc |
303
|
|
|
*/ |
304
|
4 |
|
public function getMeta() |
305
|
|
|
{ |
306
|
4 |
|
return $this->meta; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* @param mixed|null $meta |
311
|
|
|
* |
312
|
|
|
* @return self |
313
|
|
|
*/ |
314
|
4 |
|
public function setMeta($meta): self |
315
|
|
|
{ |
316
|
4 |
|
$this->hasMeta = true; |
317
|
4 |
|
$this->meta = $meta; |
318
|
|
|
|
319
|
4 |
|
return $this; |
320
|
|
|
} |
321
|
|
|
} |
322
|
|
|
|