abschluss
This commit is contained in:
106
app/Http/Request.php
Normal file
106
app/Http/Request.php
Normal file
@ -0,0 +1,106 @@
|
||||
<?php
|
||||
namespace Blog\Http;
|
||||
|
||||
/**
|
||||
* Stellt eine HTTP-Request-Objektklasse bereit.
|
||||
*
|
||||
* Kapselt HTTP-Methoden, Pfad, POST-Daten, Header und Rohdatenzugriff.
|
||||
*/
|
||||
class Request {
|
||||
private string $method;
|
||||
private string $path;
|
||||
private array $postData;
|
||||
|
||||
/**
|
||||
* Konstruktor für das Request-Objekt.
|
||||
*
|
||||
* @param string $method HTTP-Methode (z.B. GET, POST)
|
||||
* @param string $uri URI der Anfrage
|
||||
* @param array $postData Optional: POST-Daten (Standard: $_POST)
|
||||
*/
|
||||
public function __construct(string $method, string $uri, array $postData = []) {
|
||||
$this->method = strtoupper($method);
|
||||
$this->path = parse_url($uri, PHP_URL_PATH) ?? '/';
|
||||
$this->postData = $postData ?: $_POST;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt die HTTP-Methode zurück.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getMethod(): string {
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt den Pfad der Anfrage zurück.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getPath(): string {
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt einen POST-Wert anhand des Schlüssels zurück.
|
||||
*
|
||||
* @param string $key Schlüssel im POST-Array
|
||||
* @param mixed $default Optionaler Standardwert
|
||||
* @return mixed
|
||||
*/
|
||||
public function getPost(string $key, $default = null): mixed {
|
||||
return $this->postData[$key] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt alle POST-Daten als Array zurück.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function allPost(): array {
|
||||
return $this->postData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt einen Query-Parameter anhand des Schlüssels zurück.
|
||||
*
|
||||
* @param string $key Schlüssel im Query-String
|
||||
* @param mixed $default Optionaler Standardwert
|
||||
* @return mixed
|
||||
*/
|
||||
public function getQuery(string $key, $default = null): mixed {
|
||||
$query = [];
|
||||
parse_str(parse_url($this->path, PHP_URL_QUERY) ?? '', $query);
|
||||
return $query[$key] ?? $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt einen HTTP-Header zurück.
|
||||
*
|
||||
* @param string $key Header-Name (z.B. 'Content-Type')
|
||||
* @return string|null
|
||||
*/
|
||||
public function getHeader(string $key): ?string {
|
||||
$headerKey = 'HTTP_' . strtoupper(str_replace('-', '_', $key));
|
||||
return $_SERVER[$headerKey] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt den rohen Anfrage-Body zurück.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getRawInput(): string {
|
||||
return file_get_contents('php://input');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt den Anfrage-Body als assoziatives Array zurück (JSON).
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getJson(): array {
|
||||
return json_decode($this->getRawInput(), true) ?? [];
|
||||
}
|
||||
}
|
95
app/Http/Response.php
Normal file
95
app/Http/Response.php
Normal file
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
namespace Blog\Http;
|
||||
|
||||
/**
|
||||
* Stellt eine HTTP-Response-Objektklasse bereit.
|
||||
*
|
||||
* Kapselt Statuscode, Header, Body und Methoden zum Senden von Antworten.
|
||||
*/
|
||||
class Response {
|
||||
private int $status = 200;
|
||||
private array $headers = [];
|
||||
private string $body = "";
|
||||
|
||||
/**
|
||||
* Setzt den HTTP-Statuscode.
|
||||
*
|
||||
* @param int $status HTTP-Statuscode
|
||||
* @return self
|
||||
*/
|
||||
public function setStatus(int $status): self {
|
||||
$this->status = $status;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt einen HTTP-Header hinzu.
|
||||
*
|
||||
* @param string $key Header-Name
|
||||
* @param string $val Header-Wert
|
||||
* @return self
|
||||
*/
|
||||
public function addHeader(string $key, string $val): self {
|
||||
$this->headers[$key] = $val;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt das Response-Objekt zurück (für Method Chaining).
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function getBody(): self {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fügt dem Body Inhalt hinzu.
|
||||
*
|
||||
* @param string $content Inhalt, der angehängt wird
|
||||
* @return self
|
||||
*/
|
||||
public function write(string $content): self {
|
||||
$this->body .= $content;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sendet die HTTP-Antwort an den Client.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function send(): void {
|
||||
http_response_code($this->status);
|
||||
foreach($this->headers as $key => $val)
|
||||
header("{$key}: {$val}");
|
||||
echo $this->body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sendet eine JSON-Antwort.
|
||||
*
|
||||
* @param array $data Zu sendende Daten
|
||||
* @param int $status Optionaler HTTP-Statuscode (Standard: 200)
|
||||
* @return self
|
||||
*/
|
||||
public function json(array $data, int $status = 200): self {
|
||||
$this->setStatus($status);
|
||||
header("Content-Type: application/json");
|
||||
$this->body = json_encode($data);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Führt eine HTTP-Weiterleitung durch.
|
||||
*
|
||||
* @param string $url Ziel-URL
|
||||
* @param int $status Optionaler Statuscode (Standard: 302)
|
||||
* @return void
|
||||
*/
|
||||
public function redirect(string $url, int $status = 302): void {
|
||||
http_response_code($status);
|
||||
header("Location: {$url}");
|
||||
exit;
|
||||
}
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
<?php
|
||||
namespace Blog\Http;
|
||||
|
||||
class Request {
|
||||
private string $method;
|
||||
private string $path;
|
||||
private array $postData;
|
||||
|
||||
public function __construct(string $method, string $uri, array $postData = []) {
|
||||
$this->method = strtoupper($method);
|
||||
$this->path = parse_url($uri, PHP_URL_PATH) ?? '/';
|
||||
$this->postData = $postData ?: $_POST;
|
||||
}
|
||||
|
||||
public function getMethod(): string {
|
||||
return $this->method;
|
||||
}
|
||||
|
||||
public function getPath(): string {
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
public function getPost(string $key, $default = null): mixed {
|
||||
return $this->postData[$key] ?? $default;
|
||||
}
|
||||
|
||||
public function allPost(): array {
|
||||
return $this->postData;
|
||||
}
|
||||
|
||||
public function getQuery(string $key, $default = null): mixed {
|
||||
$query = [];
|
||||
parse_str(parse_url($this->path, PHP_URL_QUERY) ?? '', $query);
|
||||
return $query[$key] ?? $default;
|
||||
}
|
||||
|
||||
public function getHeader(string $key): ?string {
|
||||
$headerKey = 'HTTP_' . strtoupper(str_replace('-', '_', $key));
|
||||
return $_SERVER[$headerKey] ?? null;
|
||||
}
|
||||
|
||||
public function getRawInput(): string {
|
||||
return file_get_contents('php://input');
|
||||
}
|
||||
|
||||
public function getJson(): array {
|
||||
return json_decode($this->getRawInput(), true) ?? [];
|
||||
}
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user