Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Zend Framework (http://framework.zend.com/)
|
Chris@0
|
4 *
|
Chris@0
|
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
|
Chris@0
|
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
|
Chris@0
|
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
|
Chris@0
|
8 */
|
Chris@0
|
9
|
Chris@0
|
10 namespace Zend\Diactoros\Response;
|
Chris@0
|
11
|
Chris@0
|
12 use InvalidArgumentException;
|
Chris@0
|
13 use Psr\Http\Message\ResponseInterface;
|
Chris@0
|
14 use Psr\Http\Message\StreamInterface;
|
Chris@0
|
15 use UnexpectedValueException;
|
Chris@0
|
16 use Zend\Diactoros\AbstractSerializer;
|
Chris@0
|
17 use Zend\Diactoros\Response;
|
Chris@0
|
18 use Zend\Diactoros\Stream;
|
Chris@0
|
19
|
Chris@0
|
20 final class Serializer extends AbstractSerializer
|
Chris@0
|
21 {
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Deserialize a response string to a response instance.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @param string $message
|
Chris@0
|
26 * @return Response
|
Chris@0
|
27 * @throws UnexpectedValueException when errors occur parsing the message.
|
Chris@0
|
28 */
|
Chris@0
|
29 public static function fromString($message)
|
Chris@0
|
30 {
|
Chris@0
|
31 $stream = new Stream('php://temp', 'wb+');
|
Chris@0
|
32 $stream->write($message);
|
Chris@0
|
33 return static::fromStream($stream);
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Parse a response from a stream.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @param StreamInterface $stream
|
Chris@0
|
40 * @return Response
|
Chris@0
|
41 * @throws InvalidArgumentException when the stream is not readable.
|
Chris@0
|
42 * @throws UnexpectedValueException when errors occur parsing the message.
|
Chris@0
|
43 */
|
Chris@0
|
44 public static function fromStream(StreamInterface $stream)
|
Chris@0
|
45 {
|
Chris@0
|
46 if (! $stream->isReadable() || ! $stream->isSeekable()) {
|
Chris@0
|
47 throw new InvalidArgumentException('Message stream must be both readable and seekable');
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 $stream->rewind();
|
Chris@0
|
51
|
Chris@0
|
52 list($version, $status, $reasonPhrase) = self::getStatusLine($stream);
|
Chris@0
|
53 list($headers, $body) = self::splitStream($stream);
|
Chris@0
|
54
|
Chris@0
|
55 return (new Response($body, $status, $headers))
|
Chris@0
|
56 ->withProtocolVersion($version)
|
Chris@0
|
57 ->withStatus((int) $status, $reasonPhrase);
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 /**
|
Chris@0
|
61 * Create a string representation of a response.
|
Chris@0
|
62 *
|
Chris@0
|
63 * @param ResponseInterface $response
|
Chris@0
|
64 * @return string
|
Chris@0
|
65 */
|
Chris@0
|
66 public static function toString(ResponseInterface $response)
|
Chris@0
|
67 {
|
Chris@0
|
68 $reasonPhrase = $response->getReasonPhrase();
|
Chris@0
|
69 $headers = self::serializeHeaders($response->getHeaders());
|
Chris@0
|
70 $body = (string) $response->getBody();
|
Chris@0
|
71 $format = 'HTTP/%s %d%s%s%s';
|
Chris@0
|
72
|
Chris@0
|
73 if (! empty($headers)) {
|
Chris@0
|
74 $headers = "\r\n" . $headers;
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 $headers .= "\r\n\r\n";
|
Chris@0
|
78
|
Chris@0
|
79 return sprintf(
|
Chris@0
|
80 $format,
|
Chris@0
|
81 $response->getProtocolVersion(),
|
Chris@0
|
82 $response->getStatusCode(),
|
Chris@0
|
83 ($reasonPhrase ? ' ' . $reasonPhrase : ''),
|
Chris@0
|
84 $headers,
|
Chris@0
|
85 $body
|
Chris@0
|
86 );
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * Retrieve the status line for the message.
|
Chris@0
|
91 *
|
Chris@0
|
92 * @param StreamInterface $stream
|
Chris@0
|
93 * @return array Array with three elements: 0 => version, 1 => status, 2 => reason
|
Chris@0
|
94 * @throws UnexpectedValueException if line is malformed
|
Chris@0
|
95 */
|
Chris@0
|
96 private static function getStatusLine(StreamInterface $stream)
|
Chris@0
|
97 {
|
Chris@0
|
98 $line = self::getLine($stream);
|
Chris@0
|
99
|
Chris@0
|
100 if (! preg_match(
|
Chris@0
|
101 '#^HTTP/(?P<version>[1-9]\d*\.\d) (?P<status>[1-5]\d{2})(\s+(?P<reason>.+))?$#',
|
Chris@0
|
102 $line,
|
Chris@0
|
103 $matches
|
Chris@0
|
104 )) {
|
Chris@0
|
105 throw new UnexpectedValueException('No status line detected');
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 return [$matches['version'], $matches['status'], isset($matches['reason']) ? $matches['reason'] : ''];
|
Chris@0
|
109 }
|
Chris@0
|
110 }
|