62 lines
1.3 KiB
PHP
62 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace MyCodebox\MariaCore\Http;
|
|
|
|
class Response
|
|
{
|
|
protected int $statusCode = 200;
|
|
protected array $headers = [];
|
|
protected string $body = '';
|
|
|
|
public function __construct(string $body = '', int $statusCode = 200, array $headers = [])
|
|
{
|
|
$this->body = $body;
|
|
$this->statusCode = $statusCode;
|
|
$this->headers = $headers;
|
|
}
|
|
|
|
public function getStatusCode(): int
|
|
{
|
|
return $this->statusCode;
|
|
}
|
|
|
|
public function withStatus(int $code): self
|
|
{
|
|
$clone = clone $this;
|
|
$clone->statusCode = $code;
|
|
return $clone;
|
|
}
|
|
|
|
public function getHeaders(): array
|
|
{
|
|
return $this->headers;
|
|
}
|
|
|
|
public function withHeader(string $name, string $value): self
|
|
{
|
|
$clone = clone $this;
|
|
$clone->headers[$name] = $value;
|
|
return $clone;
|
|
}
|
|
|
|
public function getBody(): string
|
|
{
|
|
return $this->body;
|
|
}
|
|
|
|
public function withBody(string $body): self
|
|
{
|
|
$clone = clone $this;
|
|
$clone->body = $body;
|
|
return $clone;
|
|
}
|
|
|
|
public function send(): void
|
|
{
|
|
http_response_code($this->statusCode);
|
|
foreach ($this->headers as $name => $value) {
|
|
header("$name: $value");
|
|
}
|
|
echo $this->body;
|
|
}
|
|
}
|