From 9a729f1609cbe217914115b870fc1f7ea1f96e14 Mon Sep 17 00:00:00 2001 From: DualFroz Date: Sun, 23 Aug 2026 13:43:38 +0200 Subject: [PATCH] feat(core): add HTTP request, response and route match value objects --- src/Core/Request.php | 79 +++++++++++++++++++++++++++++++++++++++++ src/Core/Response.php | 60 +++++++++++++++++++++++++++++++ src/Core/RouteMatch.php | 19 ++++++++++ 3 files changed, 158 insertions(+) create mode 100644 src/Core/Request.php create mode 100644 src/Core/Response.php create mode 100644 src/Core/RouteMatch.php diff --git a/src/Core/Request.php b/src/Core/Request.php new file mode 100644 index 0000000..7ea8798 --- /dev/null +++ b/src/Core/Request.php @@ -0,0 +1,79 @@ +method; + } + + public function path(): string + { + return $this->path; + } + + public function isPost(): bool + { + return $this->method === 'POST'; + } + + public function query(string $key, mixed $default = null): mixed + { + return $this->query[$key] ?? $default; + } + + public function input(string $key, mixed $default = null): mixed + { + return $this->body[$key] ?? $default; + } + + public function integer(string $key, int $default = 0): int + { + $value = $this->body[$key] ?? $this->query[$key] ?? $default; + return is_numeric($value) ? (int) $value : $default; + } + + public function body(): array + { + return $this->body; + } + + public function header(string $name): ?string + { + $key = 'HTTP_' . strtoupper(str_replace('-', '_', $name)); + return $this->server[$key] ?? null; + } + + public function rawBody(): string + { + return file_get_contents('php://input') ?: ''; + } +} diff --git a/src/Core/Response.php b/src/Core/Response.php new file mode 100644 index 0000000..6631a87 --- /dev/null +++ b/src/Core/Response.php @@ -0,0 +1,60 @@ +headers['Content-Type'] = 'text/html; charset=UTF-8'; + return $response; + } + + public static function json(array $payload, int $status = 200): Response + { + $response = new self(json_encode($payload, JSON_UNESCAPED_SLASHES), $status); + $response->headers['Content-Type'] = 'application/json'; + return $response; + } + + public static function redirect(string $location, int $status = 302): Response + { + $response = new self('', $status); + $response->headers['Location'] = $location; + return $response; + } + + public function withHeader(string $name, string $value): Response + { + $this->headers[$name] = $value; + return $this; + } + + public function status(): int + { + return $this->status; + } + + public function content(): string + { + return $this->content; + } + + public function send(): void + { + http_response_code($this->status); + foreach ($this->headers as $name => $value) { + header($name . ': ' . $value); + } + echo $this->content; + } +} diff --git a/src/Core/RouteMatch.php b/src/Core/RouteMatch.php new file mode 100644 index 0000000..ded00df --- /dev/null +++ b/src/Core/RouteMatch.php @@ -0,0 +1,19 @@ +status === 200 && $this->handler !== null; + } +}