<?php
namespace App\Core;

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

    public function getUri() {
        return $_SERVER['REQUEST_URI'];
    }

    public function getHost() {
        return $_SERVER['HTTP_HOST'];
    }

    public function isGet() {
        return $this->getMethod() === 'GET';
    }

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

    public function getQueryParams() {
        return $_GET;
    }

    public function getBody() {
        $body = [];
        if ($this->isPost()) {
            foreach ($_POST as $key => $val) {
                $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) ?? [];
    }
}
