3 * @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4 * @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com)
5 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8 namespace Zend\Diactoros\Response;
10 use InvalidArgumentException;
11 use Psr\Http\Message\ResponseInterface;
12 use Psr\Http\Message\StreamInterface;
13 use UnexpectedValueException;
14 use Zend\Diactoros\AbstractSerializer;
15 use Zend\Diactoros\Response;
16 use Zend\Diactoros\Stream;
18 use function preg_match;
21 final class Serializer extends AbstractSerializer
24 * Deserialize a response string to a response instance.
26 * @param string $message
28 * @throws UnexpectedValueException when errors occur parsing the message.
30 public static function fromString($message)
32 $stream = new Stream('php://temp', 'wb+');
33 $stream->write($message);
34 return static::fromStream($stream);
38 * Parse a response from a stream.
40 * @param StreamInterface $stream
42 * @throws InvalidArgumentException when the stream is not readable.
43 * @throws UnexpectedValueException when errors occur parsing the message.
45 public static function fromStream(StreamInterface $stream)
47 if (! $stream->isReadable() || ! $stream->isSeekable()) {
48 throw new InvalidArgumentException('Message stream must be both readable and seekable');
53 list($version, $status, $reasonPhrase) = self::getStatusLine($stream);
54 list($headers, $body) = self::splitStream($stream);
56 return (new Response($body, $status, $headers))
57 ->withProtocolVersion($version)
58 ->withStatus((int) $status, $reasonPhrase);
62 * Create a string representation of a response.
64 * @param ResponseInterface $response
67 public static function toString(ResponseInterface $response)
69 $reasonPhrase = $response->getReasonPhrase();
70 $headers = self::serializeHeaders($response->getHeaders());
71 $body = (string) $response->getBody();
72 $format = 'HTTP/%s %d%s%s%s';
74 if (! empty($headers)) {
75 $headers = "\r\n" . $headers;
78 $headers .= "\r\n\r\n";
82 $response->getProtocolVersion(),
83 $response->getStatusCode(),
84 ($reasonPhrase ? ' ' . $reasonPhrase : ''),
91 * Retrieve the status line for the message.
93 * @param StreamInterface $stream
94 * @return array Array with three elements: 0 => version, 1 => status, 2 => reason
95 * @throws UnexpectedValueException if line is malformed
97 private static function getStatusLine(StreamInterface $stream)
99 $line = self::getLine($stream);
102 '#^HTTP/(?P<version>[1-9]\d*\.\d) (?P<status>[1-5]\d{2})(\s+(?P<reason>.+))?$#',
106 throw new UnexpectedValueException('No status line detected');
109 return [$matches['version'], $matches['status'], isset($matches['reason']) ? $matches['reason'] : ''];