We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 9b1184f commit 957ab86Copy full SHA for 957ab86
1 file changed
src/Framework/Http/HttpResponse.php
@@ -0,0 +1,30 @@
1
+<?php
2
+declare(strict_types=1);
3
+
4
5
+namespace Lib\Framework\Http;
6
7
8
+use Hyperf\HttpServer\Exception\Http\EncodingException;
9
+use Hyperf\Utils\Contracts\Arrayable;
10
+use Hyperf\Utils\Contracts\Jsonable;
11
12
+class HttpResponse extends \Hyperf\HttpServer\Response
13
+{
14
+ protected function toJson($data): string
15
+ {
16
+ if (is_array($data)) {
17
+ return json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
18
+ }
19
20
+ if ($data instanceof Jsonable) {
21
+ return (string) $data;
22
23
24
+ if ($data instanceof Arrayable) {
25
+ return json_encode($data->toArray(), JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
26
27
28
+ throw new EncodingException('Error encoding response data to JSON.');
29
30
+}
0 commit comments