annotate vendor/zendframework/zend-diactoros/src/Response/Serializer.php @ 12:7a779792577d

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