/
Response.php
122 lines (109 loc) · 2.64 KB
/
Response.php
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
namespace Lucinda\MVC;
use Lucinda\MVC\Response\HttpStatus;
use Lucinda\MVC\Response\Status;
use Lucinda\MVC\Response\View;
/**
* Compiles information about response
*/
class Response
{
private ?Status $status = null;
/**
* @var array<string,string>
*/
private array $headers = array();
private ?string $body = null;
private View $view;
/**
* Constructs an empty response based on content type
*
* @param string $contentType Value of content type header that will be sent in response
* @param string $templateFile Value of view template file that will form the basis of response
*/
public function __construct(string $contentType, string $templateFile)
{
$this->headers["Content-Type"] = $contentType;
$this->view = new View($templateFile);
}
/**
* Sets HTTP response status by its numeric code.
*
* @param HttpStatus $status
*/
public function setStatus(HttpStatus $status): void
{
$this->status = new Status($status);
}
/**
* Gets HTTP response status info.
*
* @return Status
*/
public function getStatus(): Status
{
return $this->status;
}
/**
* Gets a pointer to View
*
* @return View
*/
public function view(): View
{
return $this->view;
}
/**
* Gets or sets response headers will send back to user.
*
* @param string $key
* @param string|null $value
* @return string|array<string,string>|null
*/
public function headers(string $key="", string $value=null): string|array|null
{
if (!$key) {
return $this->headers;
} elseif ($value===null) {
return ($this->headers[$key] ?? null);
} else {
$this->headers[$key] = $value;
return null;
}
}
/**
* Sets response body
*
* @param string $body
*/
public function setBody(string $body): void
{
$this->body = $body;
}
/**
* Gets response body
*
* @return ?string
*/
public function getBody(): ?string
{
return $this->body;
}
/**
* Commits response to client.
*/
public function commit(): void
{
// sends headers
if (!headers_sent()) {
if ($this->status) {
header("HTTP/1.1 ".$this->status->getId()." ".$this->status->getDescription());
}
foreach ($this->headers as $name=>$value) {
header($name.": ".$value);
}
}
// displays body
echo $this->body;
}
}