MariaCore/src/Http/Response.php

203 lines
4.9 KiB
PHP

<?php
namespace MyCodebox\MariaCore\Http;
/**
* Class Response
*
* Immutable HTTP response object with helpers for common response types.
*/
class Response
{
protected int $statusCode = 200;
protected array $headers = [];
protected string $body = '';
/**
* Response constructor.
*
* @param string $body
* @param int $statusCode
* @param array $headers
*/
public function __construct(string $body = '', int $statusCode = 200, array $headers = [])
{
$this->body = $body;
$this->statusCode = $statusCode;
$this->headers = $headers;
}
/**
* Get the HTTP status code.
*
* @return int
*/
public function getStatusCode(): int
{
return $this->statusCode;
}
/**
* Return a new response with a different status code.
*
* @param int $code
* @return self
*/
public function withStatus(int $code): self
{
$clone = clone $this;
$clone->statusCode = $code;
return $clone;
}
/**
* Get all headers as an associative array.
*
* @return array
*/
public function getHeaders(): array
{
return $this->headers;
}
/**
* Return a new response with an additional header.
*
* @param string $name
* @param string $value
* @return self
*/
public function withHeader(string $name, string $value): self
{
$clone = clone $this;
$clone->headers[$name] = $value;
return $clone;
}
/**
* Get the response body.
*
* @return string
*/
public function getBody(): string
{
return $this->body;
}
/**
* Return a new response with a different body.
*
* @param string $body
* @return self
*/
public function withBody(string $body): self
{
$clone = clone $this;
$clone->body = $body;
return $clone;
}
/**
* Send the response to the client.
*
* @return void
*/
public function send(): void
{
http_response_code($this->statusCode);
foreach ($this->headers as $name => $value) {
header("$name: $value");
}
echo $this->body;
}
/**
* Return a new response with a redirect header.
*
* @param string $location
* @param int $statusCode
* @return self
*/
public function withRedirect(string $location, int $statusCode = 302): self
{
$clone = clone $this;
$clone->statusCode = $statusCode;
$clone->headers['Location'] = $location;
$clone->body = '';
return $clone;
}
/**
* Return a new response with HTML content.
*
* @param string $html
* @param int $statusCode
* @return self
*/
public function withHtml(string $html, int $statusCode = 200): self
{
$clone = clone $this;
$clone->body = $html;
$clone->statusCode = $statusCode;
$clone->headers['Content-Type'] = 'text/html; charset=utf-8';
return $clone;
}
/**
* Return a new response with plain text content.
*
* @param string $text
* @param int $statusCode
* @return self
*/
public function withText(string $text, int $statusCode = 200): self
{
$clone = clone $this;
$clone->body = $text;
$clone->statusCode = $statusCode;
$clone->headers['Content-Type'] = 'text/plain; charset=utf-8';
return $clone;
}
/**
* Return a new response for file download.
*
* @param string $filename
* @param string $content
* @param string $contentType
* @return self
*/
public function withDownload(string $filename, string $content, string $contentType = 'application/octet-stream'): self
{
$clone = clone $this;
$clone->body = $content;
$clone->headers['Content-Type'] = $contentType;
$clone->headers['Content-Disposition'] = 'attachment; filename="' . basename($filename) . '"';
return $clone;
}
/**
* Return a new response with JSON body and appropriate content type.
*
* @param mixed $data Data to encode as JSON
* @param int $statusCode HTTP status code
* @param int $jsonOptions JSON encoding options
* @return self
*/
public function withJson(mixed $data, int $statusCode = 200, int $jsonOptions = 0): self
{
$body = json_encode($data, $jsonOptions | JSON_UNESCAPED_UNICODE);
if ($body === false) {
$body = json_encode([
'error' => 'JSON encoding failed',
'message' => json_last_error_msg(),
]);
$statusCode = 500;
}
$clone = clone $this;
$clone->body = $body;
$clone->statusCode = $statusCode;
$clone->headers['Content-Type'] = 'application/json';
return $clone;
}
}