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; + } +}