142 lines
3.5 KiB
PHP
142 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Core;
|
|
|
|
class Response
|
|
{
|
|
private string $content = '';
|
|
private int $statusCode = 200;
|
|
private array $headers = [];
|
|
private string $contentType = 'text/html; charset=utf-8';
|
|
|
|
public function setContent(string $content): self
|
|
{
|
|
$this->content = $content;
|
|
return $this;
|
|
}
|
|
|
|
public function getContent(): string
|
|
{
|
|
return $this->content;
|
|
}
|
|
|
|
public function setStatusCode(int $statusCode): self
|
|
{
|
|
$this->statusCode = $statusCode;
|
|
return $this;
|
|
}
|
|
|
|
public function getStatusCode(): int
|
|
{
|
|
return $this->statusCode;
|
|
}
|
|
|
|
public function setHeader(string $name, string $value): self
|
|
{
|
|
$this->headers[$name] = $value;
|
|
return $this;
|
|
}
|
|
|
|
public function setHeaders(array $headers): self
|
|
{
|
|
$this->headers = array_merge($this->headers, $headers);
|
|
return $this;
|
|
}
|
|
|
|
public function getHeaders(): array
|
|
{
|
|
return $this->headers;
|
|
}
|
|
|
|
public function setContentType(string $contentType): self
|
|
{
|
|
$this->contentType = $contentType;
|
|
return $this;
|
|
}
|
|
|
|
public function getContentType(): string
|
|
{
|
|
return $this->contentType;
|
|
}
|
|
|
|
public function redirect(string $url, int $statusCode = 302): self
|
|
{
|
|
$this->setHeader('Location', $url);
|
|
$this->setStatusCode($statusCode);
|
|
return $this;
|
|
}
|
|
|
|
public function json(array $data, int $statusCode = 200): self
|
|
{
|
|
$this->setContentType('application/json');
|
|
$this->setContent(json_encode($data, JSON_UNESCAPED_UNICODE));
|
|
$this->setStatusCode($statusCode);
|
|
return $this;
|
|
}
|
|
|
|
public function download(string $filePath, string $filename = null): self
|
|
{
|
|
if (!file_exists($filePath)) {
|
|
throw new \Exception('File not found: ' . $filePath);
|
|
}
|
|
|
|
$filename = $filename ?: basename($filePath);
|
|
$mimeType = mime_content_type($filePath) ?: 'application/octet-stream';
|
|
|
|
$this->setHeader('Content-Type', $mimeType);
|
|
$this->setHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
|
|
$this->setHeader('Content-Length', (string) filesize($filePath));
|
|
$this->setHeader('Cache-Control', 'no-cache, must-revalidate');
|
|
$this->setHeader('Pragma', 'no-cache');
|
|
|
|
$this->setContent(file_get_contents($filePath));
|
|
return $this;
|
|
}
|
|
|
|
public function send(): void
|
|
{
|
|
// Set status code
|
|
http_response_code($this->statusCode);
|
|
|
|
// Set content type
|
|
header('Content-Type: ' . $this->contentType);
|
|
|
|
// Set additional headers
|
|
foreach ($this->headers as $name => $value) {
|
|
header($name . ': ' . $value);
|
|
}
|
|
|
|
// Output content
|
|
echo $this->content;
|
|
exit;
|
|
}
|
|
|
|
public function notFound(string $message = 'Seite nicht gefunden'): self
|
|
{
|
|
$this->setStatusCode(404);
|
|
$this->setContent($message);
|
|
return $this;
|
|
}
|
|
|
|
public function forbidden(string $message = 'Zugriff verweigert'): self
|
|
{
|
|
$this->setStatusCode(403);
|
|
$this->setContent($message);
|
|
return $this;
|
|
}
|
|
|
|
public function unauthorized(string $message = 'Nicht autorisiert'): self
|
|
{
|
|
$this->setStatusCode(401);
|
|
$this->setContent($message);
|
|
return $this;
|
|
}
|
|
|
|
public function serverError(string $message = 'Interner Serverfehler'): self
|
|
{
|
|
$this->setStatusCode(500);
|
|
$this->setContent($message);
|
|
return $this;
|
|
}
|
|
}
|