<?php
namespace boru\ai\Http;

class HttpResponse
{
    /** @var int */
    protected $statusCode = 0;

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

    /** @var string */
    protected $body = '';

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

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

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

    public function json()
    {
        $decoded = json_decode($this->body(), true);
        return is_array($decoded) ? $decoded : null;
    }

    public function isSuccess()
    {
        $code = $this->statusCode();
        return $code >= 200 && $code < 300;
    }
}
