1 | <?php declare(strict_types=1); |
||
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 |
|
93 | |||
94 | /** |
||
95 | * @param iterable $errors |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | 1 | public function addErrors(iterable $errors): void |
|
105 | |||
106 | /** |
||
107 | * @return ErrorCollection |
||
108 | */ |
||
109 | 3 | public function getErrors(): ErrorCollection |
|
113 | |||
114 | /** |
||
115 | * @return int |
||
116 | */ |
||
117 | 3 | public function getHttpCode(): int |
|
121 | } |
||
122 |