diff options
Diffstat (limited to 'vendor/sabre/http/lib/Message.php')
-rw-r--r-- | vendor/sabre/http/lib/Message.php | 163 |
1 files changed, 71 insertions, 92 deletions
diff --git a/vendor/sabre/http/lib/Message.php b/vendor/sabre/http/lib/Message.php index 45bd18398..fc34f8d7f 100644 --- a/vendor/sabre/http/lib/Message.php +++ b/vendor/sabre/http/lib/Message.php @@ -1,5 +1,7 @@ <?php +declare(strict_types=1); + namespace Sabre\HTTP; /** @@ -11,26 +13,26 @@ namespace Sabre\HTTP; * @author Evert Pot (http://evertpot.com/) * @license http://sabre.io/license/ Modified BSD License */ -abstract class Message implements MessageInterface { - +abstract class Message implements MessageInterface +{ /** - * Request body + * Request body. * - * This should be a stream resource + * This should be a stream resource, string or a callback writing the body to php://output * - * @var resource + * @var resource|string|callable */ protected $body; /** - * Contains the list of HTTP headers + * Contains the list of HTTP headers. * * @var array */ protected $headers = []; /** - * HTTP message version (1.0 or 1.1) + * HTTP message version (1.0, 1.1 or 2.0). * * @var string */ @@ -44,17 +46,21 @@ abstract class Message implements MessageInterface { * * @return resource */ - function getBodyAsStream() { - + public function getBodyAsStream() + { $body = $this->getBody(); - if (is_string($body) || is_null($body)) { + if (is_callable($this->body)) { + $body = $this->getBodyAsString(); + } + if (is_string($body) || null === $body) { $stream = fopen('php://temp', 'r+'); - fwrite($stream, $body); + fwrite($stream, (string) $body); rewind($stream); + return $stream; } - return $body; + return $body; } /** @@ -62,77 +68,76 @@ abstract class Message implements MessageInterface { * * Note that because the underlying data may be based on a stream, this * method could only work correctly the first time. - * - * @return string */ - function getBodyAsString() { - + public function getBodyAsString(): string + { $body = $this->getBody(); if (is_string($body)) { return $body; } - if (is_null($body)) { + if (null === $body) { return ''; } + if (is_callable($body)) { + ob_start(); + $body(); + + return ob_get_clean(); + } + /** + * @var string|int|null + */ $contentLength = $this->getHeader('Content-Length'); if (is_int($contentLength) || ctype_digit($contentLength)) { - return stream_get_contents($body, $contentLength); - } else { - return stream_get_contents($body); + return stream_get_contents($body, (int) $contentLength); } + + return stream_get_contents($body); } /** * Returns the message body, as it's internal representation. * - * This could be either a string or a stream. + * This could be either a string, a stream or a callback writing the body to php://output. * - * @return resource|string + * @return resource|string|callable */ - function getBody() { - + public function getBody() + { return $this->body; - } /** - * Replaces the body resource with a new stream or string. + * Replaces the body resource with a new stream, string or a callback writing the body to php://output. * - * @param resource|string $body + * @param resource|string|callable $body */ - function setBody($body) { - + public function setBody($body) + { $this->body = $body; - } /** * Returns all the HTTP headers as an array. * * Every header is returned as an array, with one or more values. - * - * @return array */ - function getHeaders() { - + public function getHeaders(): array + { $result = []; foreach ($this->headers as $headerInfo) { $result[$headerInfo[0]] = $headerInfo[1]; } - return $result; + return $result; } /** * Will return true or false, depending on if a HTTP header exists. - * - * @param string $name - * @return bool */ - function hasHeader($name) { - + public function hasHeader(string $name): bool + { return isset($this->headers[strtolower($name)]); - } /** @@ -148,18 +153,17 @@ abstract class Message implements MessageInterface { * `Set-Cookie` cannot be logically combined with a comma. In those cases * you *should* use getHeaderAsArray(). * - * @param string $name * @return string|null */ - function getHeader($name) { - + public function getHeader(string $name) + { $name = strtolower($name); if (isset($this->headers[$name])) { return implode(',', $this->headers[$name][1]); } - return null; + return null; } /** @@ -170,11 +174,10 @@ abstract class Message implements MessageInterface { * * If the header did not exists, this method will return an empty array. * - * @param string $name * @return string[] */ - function getHeaderAsArray($name) { - + public function getHeaderAsArray(string $name): array + { $name = strtolower($name); if (isset($this->headers[$name])) { @@ -182,7 +185,6 @@ abstract class Message implements MessageInterface { } return []; - } /** @@ -192,14 +194,11 @@ abstract class Message implements MessageInterface { * * If the header already existed, it will be overwritten. * - * @param string $name * @param string|string[] $value - * @return void */ - function setHeader($name, $value) { - - $this->headers[strtolower($name)] = [$name, (array)$value]; - + public function setHeader(string $name, $value) + { + $this->headers[strtolower($name)] = [$name, (array) $value]; } /** @@ -209,16 +208,12 @@ abstract class Message implements MessageInterface { * should be specified as either a string or an array. * * Any header that already existed will be overwritten. - * - * @param array $headers - * @return void */ - function setHeaders(array $headers) { - + public function setHeaders(array $headers) + { foreach ($headers as $name => $value) { $this->setHeader($name, $value); } - } /** @@ -228,77 +223,62 @@ abstract class Message implements MessageInterface { * another value. Individual values can be retrieved with * getHeadersAsArray. * - * @param string $name - * @param string $value - * @return void + * @param string|string[] $value */ - function addHeader($name, $value) { - + public function addHeader(string $name, $value) + { $lName = strtolower($name); if (isset($this->headers[$lName])) { $this->headers[$lName][1] = array_merge( $this->headers[$lName][1], - (array)$value + (array) $value ); } else { $this->headers[$lName] = [ $name, - (array)$value + (array) $value, ]; } - } /** * Adds a new set of HTTP headers. * * Any existing headers will not be overwritten. - * - * @param array $headers - * @return void */ - function addHeaders(array $headers) { - + public function addHeaders(array $headers) + { foreach ($headers as $name => $value) { $this->addHeader($name, $value); } - } - /** * Removes a HTTP header. * * The specified header name must be treated as case-insensitive. * This method should return true if the header was successfully deleted, * and false if the header did not exist. - * - * @param string $name - * @return bool */ - function removeHeader($name) { - + public function removeHeader(string $name): bool + { $name = strtolower($name); if (!isset($this->headers[$name])) { return false; } unset($this->headers[$name]); - return true; + return true; } /** * Sets the HTTP version. * - * Should be 1.0 or 1.1. - * - * @param string $version - * @return void + * Should be 1.0, 1.1 or 2.0. */ - function setHttpVersion($version) { - + public function setHttpVersion(string $version) + { $this->httpVersion = $version; - } /** @@ -306,9 +286,8 @@ abstract class Message implements MessageInterface { * * @return string */ - function getHttpVersion() { - + public function getHttpVersion(): string + { return $this->httpVersion; - } } |