1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Neomerx\JsonApi\Exceptions; |
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 Exception; |
22
|
|
|
use Neomerx\JsonApi\Contracts\Schema\ErrorInterface; |
23
|
|
|
use Neomerx\JsonApi\Schema\ErrorCollection; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @package Neomerx\JsonApi |
27
|
|
|
*/ |
28
|
|
|
class JsonApiException extends BaseJsonApiException |
29
|
|
|
{ |
30
|
|
|
/** Default HTTP code */ |
31
|
|
|
const HTTP_CODE_BAD_REQUEST = 400; |
32
|
|
|
|
33
|
|
|
/** Default HTTP code */ |
34
|
|
|
const HTTP_CODE_FORBIDDEN = 403; |
35
|
|
|
|
36
|
|
|
/** Default HTTP code */ |
37
|
|
|
const HTTP_CODE_NOT_ACCEPTABLE = 406; |
38
|
|
|
|
39
|
|
|
/** Default HTTP code */ |
40
|
|
|
const HTTP_CODE_CONFLICT = 409; |
41
|
|
|
|
42
|
|
|
/** Default HTTP code */ |
43
|
|
|
const HTTP_CODE_UNSUPPORTED_MEDIA_TYPE = 415; |
44
|
|
|
|
45
|
|
|
/** Default HTTP code */ |
46
|
|
|
const DEFAULT_HTTP_CODE = self::HTTP_CODE_BAD_REQUEST; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var ErrorCollection |
50
|
|
|
*/ |
51
|
|
|
private $errors; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
private $httpCode; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param ErrorInterface|iterable $errors |
60
|
|
|
* @param int $httpCode |
61
|
|
|
* @param Exception|null $previous |
62
|
|
|
* |
63
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
64
|
|
|
* @SuppressWarnings(PHPMD.ElseExpression) |
65
|
|
|
*/ |
66
|
9 |
|
public function __construct($errors, int $httpCode = self::DEFAULT_HTTP_CODE, Exception $previous = null) |
67
|
|
|
{ |
68
|
9 |
|
parent::__construct('JSON API error', 0, $previous); |
69
|
|
|
|
70
|
9 |
|
if ($errors instanceof ErrorCollection) { |
71
|
1 |
|
$this->errors = clone $errors; |
72
|
8 |
|
} elseif (is_iterable($errors) === true) { |
73
|
1 |
|
$this->errors = new ErrorCollection(); |
74
|
1 |
|
$this->addErrors($errors); |
75
|
|
|
} else { |
76
|
|
|
// should be ErrorInterface |
77
|
7 |
|
$this->errors = new ErrorCollection(); |
78
|
7 |
|
$this->addError($errors); |
79
|
|
|
} |
80
|
|
|
|
81
|
9 |
|
$this->httpCode = $httpCode; |
82
|
9 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param ErrorInterface $error |
86
|
|
|
* |
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
8 |
|
public function addError(ErrorInterface $error): void |
90
|
|
|
{ |
91
|
8 |
|
$this->errors[] = $error; |
92
|
8 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param iterable $errors |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
1 |
|
public function addErrors(iterable $errors): void |
100
|
|
|
{ |
101
|
1 |
|
foreach ($errors as $error) { |
102
|
1 |
|
$this->addError($error); |
103
|
|
|
} |
104
|
1 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return ErrorCollection |
108
|
|
|
*/ |
109
|
3 |
|
public function getErrors(): ErrorCollection |
110
|
|
|
{ |
111
|
3 |
|
return $this->errors; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return int |
116
|
|
|
*/ |
117
|
3 |
|
public function getHttpCode(): int |
118
|
|
|
{ |
119
|
3 |
|
return $this->httpCode; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|