1 | <?php |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Gewaer\Http; |
||
6 | |||
7 | use Phalcon\Http\Response as PhResponse; |
||
8 | use Phalcon\Mvc\Model\MessageInterface as ModelMessage; |
||
9 | use Phalcon\Validation\Message\Group as ValidationMessage; |
||
10 | |||
11 | class Response extends PhResponse |
||
12 | { |
||
13 | const OK = 200; |
||
14 | const CREATED = 201; |
||
15 | const ACCEPTED = 202; |
||
16 | const MOVED_PERMANENTLY = 301; |
||
17 | const FOUND = 302; |
||
18 | const TEMPORARY_REDIRECT = 307; |
||
19 | const PERMANENTLY_REDIRECT = 308; |
||
20 | const BAD_REQUEST = 400; |
||
21 | const UNAUTHORIZED = 401; |
||
22 | const FORBIDDEN = 403; |
||
23 | const NOT_FOUND = 404; |
||
24 | const NOT_ACCEPTABLE = 406; |
||
25 | const INTERNAL_SERVER_ERROR = 500; |
||
26 | const NOT_IMPLEMENTED = 501; |
||
27 | const BAD_GATEWAY = 502; |
||
28 | |||
29 | private $codes = [ |
||
30 | 200 => 'OK', |
||
31 | 301 => 'Moved Permanently', |
||
32 | 302 => 'Found', |
||
33 | 307 => 'Temporary Redirect', |
||
34 | 308 => 'Permanent Redirect', |
||
35 | 400 => 'Bad Request', |
||
36 | 401 => 'Unauthorized', |
||
37 | 403 => 'Forbidden', |
||
38 | 404 => 'Not Found', |
||
39 | 500 => 'Internal Server Error', |
||
40 | 501 => 'Not Implemented', |
||
41 | 502 => 'Bad Gateway', |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * Returns the http code description or if not found the code itself |
||
46 | * @param int $code |
||
47 | * |
||
48 | * @return int|string |
||
49 | */ |
||
50 | 2 | public function getHttpCodeDescription(int $code) |
|
51 | { |
||
52 | 2 | if (true === isset($this->codes[$code])) { |
|
53 | 2 | return sprintf('%d (%s)', $code, $this->codes[$code]); |
|
54 | } |
||
55 | |||
56 | 1 | return $code; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * Send the response back |
||
61 | * |
||
62 | * @return PhResponse |
||
63 | */ |
||
64 | 1 | public function send(): PhResponse |
|
65 | { |
||
66 | 1 | $content = $this->getContent(); |
|
67 | 1 | $timestamp = date('c'); |
|
68 | 1 | $hash = sha1($timestamp . $content); |
|
69 | 1 | $eTag = sha1($content); |
|
70 | |||
71 | /** @var array $content */ |
||
72 | 1 | $content = json_decode($this->getContent(), true); |
|
73 | |||
74 | $jsonapi = [ |
||
75 | 1 | 'jsonapi' => [ |
|
76 | 'version' => '1.0', |
||
77 | ], |
||
78 | ]; |
||
79 | $meta = [ |
||
80 | 'meta' => [ |
||
81 | 1 | 'timestamp' => $timestamp, |
|
82 | 1 | 'hash' => $hash, |
|
83 | ] |
||
84 | ]; |
||
85 | |||
86 | /** |
||
87 | * Join the array again |
||
88 | */ |
||
89 | 1 | $data = $jsonapi + $content + $meta; |
|
90 | $this |
||
91 | 1 | ->setHeader('E-Tag', $eTag) |
|
92 | 1 | ->setJsonContent($data); |
|
93 | |||
94 | 1 | return parent::send(); |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * Sets the payload code as Error |
||
99 | * |
||
100 | * @param string $detail |
||
101 | * |
||
102 | * @return Response |
||
103 | */ |
||
104 | 2 | public function setPayloadError(string $detail = ''): Response |
|
105 | { |
||
106 | 2 | $this->setJsonContent([ |
|
107 | 'errors' => [ |
||
108 | 2 | 'message' => $detail, |
|
109 | 2 | 'type' => $this->codes[404] |
|
110 | ] |
||
111 | ]); |
||
112 | |||
113 | 2 | return $this; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Traverses the errors collection and sets the errors in the payload |
||
118 | * |
||
119 | * @param ModelMessage[]|ValidationMessage $errors |
||
120 | * |
||
121 | * @return Response |
||
122 | */ |
||
123 | public function setPayloadErrors($errors): Response |
||
124 | { |
||
125 | $data = []; |
||
126 | foreach ($errors as $error) { |
||
127 | $data[] = $error->getMessage(); |
||
128 | } |
||
129 | |||
130 | $this->setJsonContent(['errors' => $data]); |
||
131 | |||
132 | return $this; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Sets the payload code as Success |
||
137 | * |
||
138 | * @param null|string|array $content The content |
||
139 | * |
||
140 | * @return Response |
||
141 | */ |
||
142 | 1 | public function setPayloadSuccess($content = []): Response |
|
143 | { |
||
144 | 1 | $data = (true === is_array($content)) ? $content : ['data' => $content]; |
|
145 | 1 | $data = (true === isset($data['data'])) ? $data : ['data' => $data]; |
|
146 | |||
147 | 1 | $this->setJsonContent($data); |
|
148 | |||
149 | 1 | return $this; |
|
150 | } |
||
151 | } |
||
152 |