forked from GravitLauncher/HttpMethodExample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponse.php
More file actions
49 lines (41 loc) · 1.12 KB
/
Response.php
File metadata and controls
49 lines (41 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace Gravita\Http;
class Response {
private $httpCode;
private $errorCode;
private $errorMessage;
public function __construct() {
$this->httpCode = 400;
$this->errorCode = 0;
$this->errorMessage = "Unknown error";
}
public function http($newValue) : Response {
$this->httpCode = $newValue;
return $this;
}
public function code($newValue) : Response {
$this->errorCode = $newValue;
return $this;
}
public function message($newValue) : Response {
$this->errorMessage = $newValue;
return $this;
}
public function error_and_exit() {
Response::json_response_and_exit($this->httpCode, [
"error" => $this->errorMessage,
"code" => $this->errorCode
]);
}
public static function json_response_and_exit($code, $data)
{
http_response_code($code);
header("Content-Type: application/json");
echo json_encode($data);
exit(0);
}
public static function not_found_and_exit() {
http_response_code(404);
exit(0);
}
}