method = $_SERVER['REQUEST_METHOD']; $this->uri = $this->parseUri(); $this->headers = $this->parseHeaders(); $this->body = $this->parseBody(); } private function parseUri(): string { $uri = $_SERVER['REQUEST_URI'] ?? '/'; // Retirer le query string if (($pos = strpos($uri, '?')) !== false) { $uri = substr($uri, 0, $pos); } // Retirer le préfixe /api si présent $uri = preg_replace('#^/api#', '', $uri); return '/' . trim($uri, '/'); } private function parseHeaders(): array { $headers = []; foreach ($_SERVER as $key => $value) { if (str_starts_with($key, 'HTTP_')) { $name = str_replace('_', '-', substr($key, 5)); $headers[$name] = $value; } } return $headers; } private function parseBody(): array { if (in_array($this->method, ['POST', 'PUT', 'PATCH'])) { $input = file_get_contents('php://input'); if (!empty($input)) { $decoded = json_decode($input, true); if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) { return $decoded; } } // Fallback sur $_POST ou array vide return $_POST ?: []; } return []; } public function getMethod(): string { return $this->method; } public function getUri(): string { return $this->uri; } public function getHeader(string $name): ?string { $name = strtoupper(str_replace('-', '_', $name)); return $this->headers[$name] ?? null; } public function getSessionId(): ?string { return $this->getHeader('X-SESSION-ID'); } public function getBody(): array { return $this->body; } public function get(string $key, mixed $default = null): mixed { return $this->body[$key] ?? $_GET[$key] ?? $default; } public function setParams(array $params): void { $this->params = $params; } public function getParam(string $key, mixed $default = null): mixed { return $this->params[$key] ?? $default; } public function getParams(): array { return $this->params; } }