Convert response.

@author yansongda <me@yansongda.cn>

@param ResponseInterface $response

@return array|string
505614
public function unwrapResponse(ResponseInterface $response)
    {
        $contentType = $response->getHeaderLine('Content-Type');
        $contents = $response->getBody()->getContents();

        if (false !== stripos($contentType, 'json') || stripos($contentType, 'javascript')) {
            return json_decode($contents, true);
        } elseif (false !== stripos($contentType, 'xml')) {
            return json_decode(json_encode(simplexml_load_string($contents, 'SimpleXMLElement', LIBXML_NOCDATA), JSON_UNESCAPED_UNICODE), true);
        }

        return $contents;
    }
697301
private function decodeResponse(ResponseInterface $response)
    {
        $responseBody = trim((string) $response->getBody());

        if ($response->getHeader('Content-Type')[0] === 'text/plain;charset=UTF-8') {
            $lines = explode("\n", $responseBody);
            $data = [];

            // parse endomondo text response
            foreach ($lines as $line) {
                $items = explode('=', $line);
                if (count($items) === 2) {
                    $data[$items[0]] = $items[1];
                }
            }

            return $data;
        }

        $data = json_decode($responseBody, true);

        if (isset($data['error'])) {
            throw new EndomondoApiOldException('Api error: ' . $data['error']['type']);
        }

        return $data;
    }
372368
protected function extractMetaData(ResponseInterface $response)
    {
        $headers = $this->findMetaHeaders($response);

        if (!count($headers)) {
            return [];
        }

        $metaData = [];

        foreach ($headers as $header) {
            $metaData[$header] = $response->getHeaderLine($header);
        }

        return $metaData;
    }
313299
public function sendHeaders(ResponseInterface $response)
    {
        foreach ($response->getHeaders() as $header => $values) {
            $first = true;
            $header = HeaderHelper::normalizeHeaderName($header);

            foreach ($values as $value) {
                $this->header(sprintf('%s: %s', $header, $value), $first);

                $first = false;
            }
        }

        return $this;
    }
1106160
protected function getRiakContentList(ResponseInterface $response)
    {
        $contentList = [];
        $code        = $response->getStatusCode();

        if ($code == 300) {
            $contentList = $this->getMultipartBodyContent($response);
        }

        if ($code == 200 || $code == 201) {
            $contentList[] = $this->getBodyContent($response);
        }

        return $contentList;
    }
1309990
public function getResponse(MessageInterface $response)
    {
        $result = [];

        if (null !== $response) {
            $responseStr = $this->parser->getBodyWithHeaders($response);

            $result = ['response' => $responseStr];
        }

        return $result;
    }
5300
public function modify(ResponseInterface $response) : ResponseInterface
    {
        /** @var ResponseModifierInterface $modifier */
        foreach ($this->collection as $modifier) {
            $response = $modifier->modify($response);
        }

        return $response;
    }
542236
public function buildData(ResponseInterface $response)
    {
        $data = new \stdClass();
        $data->apiVersion = $response->getApiVersion()->getValue();
        $data->method = $response->getHttpMethod()->getValue();

        $data->params = new \stdClass();
        /** @var Crak\Component\RestNormalizer\ParameterInterface $parameter */
        foreach ($response->getParameters() as $parameter) {
            $values = $parameter->getValues();
            $nbValues = count($values);

            $plainValues = null;
            if ($nbValues === 1) {
                $plainValues = $values->first()->getValue();
            } else if ($nbValues > 1) {
                foreach ($values as $value) {
                    $plainValues[] = $value->getValue();
                }
            }

            $data->params->{$parameter->getId()->getValue()} = $plainValues;
        }

        return $data;
    }
521741
public static function convertToSimpleXml(ResponseInterface $response)
    {
        $body = $response->getBody();
        try {
            return new \SimpleXMLElement((string) $body ?: '<root />');
        } catch (\Exception $e) {
            throw new LinkedInTransferException('Unable to parse response body into XML.');
        }
    }
129694
protected function generateResponse(ResponseInterface $response): array
    {
        $json = $this->validateJson($response);

        $output = [];
        if (!isset($json['LocationList']['StopLocation'])) {
            return $output;
        }

        $stops = self::checkForSingle($json['LocationList']['StopLocation'], 'name');
        foreach ($stops as $stop) {
            $output[] = StopLocationResponse::createFromArray($stop);
        }

        return $output;
    }
975776
public function build(ResponseInterface $response)
    {
        $rawData = json_decode((string)$response->getBody(), true) ?: [];
        $data = ModelHelper::camelize($rawData);
        $data['data'] = ArrayHelper::getValue($rawData, 'data', []);

        if ($auth = ArrayHelper::getValue($data, 'auth')) {
            $data['auth'] = new Auth($auth);
        }

        return new Response($data);
    }
589296
public static function getHeader(ResponseInterface $response, $name)
    {
        $headers = $response->getHeader($name);

        return array_shift($headers);
    }
808342
protected function getBatchResponses(ResponseInterface $response)
    {
        $results = JsonRpc\json_decode((string) $response->getBody(), true);

        return array_map(function (array $result) use ($response) {
            return $this->messageFactory->createResponse(
                $response->getStatusCode(),
                $response->getHeaders(),
                $result
            );
        }, $results);
    }
451639
public static function getResponseHeaders(ResponseInterface $response)
    {
        $responseHeaderArray = $response->getHeaders();

        /** @var string[] $responseHeaders */
        $responseHeaders = [];

        foreach ($responseHeaderArray as $key => $value) {
            $responseHeaders[strtolower($key)] = implode(',', $value);
        }

        return $responseHeaders;
    }
568508
public static function getResponseLine(ResponseInterface $response): string
    {
        return sprintf(
            '%s %d%s',
            self::getProtocol($response),
            self::getStatus($response),
            ($response->getReasonPhrase() ? ' '.$response->getReasonPhrase() : '')
        );
    }
1040605
public static function responseToString(ResponseInterface $response): string
    {
        $reasonPhrase = $response->getReasonPhrase();
        $headers = self::serializeHeaders($response->getHeaders());
        $body = (string) $response->getBody();
        $format = 'HTTP/%s %d%s%s%s';
        if (! empty($headers)) {
            $headers = self::EOL . $headers;
        }
        $headers .= self::EOL . self::EOL;

        return sprintf(
            $format,
            $response->getProtocolVersion(),
            $response->getStatusCode(),
            ($reasonPhrase ? ' ' . $reasonPhrase : ''),
            $headers,
            $body
        );
    }
450257
public static function getResponseBodySummary(ResponseInterface $response)
    {
        if (method_exists(RequestException::class, 'getResponseBodySummary')) {
            return RequestException::getResponseBodySummary($response);
        }

        $body = \GuzzleHttp\Psr7\copy_to_string($response->getBody());

        if (\strlen($body) > 120) {
            return substr($body, 0, 120).' (truncated...)';
        }

        return $body;
    }