<?php
namespace boru\ai\Http;

class HttpRequest
{
    /** @var string */
    protected $method = 'GET';

    /** @var string */
    protected $url;

    /** @var array */
    protected $headers = array();

    /** @var array|null */
    protected $json;

    /** @var string|null */
    protected $body;

    /** @var array|null */
    protected $multipart;

    /** @var array */
    protected $options = array();

    /** @var callable|null */
    protected $streamCallback;

    public function method($value = null)
    {
        if ($value !== null) {
            $this->method = strtoupper((string)$value);
            return $this;
        }
        return $this->method;
    }

    public function url($value = null)
    {
        if ($value !== null) {
            $this->url = (string)$value;
            return $this;
        }
        return $this->url;
    }

    public function headers(array $value = null)
    {
        if ($value !== null) {
            $this->headers = $value;
            return $this;
        }
        return $this->headers;
    }

    public function header($name, $value = null)
    {
        if ($value !== null) {
            $this->headers[$name] = $value;
            return $this;
        }
        return isset($this->headers[$name]) ? $this->headers[$name] : null;
    }

    public function json(array $value = null)
    {
        if ($value !== null) {
            $this->json = $value;
            return $this;
        }
        return $this->json;
    }

    public function body($value = null)
    {
        if ($value !== null) {
            $this->body = (string)$value;
            return $this;
        }
        return $this->body;
    }

    public function multipart(array $value = null)
    {
        if ($value !== null) {
            $this->multipart = $value;
            return $this;
        }
        return $this->multipart;
    }

    public function option($key, $value = null)
    {
        if (func_num_args() > 1) {
            $this->options[$key] = $value;
            return $this;
        }
        return isset($this->options[$key]) ? $this->options[$key] : null;
    }

    public function options(array $value = null)
    {
        if ($value !== null) {
            $this->options = $value;
            return $this;
        }
        return $this->options;
    }

    public function streamCallback($value = null)
    {
        if ($value !== null) {
            if (!is_callable($value)) {
                throw new \InvalidArgumentException('Stream callback must be callable');
            }
            $this->streamCallback = $value;
            return $this;
        }
        return $this->streamCallback;
    }
}
