<?php
namespace App\Core;

class Request {
    public function getMethod() {
        return $_SERVER['REQUEST_METHOD'];
    }

    public function isPost() {
        return $this->getMethod() === 'POST';
    }

    public function getQueryParams() {
        return $_GET;
    }

    public function getBody() {
        $body = [];
        if ($this->isPost()) {
            foreach ($_POST as $key => $val) {
                if (is_array($val)) {
                    $body[$key] = filter_var_array($val, FILTER_SANITIZE_SPECIAL_CHARS);
                } else {
                    $body[$key] = filter_input(INPUT_POST, $key, FILTER_SANITIZE_SPECIAL_CHARS);
                }
            }
        }
        return $body;
    }

    public function getJsonBody() {
        $json = file_get_contents('php://input');
        return json_decode($json, true) ?? [];
    }
}
