48 lines
1.0 KiB
PHP
48 lines
1.0 KiB
PHP
<?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;
|
|
}
|
|
}
|