<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
class CiRequestAdapter
{
    public function __construct(protected Request $request)
    {
    }

    public function getGet(string $key = null, $default = null)
    {
        if ($key === null) {
            return $this->request->query->all();
        }
        return $this->request->query->get($key, $default);
    }

    public function getPost(string $key = null, $default = null)
    {
        if ($key === null) {
            return $this->request->request->all();
        }
        return $this->request->request->get($key, $default);
    }

    public function getJSON(bool $assoc = false)
    {
        $content = $this->request->getContent();
        if ($content === '') {
            return $assoc ? [] : null;
        }
        return json_decode($content, $assoc);
    }

    public function getVar(string $key, $default = null)
    {
        return $this->request->input($key, $default);
    }

    public function getHeader(string $key)
    {
        return $this->request->headers->get($key);
    }

    public function getHeaderLine(string $key)
    {
        return $this->request->header($key);
    }

    public function getFile(string $key)
    {
        return $this->request->file($key);
    }

    public function getRawInput(): string
    {
        return $this->request->getContent();
    }

    public function all(): array
    {
        return array_merge(
            $this->request->query->all(),
            $this->request->request->all(),
            $this->request->json()->all()
        );
    }
}
