X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=vendor%2Fzendframework%2Fzend-diactoros%2Fsrc%2FResponse%2FSerializer.php;fp=vendor%2Fzendframework%2Fzend-diactoros%2Fsrc%2FResponse%2FSerializer.php;h=1d48dc106a79a5abb950ac77920781c67a0a9a1f;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/vendor/zendframework/zend-diactoros/src/Response/Serializer.php b/vendor/zendframework/zend-diactoros/src/Response/Serializer.php new file mode 100644 index 000000000..1d48dc106 --- /dev/null +++ b/vendor/zendframework/zend-diactoros/src/Response/Serializer.php @@ -0,0 +1,110 @@ +write($message); + return static::fromStream($stream); + } + + /** + * Parse a response from a stream. + * + * @param StreamInterface $stream + * @return Response + * @throws InvalidArgumentException when the stream is not readable. + * @throws UnexpectedValueException when errors occur parsing the message. + */ + public static function fromStream(StreamInterface $stream) + { + if (! $stream->isReadable() || ! $stream->isSeekable()) { + throw new InvalidArgumentException('Message stream must be both readable and seekable'); + } + + $stream->rewind(); + + list($version, $status, $reasonPhrase) = self::getStatusLine($stream); + list($headers, $body) = self::splitStream($stream); + + return (new Response($body, $status, $headers)) + ->withProtocolVersion($version) + ->withStatus((int) $status, $reasonPhrase); + } + + /** + * Create a string representation of a response. + * + * @param ResponseInterface $response + * @return string + */ + public static function toString(ResponseInterface $response) + { + $reasonPhrase = $response->getReasonPhrase(); + $headers = self::serializeHeaders($response->getHeaders()); + $body = (string) $response->getBody(); + $format = 'HTTP/%s %d%s%s%s'; + + if (! empty($headers)) { + $headers = "\r\n" . $headers; + } + + $headers .= "\r\n\r\n"; + + return sprintf( + $format, + $response->getProtocolVersion(), + $response->getStatusCode(), + ($reasonPhrase ? ' ' . $reasonPhrase : ''), + $headers, + $body + ); + } + + /** + * Retrieve the status line for the message. + * + * @param StreamInterface $stream + * @return array Array with three elements: 0 => version, 1 => status, 2 => reason + * @throws UnexpectedValueException if line is malformed + */ + private static function getStatusLine(StreamInterface $stream) + { + $line = self::getLine($stream); + + if (! preg_match( + '#^HTTP/(?P[1-9]\d*\.\d) (?P[1-5]\d{2})(\s+(?P.+))?$#', + $line, + $matches + )) { + throw new UnexpectedValueException('No status line detected'); + } + + return [$matches['version'], $matches['status'], isset($matches['reason']) ? $matches['reason'] : '']; + } +}