response.php 1.66 KB
<?php
class Flora_Api_Response
{
    protected $data;

    public function __construct($response, $api, $apiparams, $throwOnUpdateVersion = true, $debug = false, $url)
    {
        $this->data = json_decode($response);
        $this->url = $url;

        if (!$this->data) {
            throw new Flora_Api_Exception("Invalid JSON data:" . $response, json_last_error());
        }

        if ($throwOnUpdateVersion && $this->isClientAdapterRequired()) {
            $system = $this->getSystem();
            throw new Flora_Api_Exception("Please update your flora client adapter. Please check url " .
                $system->getApiVersionUrl(), Flora_Api_Exception::API_NEED_UPDATE_CLIENT);
        }

        if ($this->data->error->message) {
            if ($debug) {
                throw new Flora_Api_Exception($this->data->error->message . ' at ' . $api .
                    ' with params: ' . http_build_query($apiparams), (int)$this->data->error->code);
            } else {
                throw new Flora_Api_Exception($this->data->error->message, (int)$this->data->error->code);
            }
        }
    }

    public function isClientAdapterRequired()
    {
        $system = $this->getSystem();

        return $system->getApiVersion() != Flora_Api::API_VERSION;
    }

    public function getSystem()
    {
        return new Flora_Api_Response_System($this);
    }

    public function __toString()
    {
        return "<pre>" . print_r($this->data, 1) . "</pre>";
    }

    public function getResponseObject()
    {
        return $this->data;
    }

    /**
     * @return mixed
     */
    public function getResult()
    {
        return $this->data->result;
    }
}