<commit_before><?php

namespace evaisse\SimpleHttpBundle\Http;


use evaisse\SimpleHttpBundle\Http\Error\InvalidResponseError;
use evaisse\SimpleHttpBundle\Http\Exception\InvalidJsonResponseException;
use evaisse\SimpleHttpBundle\Http\Exception\ServiceErrorException;

class Response extends \Symfony\Component\HttpFoundation\Response
{
 
    /**
     * Service response result var
     * @var mixed
     */
    protected $result;


    /**
     * [$error description]
     * @var Error
     */
    protected $error;

    /**
     * Raw transfer infos (for cURL)
     * @var array
     */
    protected $transferInfos = array();

    /**
     * [__construct description]
     * @param string  $content [description]
     * @param integer $status  [description]
     * @param array   $headers [description]
     */
    public function __construct($content = '', $status = 200, array $headers = array())
    {
        parent::__construct($content, $status, $headers);
        $this->parseResponse();
    }

    /**
     * Parse response body and extract results
     */
    protected function parseResponse()
    {
        if ($this->headers->get('content-type') === "application/json") {
            $this->result = json_decode($this->getContent(), true);
            if (json_last_error()) {
                $this->error = new InvalidResponseError('Invalid Json response body');
            }
        }



        if (isset($this->result['error'])) {
            $errorMsg = $this->result['error']['message'];
            if (isset($this->result['error']['raw'])) {
                $errorMsg .= " -- " . print_r($this->result['error'], true);
            }
            if ($this->result['error']['code'] > 10000) {
                $this->error = new ServiceAuthenticationException($errorMsg, $this->result['error']['code']);
            } else {
                $this->error = new ServiceErrorException($errorMsg, $this->result['error']['code']);
            }
        }
    }

    /** 
     * 
     */
    public function hasError()
    {
        return !!$this->error;
    }


    /**
     * @return Exception 
     */
    public function getError()
    {
        return $this->error;
    }


    /**
     * Get service response result var
     * @return mixed get service response result var
     */
    public function getResult()
    {
        return $this->result;
    }



    /**
     * Gets the Raw transfer infos (for cURL).
     *
     * @return array
     */
    public function getTransferInfos()
    {
        return $this->transferInfos;
    }

    /**
     * Sets the Raw transfer infos (for cURL).
     *
     * @param array $transferInfos the transfer infos
     */
    public function setTransferInfos(array $transferInfos)
    {
        $this->transferInfos = $transferInfos;<commit_msg>Add response parsing switch token.<commit_after><?php

namespace evaisse\SimpleHttpBundle\Http;


use evaisse\SimpleHttpBundle\Http\Error\InvalidResponseError;
use evaisse\SimpleHttpBundle\Http\Exception\InvalidJsonResponseException;
use evaisse\SimpleHttpBundle\Http\Exception\ServiceErrorException;

class Response extends \Symfony\Component\HttpFoundation\Response
{
 
    /**
     * Service response result var
     * @var mixed
     */
    protected $result;

    /**
     * @var
     */
    protected $hasParsedResult;



    /**
     * [$error description]
     * @var Error
     */
    protected $error;

    /**
     * Raw transfer infos (for cURL)
     * @var array
     */
    protected $transferInfos = array();

    /**
     * [__construct description]
     * @param string  $content [description]
     * @param integer $status  [description]
     * @param array   $headers [description]
     */
    public function __construct($content = '', $status = 200, array $headers = array())
    {
        parent::__construct($content, $status, $headers);
        $this->parseResponse();
    }

    /**
     * Parse response body and extract results
     */
    protected function parseResponse()
    {
        if ($this->headers->get('content-type') === "application/json") {
            $result = json_decode($this->getContent(), true);
            if (json_last_error()) {
                $this->error = new InvalidResponseError('Invalid Json response body');
            }
            $this->setResult($result);
        }
    }

    /** 
     * 
     */
    public function hasError()
    {
        return !!$this->error;
    }


    /**
     * @return Exception 
     */
    public function getError()
    {
        return $this->error;
    }



    /**
     * Get service response result var
     * @return mixed get service response result var
     */
    public function getResult()
    {
        if ($this->hasParsedResult === null) {
            $this->hasParsedResult = false;
            $this->parseResponse();
        }

        if ($this->hasParsedResult) {
            return $this->result;
        } else {
            return $this->content;
        }
    }

    /**
     * @param mixed $result parsed result
     * @return self
     */
    public function setResult($result)
    {
        $this->result = $result;
        $this->hasParsedResult = true;
        return $this;
    }

    /**
     * Gets the Raw transfer infos (for cURL).
     *
     * @return array
     */
    public function getTransferInfos()
    {
        return $this->transferInfos;
    }

    /**
     * Sets the Raw transfer infos (for cURL).
     *
     * @param array $transferInfos the transfer infos
     */
    public function setTransferInfos(array $transferInfos)
    {
        $this->transferInfos = $transferInfos;
{'fraction_non_alphanumeric': 0.13079793851075172, 'fraction_numerical': 0.001954860494046561, 'mean_word_length': 2.0636908002177465, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3345456', 'n_tokens_mistral': 1597, 'n_tokens_neox': 1532, 'n_words': 351}