first commit
This commit is contained in:
47
app/Http/response.php
Normal file
47
app/Http/response.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace Blog\Http;
|
||||
|
||||
class Response {
|
||||
private int $status = 200;
|
||||
private array $headers = [];
|
||||
private string $body = "";
|
||||
|
||||
public function setStatus(int $status): self {
|
||||
$this->status = $status;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addHeader(string $key, string $val): self {
|
||||
$this->headers[$key] = $val;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getBody(): self {
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function write(string $content): self {
|
||||
$this->body .= $content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function send(): void {
|
||||
http_response_code($this->status);
|
||||
foreach($this->headers as $key => $val)
|
||||
header("{$key}: {$val}");
|
||||
echo $this->body;
|
||||
}
|
||||
|
||||
public function json(array $data, int $status = 200): self {
|
||||
$this->setStatus($status);
|
||||
header("Content-Type: application/json");
|
||||
$this->body = json_encode($data);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function redirect(string $url, int $status = 302): void {
|
||||
http_response_code($status);
|
||||
header("Location: {$url}");
|
||||
exit;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user