3 * Zend Framework (http://framework.zend.com/)
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
10 namespace Zend\Diactoros\Response;
12 use InvalidArgumentException;
13 use Psr\Http\Message\ResponseInterface;
14 use Psr\Http\Message\StreamInterface;
15 use UnexpectedValueException;
16 use Zend\Diactoros\AbstractSerializer;
17 use Zend\Diactoros\Response;
18 use Zend\Diactoros\Stream;
20 final class Serializer extends AbstractSerializer
23 * Deserialize a response string to a response instance.
25 * @param string $message
27 * @throws UnexpectedValueException when errors occur parsing the message.
29 public static function fromString($message)
31 $stream = new Stream('php://temp', 'wb+');
32 $stream->write($message);
33 return static::fromStream($stream);
37 * Parse a response from a stream.
39 * @param StreamInterface $stream
41 * @throws InvalidArgumentException when the stream is not readable.
42 * @throws UnexpectedValueException when errors occur parsing the message.
44 public static function fromStream(StreamInterface $stream)
46 if (! $stream->isReadable() || ! $stream->isSeekable()) {
47 throw new InvalidArgumentException('Message stream must be both readable and seekable');
52 list($version, $status, $reasonPhrase) = self::getStatusLine($stream);
53 list($headers, $body) = self::splitStream($stream);
55 return (new Response($body, $status, $headers))
56 ->withProtocolVersion($version)
57 ->withStatus((int) $status, $reasonPhrase);
61 * Create a string representation of a response.
63 * @param ResponseInterface $response
66 public static function toString(ResponseInterface $response)
68 $reasonPhrase = $response->getReasonPhrase();
69 $headers = self::serializeHeaders($response->getHeaders());
70 $body = (string) $response->getBody();
71 $format = 'HTTP/%s %d%s%s%s';
73 if (! empty($headers)) {
74 $headers = "\r\n" . $headers;
77 $headers .= "\r\n\r\n";
81 $response->getProtocolVersion(),
82 $response->getStatusCode(),
83 ($reasonPhrase ? ' ' . $reasonPhrase : ''),
90 * Retrieve the status line for the message.
92 * @param StreamInterface $stream
93 * @return array Array with three elements: 0 => version, 1 => status, 2 => reason
94 * @throws UnexpectedValueException if line is malformed
96 private static function getStatusLine(StreamInterface $stream)
98 $line = self::getLine($stream);
101 '#^HTTP/(?P<version>[1-9]\d*\.\d) (?P<status>[1-5]\d{2})(\s+(?P<reason>.+))?$#',
105 throw new UnexpectedValueException('No status line detected');
108 return [$matches['version'], $matches['status'], isset($matches['reason']) ? $matches['reason'] : ''];