mirror of
https://github.com/guzzle/guzzle.git
synced 2025-02-24 18:13:00 +01:00
Making Response object immutable. Changing "version" in parsed messages to "protocol_version"
This commit is contained in:
parent
1feae539a2
commit
f592946705
@ -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
|
||||
|
@ -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']
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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)
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user