Response.php
Pfad: src/Infrastructure/Http/Response.php
Ext: php
Größe: 1443 Bytes
Geändert: 2026-07-15 21:48:59+02
<?php
declare(strict_types=1);
namespace Demo\Infrastructure\Http;
/**
* Unveraenderliche HTTP-Antwort: Status, Content-Type, Rumpf und Zusatz-Header.
*/
final class Response
{
/**
* @param int $status HTTP-Statuscode.
* @param string $contentType Content-Type-Header.
* @param string $body Antwort-Rumpf.
* @param array<string,string> $headers Weitere Header (z.B. Location).
*/
public function __construct(
public readonly int $status,
public readonly string $contentType,
public readonly string $body,
public readonly array $headers = []
) {
}
/**
* Erzeugt eine Weiterleitung (Post/Redirect/Get).
*
* @param string $location Ziel-URL.
* @param int $status Redirect-Status (Default 303).
* @return self Redirect-Antwort.
*/
public static function redirect(string $location, int $status = 303): self
{
return new self($status, 'text/plain; charset=UTF-8', '', ['Location' => $location]);
}
/**
* Sendet Status, Header und Rumpf an den Client.
*/
public function send(): void
{
http_response_code($this->status);
header('Content-Type: ' . $this->contentType);
foreach ($this->headers as $name => $value) {
header($name . ': ' . $value);
}
echo $this->body;
}
}