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\Request;
12 use InvalidArgumentException;
13 use Psr\Http\Message\RequestInterface;
14 use Psr\Http\Message\StreamInterface;
15 use UnexpectedValueException;
16 use Zend\Diactoros\AbstractSerializer;
17 use Zend\Diactoros\Request;
18 use Zend\Diactoros\Stream;
19 use Zend\Diactoros\Uri;
21 use function preg_match;
25 * Serialize (cast to string) or deserialize (cast string to Request) messages.
27 * This class provides functionality for serializing a RequestInterface instance
28 * to a string, as well as the reverse operation of creating a Request instance
29 * from a string/stream representing a message.
31 final class Serializer extends AbstractSerializer
34 * Deserialize a request string to a request instance.
36 * Internally, casts the message to a stream and invokes fromStream().
38 * @param string $message
40 * @throws UnexpectedValueException when errors occur parsing the message.
42 public static function fromString($message)
44 $stream = new Stream('php://temp', 'wb+');
45 $stream->write($message);
46 return self::fromStream($stream);
50 * Deserialize a request stream to a request instance.
52 * @param StreamInterface $stream
54 * @throws UnexpectedValueException when errors occur parsing the message.
56 public static function fromStream(StreamInterface $stream)
58 if (! $stream->isReadable() || ! $stream->isSeekable()) {
59 throw new InvalidArgumentException('Message stream must be both readable and seekable');
64 list($method, $requestTarget, $version) = self::getRequestLine($stream);
65 $uri = self::createUriFromRequestTarget($requestTarget);
67 list($headers, $body) = self::splitStream($stream);
69 return (new Request($uri, $method, $body, $headers))
70 ->withProtocolVersion($version)
71 ->withRequestTarget($requestTarget);
75 * Serialize a request message to a string.
77 * @param RequestInterface $request
80 public static function toString(RequestInterface $request)
82 $httpMethod = $request->getMethod();
83 if (empty($httpMethod)) {
84 throw new UnexpectedValueException('Object can not be serialized because HTTP method is empty');
86 $headers = self::serializeHeaders($request->getHeaders());
87 $body = (string) $request->getBody();
88 $format = '%s %s HTTP/%s%s%s';
90 if (! empty($headers)) {
91 $headers = "\r\n" . $headers;
94 $headers .= "\r\n\r\n";
100 $request->getRequestTarget(),
101 $request->getProtocolVersion(),
108 * Retrieve the components of the request line.
110 * Retrieves the first line of the stream and parses it, raising an
111 * exception if it does not follow specifications; if valid, returns a list
112 * with the method, target, and version, in that order.
114 * @param StreamInterface $stream
117 private static function getRequestLine(StreamInterface $stream)
119 $requestLine = self::getLine($stream);
122 '#^(?P<method>[!\#$%&\'*+.^_`|~a-zA-Z0-9-]+) (?P<target>[^\s]+) HTTP/(?P<version>[1-9]\d*\.\d+)$#',
126 throw new UnexpectedValueException('Invalid request line detected');
129 return [$matches['method'], $matches['target'], $matches['version']];
133 * Create and return a Uri instance based on the provided request target.
135 * If the request target is of authority or asterisk form, an empty Uri
136 * instance is returned; otherwise, the value is used to create and return
137 * a new Uri instance.
139 * @param string $requestTarget
142 private static function createUriFromRequestTarget($requestTarget)
144 if (preg_match('#^https?://#', $requestTarget)) {
145 return new Uri($requestTarget);
148 if (preg_match('#^(\*|[^/])#', $requestTarget)) {
152 return new Uri($requestTarget);