diff --git a/src/Guzzle/Http/Message/MessageFactory.php b/src/Guzzle/Http/Message/MessageFactory.php index 01236615..5f3ec742 100644 --- a/src/Guzzle/Http/Message/MessageFactory.php +++ b/src/Guzzle/Http/Message/MessageFactory.php @@ -51,13 +51,8 @@ class MessageFactory implements MessageFactoryInterface return new Response($statusCode, $headers, $body, $options); } - public function createRequest( - $method, - $url, - array $headers = [], - $body = null, - array $options = array() - ) { + public function createRequest($method, $url, array $headers = [], $body = null, array $options = []) + { $request = new Request( $method, $url, @@ -94,8 +89,9 @@ class MessageFactory implements MessageFactoryInterface $parsed['method'], Url::buildUrl($parsed['request_url']), $parsed['headers'], - $parsed['body'] - )->setProtocolVersion($parsed['version']); + $parsed['body'], + $parsed + ); // "Expect: 100-Continue" header is added when using a raw request body for PUT or POST requests. // This factory method should accurately reflect the message, so here we are removing the Expect diff --git a/src/Guzzle/Http/Message/MessageParser.php b/src/Guzzle/Http/Message/MessageParser.php index 11114811..9e6dafab 100644 --- a/src/Guzzle/Http/Message/MessageParser.php +++ b/src/Guzzle/Http/Message/MessageParser.php @@ -26,7 +26,7 @@ class MessageParser implements MessageParserInterface $parsed = [ 'method' => strtoupper($parts['start_line'][0]), 'protocol' => $protocol, - 'version' => $version, + 'protocol_version' => $version, 'headers' => $parts['headers'], 'body' => $parts['body'] ]; @@ -45,12 +45,12 @@ class MessageParser implements MessageParserInterface list($protocol, $version) = explode('/', trim($parts['start_line'][0])); return [ - 'protocol' => $protocol, - 'version' => $version, - 'code' => $parts['start_line'][1], - 'reason_phrase' => isset($parts['start_line'][2]) ? $parts['start_line'][2] : '', - 'headers' => $parts['headers'], - 'body' => $parts['body'] + 'protocol' => $protocol, + 'protocol_version' => $version, + 'code' => $parts['start_line'][1], + 'reason_phrase' => isset($parts['start_line'][2]) ? $parts['start_line'][2] : '', + 'headers' => $parts['headers'], + 'body' => $parts['body'] ]; } diff --git a/src/Guzzle/Http/Message/Response.php b/src/Guzzle/Http/Message/Response.php index b8145429..2df29f15 100644 --- a/src/Guzzle/Http/Message/Response.php +++ b/src/Guzzle/Http/Message/Response.php @@ -93,22 +93,11 @@ class Response implements ResponseInterface */ public static function fromMessage($message) { - $parser = new MessageParser(); - if (!($data = $parser->parseResponse($message))) { + if (!($data = (new MessageParser())->parseResponse($message))) { throw new \InvalidArgumentException('Unable to parse response message'); } - $response = new static(); - $response->setStatus($data['code']) - ->setHeaders($data['headers']) - ->setProtocolVersion($data['version']) - ->setStatus($data['code'], $data['reason_phrase']); - - if (strlen($data['body']) > 0) { - $response->setBody($data['body']); - } - - return $response; + return new static($data['code'], $data['headers'], $data['body'], $data); } /** @@ -117,13 +106,20 @@ class Response implements ResponseInterface * @param string|resource|StreamInterface $body The body of the response * @param array $options Response message options * - header_factory: Factory used to create headers + * - reason_phrase: Set a custom reason phrease + * - protocol_version: Set a custom protocol version */ - public function __construct($statusCode = null, array $headers = [], $body = null, array $options = []) + public function __construct($statusCode, array $headers = [], $body = null, array $options = []) { $this->initializeMessage($options); - if ($statusCode) { - $this->setStatus($statusCode); + $this->statusCode = (string) $statusCode; + + if (isset($options['reason_phrase'])) { + $this->reasonPhrase = $options['reason_phrase']; + } elseif (isset(self::$statusTexts[$this->statusCode])) { + $this->reasonPhrase = self::$statusTexts[$this->statusCode]; } + if ($headers) { $this->setHeaders($headers); } @@ -148,19 +144,6 @@ class Response implements ResponseInterface return $this->body; } - public function setStatus($statusCode, $reasonPhrase = '') - { - $this->statusCode = (string) $statusCode; - - if (!$reasonPhrase && isset(self::$statusTexts[$this->statusCode])) { - $this->reasonPhrase = self::$statusTexts[$this->statusCode]; - } else { - $this->reasonPhrase = $reasonPhrase; - } - - return $this; - } - public function getStatusCode() { return $this->statusCode; diff --git a/src/Guzzle/Http/Message/ResponseInterface.php b/src/Guzzle/Http/Message/ResponseInterface.php index 48293839..2b860b74 100644 --- a/src/Guzzle/Http/Message/ResponseInterface.php +++ b/src/Guzzle/Http/Message/ResponseInterface.php @@ -4,16 +4,6 @@ namespace Guzzle\Http\Message; interface ResponseInterface extends MessageInterface { - /** - * Set the response status - * - * @param string $statusCode Response status code to set (e.g., "200") - * @param string $reasonPhrase Response reason phrase (e.g., "OK") - * - * @return self - */ - public function setStatus($statusCode, $reasonPhrase = null); - /** * Get the response status code (e.g. "200", "404", etc) *