Chris@0: $response->getStatusCode(), Chris@0: 'reason_phrase' => $response->getReasonPhrase(), Chris@0: 'protocol_version' => $response->getProtocolVersion(), Chris@0: 'headers' => $response->getHeaders(), Chris@0: 'body' => (string) $response->getBody(), Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Deserialize a response array to a response instance. Chris@0: * Chris@0: * @param array $serializedResponse Chris@0: * @return Response Chris@0: * @throws UnexpectedValueException when cannot deserialize response Chris@0: */ Chris@0: public static function fromArray(array $serializedResponse) Chris@0: { Chris@0: try { Chris@0: $body = new Stream('php://memory', 'wb+'); Chris@0: $body->write(self::getValueFromKey($serializedResponse, 'body')); Chris@0: Chris@0: $statusCode = self::getValueFromKey($serializedResponse, 'status_code'); Chris@0: $headers = self::getValueFromKey($serializedResponse, 'headers'); Chris@0: $protocolVersion = self::getValueFromKey($serializedResponse, 'protocol_version'); Chris@0: $reasonPhrase = self::getValueFromKey($serializedResponse, 'reason_phrase'); Chris@0: Chris@0: return (new Response($body, $statusCode, $headers)) Chris@0: ->withProtocolVersion($protocolVersion) Chris@0: ->withStatus($statusCode, $reasonPhrase); Chris@0: } catch (\Exception $exception) { Chris@0: throw new UnexpectedValueException('Cannot deserialize response', null, $exception); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param array $data Chris@0: * @param string $key Chris@0: * @param string $message Chris@0: * @return mixed Chris@0: * @throws UnexpectedValueException Chris@0: */ Chris@0: private static function getValueFromKey(array $data, $key, $message = null) Chris@0: { Chris@0: if (isset($data[$key])) { Chris@0: return $data[$key]; Chris@0: } Chris@0: if ($message === null) { Chris@0: $message = sprintf('Missing "%s" key in serialized request', $key); Chris@0: } Chris@0: throw new UnexpectedValueException($message); Chris@0: } Chris@0: }