|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file is part of the Phalcon API. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Phalcon Team <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view |
|
11
|
|
|
* the LICENSE file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace MikoPBX\PBXCoreREST\Http; |
|
15
|
|
|
|
|
16
|
|
|
use Phalcon\Http\Response as PhResponse; |
|
17
|
|
|
use Phalcon\Http\ResponseInterface; |
|
18
|
|
|
use Phalcon\Messages\Messages; |
|
19
|
|
|
|
|
20
|
|
|
use function date; |
|
21
|
|
|
use function json_decode; |
|
22
|
|
|
use function sha1; |
|
23
|
|
|
|
|
24
|
|
|
class Response extends PhResponse |
|
25
|
|
|
{ |
|
26
|
|
|
public const OK = 200; |
|
27
|
|
|
|
|
28
|
|
|
public const CREATED = 201; |
|
29
|
|
|
|
|
30
|
|
|
public const ACCEPTED = 202; |
|
31
|
|
|
|
|
32
|
|
|
public const MOVED_PERMANENTLY = 301; |
|
33
|
|
|
|
|
34
|
|
|
public const FOUND = 302; |
|
35
|
|
|
|
|
36
|
|
|
public const TEMPORARY_REDIRECT = 307; |
|
37
|
|
|
|
|
38
|
|
|
public const PERMANENTLY_REDIRECT = 308; |
|
39
|
|
|
|
|
40
|
|
|
public const BAD_REQUEST = 400; |
|
41
|
|
|
|
|
42
|
|
|
public const UNAUTHORIZED = 401; |
|
43
|
|
|
|
|
44
|
|
|
public const FORBIDDEN = 403; |
|
45
|
|
|
|
|
46
|
|
|
public const NOT_FOUND = 404; |
|
47
|
|
|
|
|
48
|
|
|
public const INTERNAL_SERVER_ERROR = 500; |
|
49
|
|
|
|
|
50
|
|
|
public const NOT_IMPLEMENTED = 501; |
|
51
|
|
|
|
|
52
|
|
|
public const BAD_GATEWAY = 502; |
|
53
|
|
|
|
|
54
|
|
|
private $codes = [ |
|
55
|
|
|
200 => 'OK', |
|
56
|
|
|
301 => 'Moved Permanently', |
|
57
|
|
|
302 => 'Found', |
|
58
|
|
|
307 => 'Temporary Redirect', |
|
59
|
|
|
308 => 'Permanent Redirect', |
|
60
|
|
|
400 => 'Bad Request', |
|
61
|
|
|
401 => 'Unauthorized', |
|
62
|
|
|
403 => 'Forbidden', |
|
63
|
|
|
404 => 'Not Found', |
|
64
|
|
|
500 => 'Internal Server Error', |
|
65
|
|
|
501 => 'Not Implemented', |
|
66
|
|
|
502 => 'Bad Gateway', |
|
67
|
|
|
]; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Returns the http code description or if not found the code itself |
|
71
|
|
|
* |
|
72
|
|
|
* @param int $code |
|
73
|
|
|
* |
|
74
|
|
|
* @return int|string |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getHttpCodeDescription(int $code) |
|
77
|
|
|
{ |
|
78
|
|
|
if (true === isset($this->codes[$code])) { |
|
79
|
|
|
return sprintf('%d (%s)', $code, $this->codes[$code]); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $code; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Send the response back |
|
87
|
|
|
* |
|
88
|
|
|
* @return ResponseInterface |
|
89
|
|
|
*/ |
|
90
|
|
|
public function send(): ResponseInterface |
|
91
|
|
|
{ |
|
92
|
|
|
$content = $this->getContent(); |
|
93
|
|
|
$timestamp = date('c'); |
|
94
|
|
|
$hash = sha1($timestamp . $content); |
|
95
|
|
|
$eTag = sha1($content); |
|
96
|
|
|
|
|
97
|
|
|
/** @var array $content */ |
|
98
|
|
|
$content = json_decode($this->getContent(), true); |
|
99
|
|
|
$jsonapi = [ |
|
100
|
|
|
'jsonapi' => [ |
|
101
|
|
|
'version' => '1.0', |
|
102
|
|
|
], |
|
103
|
|
|
]; |
|
104
|
|
|
$meta = [ |
|
105
|
|
|
'meta' => [ |
|
106
|
|
|
'timestamp' => $timestamp, |
|
107
|
|
|
'hash' => $hash, |
|
108
|
|
|
], |
|
109
|
|
|
]; |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Join the array again |
|
113
|
|
|
*/ |
|
114
|
|
|
$data = $jsonapi + $content + $meta; |
|
115
|
|
|
$this |
|
116
|
|
|
->setHeader('E-Tag', $eTag) |
|
117
|
|
|
->setJsonContent($data); |
|
118
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
return parent::send(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Sets the payload code as Error |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $detail |
|
127
|
|
|
* |
|
128
|
|
|
* @return Response |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setPayloadError(string $detail = ''): Response |
|
131
|
|
|
{ |
|
132
|
|
|
$this->setJsonContent(['errors' => [$detail]]); |
|
133
|
|
|
|
|
134
|
|
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Traverses the errors collection and sets the errors in the payload |
|
139
|
|
|
* |
|
140
|
|
|
* @param Messages $errors |
|
141
|
|
|
* |
|
142
|
|
|
* @return Response |
|
143
|
|
|
*/ |
|
144
|
|
|
public function setPayloadErrors($errors): Response |
|
145
|
|
|
{ |
|
146
|
|
|
$data = []; |
|
147
|
|
|
foreach ($errors as $error) { |
|
148
|
|
|
$data[] = $error->getMessage(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$this->setJsonContent(['errors' => $data]); |
|
152
|
|
|
|
|
153
|
|
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Sets the payload code as Success |
|
158
|
|
|
* |
|
159
|
|
|
* @param null|string|array $content The content |
|
160
|
|
|
* |
|
161
|
|
|
* @return Response |
|
162
|
|
|
*/ |
|
163
|
|
|
public function setPayloadSuccess($content = []): Response |
|
164
|
|
|
{ |
|
165
|
|
|
$data = (true === is_array($content)) ? $content : ['data' => $content]; |
|
166
|
|
|
$this->setJsonContent($data); |
|
167
|
|
|
|
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Send raw content without additional tags |
|
173
|
|
|
* |
|
174
|
|
|
* @return ResponseInterface |
|
175
|
|
|
*/ |
|
176
|
|
|
public function sendRaw(): ResponseInterface |
|
177
|
|
|
{ |
|
178
|
|
|
return parent::send(); |
|
179
|
|
|
} |
|
180
|
|
|
} |