Maurits van der Schee 5 년 전
부모
커밋
4474b66dff
2개의 변경된 파일86개의 추가작업 그리고 0개의 파일을 삭제
  1. 37
    0
      src/Tqdev/PhpCrudApi/ResponseFactory.php
  2. 49
    0
      src/Tqdev/PhpCrudApi/ResponseUtils.php

+ 37
- 0
src/Tqdev/PhpCrudApi/ResponseFactory.php 파일 보기

@@ -0,0 +1,37 @@
1
+<?php
2
+namespace Tqdev\PhpCrudApi;
3
+
4
+use Nyholm\Psr7\Factory\Psr17Factory;
5
+use Psr\Http\Message\ResponseInterface;
6
+
7
+class ResponseFactory
8
+{
9
+    const OK = 200;
10
+    const UNAUTHORIZED = 401;
11
+    const FORBIDDEN = 403;
12
+    const NOT_FOUND = 404;
13
+    const METHOD_NOT_ALLOWED = 405;
14
+    const CONFLICT = 409;
15
+    const UNPROCESSABLE_ENTITY = 422;
16
+    const INTERNAL_SERVER_ERROR = 500;
17
+
18
+    public static function fromObject(int $status, $body): ResponseInterface
19
+    {
20
+        $psr17Factory = new Psr17Factory();
21
+        $response = $psr17Factory->createResponse($status);
22
+        $content = json_encode($body, JSON_UNESCAPED_UNICODE);
23
+        $stream = $psr17Factory->createStream($content);
24
+        $stream->rewind();
25
+        $response = $response->withBody($stream);
26
+        $response = $response->withHeader('Content-Type', 'application/json');
27
+        $response = $response->withHeader('Content-Length', strlen($content));
28
+        return $response;
29
+    }
30
+
31
+    public static function fromStatus(int $status): ResponseInterface
32
+    {
33
+        $psr17Factory = new Psr17Factory();
34
+        return $psr17Factory->createResponse($status);
35
+    }
36
+
37
+}

+ 49
- 0
src/Tqdev/PhpCrudApi/ResponseUtils.php 파일 보기

@@ -0,0 +1,49 @@
1
+<?php
2
+namespace Tqdev\PhpCrudApi;
3
+
4
+use Psr\Http\Message\ResponseInterface;
5
+
6
+class ResponseUtils
7
+{
8
+    public static function output(ResponseInterface $response)
9
+    {
10
+        $status = $response->getStatusCode();
11
+        $headers = $response->getHeaders();
12
+        $body = $response->getBody()->getContents();
13
+
14
+        http_response_code($status);
15
+        foreach ($headers as $key => $values) {
16
+            foreach ($values as $value) {
17
+                header("$key: $value");
18
+            }
19
+        }
20
+        echo $body;
21
+    }
22
+
23
+    public static function addExceptionHeaders(ResponseInterface $response, \Throwable $e): ResponseInterface
24
+    {
25
+        $response = $response->withHeader('X-Exception-Name', get_class($e));
26
+        $response = $response->withHeader('X-Exception-Message', $e->getMessage());
27
+        $response = $response->withHeader('X-Exception-File', $e->getFile() . ':' . $e->getLine());
28
+        return $response;
29
+    }
30
+
31
+    public static function toString(ResponseInterface $response): string
32
+    {
33
+        $status = $response->getStatusCode();
34
+        $headers = $response->getHeaders();
35
+        $body = $response->getBody()->getContents();
36
+
37
+        $str = "$status\n";
38
+        foreach ($headers as $key => $values) {
39
+            foreach ($values as $value) {
40
+                $str .= "$key: $value\n";
41
+            }
42
+        }
43
+        if ($body !== '') {
44
+            $str .= "\n";
45
+            $str .= "$body\n";
46
+        }
47
+        return $str;
48
+    }
49
+}

Loading…
취소
저장