Handler for validate the response body.

@param string $responseBody Content body of the response

@return string
552154
private function responseHandler($responseBody)
    {
        if ('json' === $this->outputFormat) {
            return $this->validateJsonResponse($responseBody);
        }

        return $this->validateXmlResponse($responseBody);
    }
909833
private function retrieveErrorMessage($body)
    {
        if (empty($body) || false === strpos($body, '<?xml')) {
            return '';
        }
        $xml = simplexml_load_string($body);
        if (isset($xml->Message)) {
            return strval($xml->Message);
        }
        return '';
    }
118475
public static function decodeChunkedBody($body)
    {
        $decBody = '';
        
        while (trim($body)) {
            if (! preg_match("/^([\da-fA-F]+)[^\r\n]*\r\n/sm", $body, $m)) {
                require_once 'Zend/Http/Exception.php';
                throw new Zend_Http_Exception("Error parsing body - doesn't seem to be a chunked message");
            }

            $length = hexdec(trim($m[1]));
            $cut = strlen($m[0]);

            $decBody .= substr($body, $cut, $length);
            $body = substr($body, $cut + $length + 2);
        }

        return $decBody;
    }
794113
public static function normalize($body, $method)
    {
        if ($method === RequestInterface::METHOD_HEAD || empty($body)) {
            return;
        }

        if (is_callable($body)) {
            return call_user_func($body);
        }

        return $body;
    }
862970
public function setBody($responseBody)
	{
		if($this->initialized !== true)
			throw new \Exception('ResponseBuilder not initialized. Call init() first');
		
		// TODO make sure, this can only be a string or a BaseObject
		// TODO throws an error when not valid JSON - how to deal with this?
		if(gettype($responseBody) != 'string')
			$responseBody = $responseBody->getJSONString();
		
		$this->sonicResponse->setBody($responseBody);
		
		return $this;
	}
380089
public function compileHtmlBody(string $textBody): string
    {
        $html = Html::encode($textBody);
        $html = Markdown::process($html);

        return $html;
    }
24150
protected function parseResponse($body)
    {
        $this->location = $body->getElementsByTagName('location')->item(0)->nodeValue;


        if ($body->getElementsByTagName('errNo')->length > 0) {
            $this->errNo = (int)$body->getElementsByTagName('errNo')->item(0)->nodeValue;
        }

        if ($body->getElementsByTagName('errText')->length > 0) {
            $this->errText = $body->getElementsByTagName('errText')->item(0)->nodeValue;
        }
    }
1202557
protected function decodeResponseBody($responseBody)
    {
        // Response body is empty for HTTP 204 and 304 status code.
        if (empty($responseBody)) {
            return array();
        }

        $responseBody = json_decode($responseBody, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new DeserializeException('Unable to deserialize JSON data: ' . json_last_error_msg(), json_last_error());
        }

        return $responseBody;
    }
73283
protected function parseAccessTokenResponse($responseBody)
    {
        $request = \Request::getInstance();
        if ($request->get('error')) {
            $reason = $request->get('error_description');
            throw new TokenResponseException($reason);
        }

        $data = json_decode($responseBody, true);

        if (null === $data || !is_array($data)) {
            throw new TokenResponseException('Unable to parse response.');
        } elseif (isset($data['error'])) {
            throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"');
        }

        $token = new StdOAuth2Token();
        $token->setAccessToken($data['access_token']);
        $token->setLifetime($data['expires_in']);

        if (isset($data['refresh_token'])) {
            $token->setRefreshToken($data['refresh_token']);
            unset($data['refresh_token']);
        }

        unset($data['access_token']);
        unset($data['expires_in']);

        $data['state'] = $request->get('state');

        $token->setExtraParams($data);

        return $token;
    }
478054
private function parse($body)
    {
        $body = json_decode($body, true);

        if (isset($body['response'])) {
            return new Options($body['response']);
        }

        return new Options($body);
    }
452154
private function parse($body)
    {
        $result = array();
        $data = json_decode($body);

        $response = is_array($data->response) ? new \stdClass : $data->response;
        if (null == $response) {
            $response = $data;
        }
        $response->errors = $data->errors;
        $response->test = isset($data->test) ? $data->test : $response->test;

        if ($data->successful == true) {
            if ((isset($response->authorized)
                && $response->authorized == true)
                || (isset($response->id)
                && $response->id !== null)
            ) {
                $response->authorization_id = isset($response->id)
                    ? $response->id
                    : $response->token;
            } else {
                $response->authorization_id = null;
            }

            $response->success = true;
        } else {
            $response->success = false;
            $response->authorization_id = null;
        }

        $response->message = isset($response->message) ? $response->message : $data->errors;

        return (array) $response;

    }
870315
protected function getResponse(array $headers, $body)
	{
		// Create the response object.
		$return = new Response;

		// Set the body for the response.
		$return->body = $body;

		// Get the response code from the first offset of the response headers.
		preg_match('/[0-9]{3}/', array_shift($headers), $matches);
		$code = $matches[0];

		if (is_numeric($code))
		{
			$return->code = (int) $code;
		}

		// No valid response code was detected.
		else
		{
			throw new \UnexpectedValueException('No HTTP response code found.');
		}

		// Add the response headers to the response object.
		foreach ($headers as $header)
		{
			$pos = strpos($header, ':');
			$return->headers[trim(substr($header, 0, $pos))] = trim(substr($header, ($pos + 1)));
		}

		return $return;
	}
141222
private function parse($body)
    {
        $xml = simplexml_load_string($body);

        $data = $xml->xpath('//soap:Body');

        $response = array();

        foreach ($data as $node) {
            $this->parseElement($response, $node);
        }

        return $response;
    }
1307085
public function setBody($body) : Response
    {
        if (is_null($body) || is_scalar($body)) {
            $this->body = (string) $body;

            return $this;
        }

        $prefix = 'Body must be null or scalar, ';
        $suffix = gettype($body) . ' given.';

        throw new \InvalidArgumentException("{$prefix}{$suffix}");
    }
1128672
protected function verifyErrorFormat($body, $httpStatusCode)
    {
        if (is_array($body) === false)
        {
            $this->throwServerError($body, $httpStatusCode);
        }

        if ((isset($body['error']) === false) or
            (isset($body['error']['code']) === false))
        {
            $this->throwServerError($body, $httpStatusCode);
        }

        $code = $body['error']['code'];

        if (Errors\ErrorCode::exists($code) === false)
        {
            $this->throwServerError($body, $httpStatusCode);
        }
    }
1069472
private function handleResponse($response)
    {
        if ($this->client->newFormatModeEnabled === true) {
            return $response;
        }

        return $this->client->handleHttpResponse($response);
    }
773806
protected function getResponseBody($response)
    {
        //Response must be string
        $body = $response;

        if ($response instanceof ResponseInterface) {
            //Fetch the body
            $body = $response->getBody();
        }

        if ($body instanceof StreamInterface) {
            $body = $body->getContents();
        }

        return (string) $body;
    }