<?php
declare(strict_types=1);
namespace Helpers;

final class Request
{
    public static function method(): string { return strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET'); }

    public static function path(): string
    {
        $uri = parse_url($_SERVER['REQUEST_URI'] ?? '/', PHP_URL_PATH) ?: '/';
        return '/' . trim($uri, '/');
    }

    public static function input(): array
    {
        $raw = file_get_contents('php://input') ?: '';
        $json = json_decode($raw, true);
        if (is_array($json)) return self::sanitize($json);
        return self::sanitize($_POST);
    }

    public static function query(): array { return self::sanitize($_GET); }

    private static function sanitize(array $data): array
    {
        foreach ($data as $key => $value) {
            if (is_array($value)) $data[$key] = self::sanitize($value);
            elseif (is_string($value)) $data[$key] = trim($value);
        }
        return $data;
    }
}
