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