@@ -18,15 +18,7 @@ class JsonApiHttpResponse extends Response
1818 */
1919 public static function validationError (array $ data ): self
2020 {
21- $ content = json_encode ($ data , JSON_THROW_ON_ERROR );
22-
23- return new self (
24- $ content ?: null ,
25- Response::HTTP_UNPROCESSABLE_ENTITY ,
26- [
27- 'Content-Type ' => self ::CONTENT_TYPE ,
28- ]
29- );
21+ return self ::makeError ($ data , Response::HTTP_UNPROCESSABLE_ENTITY );
3022 }
3123
3224 public static function notFound (): self
@@ -47,15 +39,7 @@ public static function notFound(): self
4739 */
4840 public static function badRequest (array $ data ): self
4941 {
50- $ content = json_encode ($ data , JSON_THROW_ON_ERROR );
51-
52- return new self (
53- $ content ?: null ,
54- Response::HTTP_BAD_REQUEST ,
55- [
56- 'Content-Type ' => self ::CONTENT_TYPE ,
57- ]
58- );
42+ return self ::makeError ($ data , Response::HTTP_BAD_REQUEST );
5943 }
6044
6145 /**
@@ -65,15 +49,7 @@ public static function badRequest(array $data): self
6549 */
6650 public static function forbidden (array $ data ): self
6751 {
68- $ content = json_encode ($ data , JSON_THROW_ON_ERROR );
69-
70- return new self (
71- $ content ?: null ,
72- Response::HTTP_FORBIDDEN ,
73- [
74- 'Content-Type ' => self ::CONTENT_TYPE ,
75- ]
76- );
52+ return self ::makeError ($ data , Response::HTTP_FORBIDDEN );
7753 }
7854
7955 /**
@@ -83,15 +59,7 @@ public static function forbidden(array $data): self
8359 */
8460 public static function unauthorized (array $ data ): self
8561 {
86- $ content = json_encode ($ data , JSON_THROW_ON_ERROR );
87-
88- return new self (
89- $ content ?: null ,
90- Response::HTTP_UNAUTHORIZED ,
91- [
92- 'Content-Type ' => self ::CONTENT_TYPE ,
93- ]
94- );
62+ return self ::makeError ($ data , Response::HTTP_UNAUTHORIZED );
9563 }
9664
9765 /**
@@ -101,15 +69,7 @@ public static function unauthorized(array $data): self
10169 */
10270 public static function serverError (array $ data ): self
10371 {
104- $ content = json_encode ($ data , JSON_THROW_ON_ERROR );
105-
106- return new self (
107- $ content ?: null ,
108- Response::HTTP_INTERNAL_SERVER_ERROR ,
109- [
110- 'Content-Type ' => self ::CONTENT_TYPE ,
111- ]
112- );
72+ return self ::makeError ($ data , Response::HTTP_INTERNAL_SERVER_ERROR );
11373 }
11474
11575 /**
@@ -118,12 +78,32 @@ public static function serverError(array $data): self
11878 * @throws \JsonException
11979 */
12080 public static function fromSymfonyHttpException (array $ data , HttpExceptionInterface $ exception ): self
81+ {
82+ return self ::makeError ($ data , $ exception ->getStatusCode ());
83+ }
84+
85+ /**
86+ * @param array<string, mixed> $data
87+ *
88+ * @throws \JsonException
89+ */
90+ public static function conflict (array $ data ): self
91+ {
92+ return self ::makeError ($ data , Response::HTTP_CONFLICT );
93+ }
94+
95+ /**
96+ * @param array<string, mixed> $data
97+ *
98+ * @throws \JsonException
99+ */
100+ private static function makeError (array $ data , int $ statusCode ): self
121101 {
122102 $ content = json_encode ($ data , JSON_THROW_ON_ERROR );
123103
124104 return new self (
125105 $ content ?: null ,
126- $ exception -> getStatusCode () ,
106+ $ statusCode ,
127107 [
128108 'Content-Type ' => self ::CONTENT_TYPE ,
129109 ]
0 commit comments