-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponse.php
More file actions
56 lines (47 loc) · 1.05 KB
/
Response.php
File metadata and controls
56 lines (47 loc) · 1.05 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
50
51
52
53
54
55
56
<?php
declare(strict_types=1);
class Response
{
const STATUS_OK = 200;
const STATUS_BAD_REQUEST = 400;
const STATUS_UNAUTHORIZED = 401;
const STATUS_NOT_FOUND = 404;
const STATUS_SERVER_ERROR = 500;
const CONTENT_TYPE_TEXT = 'text/plain';
const CONTENT_TYPE_HTML = 'text/html';
const CONTENT_TYPE_JSON = 'application/json';
private $status_code;
private $content_type;
private $content_data;
public function __construct(int $status_code, string $content_type, $content_data)
{
$this->status_code = $status_code;
$this->content_type = $content_type;
$this->content_data = $content_data;
}
public function output()
{
http_response_code($this->status_code);
header("Content-type: " . $this->content_type);
switch ($this->content_type)
{
case Response::CONTENT_TYPE_JSON:
{
if (is_array($this->content_data))
{
echo json_encode($this->content_data);
}
else
{
echo $this->content_data;
}
}
break;
default:
{
echo $this->content_data;
}
break;
}
}
}