59 lines
1.8 KiB
PHP
59 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Core;
|
|
|
|
final class Request
|
|
{
|
|
private string $method;
|
|
private string $path;
|
|
private array $headers;
|
|
private array $queryParams;
|
|
private array $body;
|
|
private array $files;
|
|
public ?object $user = null; // Populated by AuthMiddleware
|
|
public ?string $tenantId = null; // Populated by TenantMiddleware
|
|
|
|
public function __construct()
|
|
{
|
|
$this->method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
// Read API path from query string: index.php?route=/api/v1/auth/login
|
|
$this->path = $_GET['route'] ?? parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
|
$this->headers = getallheaders();
|
|
$this->queryParams = $_GET;
|
|
$this->files = $_FILES;
|
|
|
|
$contentType = $this->getHeader('Content-Type');
|
|
if ($contentType && str_contains($contentType, 'application/json')) {
|
|
$this->body = json_decode(file_get_contents('php://input'), true) ?? [];
|
|
} else {
|
|
$this->body = $_POST;
|
|
}
|
|
}
|
|
|
|
public function getMethod(): string { return $this->method; }
|
|
public function getPath(): string { return $this->path; }
|
|
public function getHeaders(): array { return $this->headers; }
|
|
public function getQueryParams(): array { return $this->queryParams; }
|
|
public function getBody(): array { return $this->body; }
|
|
public function getFiles(): array { return $this->files; }
|
|
|
|
public function getHeader(string $name): ?string
|
|
{
|
|
$name = strtolower($name);
|
|
foreach ($this->headers as $key => $value) {
|
|
if (strtolower($key) === $name) {
|
|
return $value;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function input(string $key, mixed $default = null): mixed
|
|
{
|
|
return $this->body[$key] ?? $this->queryParams[$key] ?? $default;
|
|
}
|
|
}
|