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;
22 * Serialize (cast to string) or deserialize (cast string to Request) messages.
24 * This class provides functionality for serializing a RequestInterface instance
25 * to a string, as well as the reverse operation of creating a Request instance
26 * from a string/stream representing a message.
28 final class Serializer extends AbstractSerializer
31 * Deserialize a request string to a request instance.
33 * Internally, casts the message to a stream and invokes fromStream().
35 * @param string $message
37 * @throws UnexpectedValueException when errors occur parsing the message.
39 public static function fromString($message)
41 $stream = new Stream('php://temp', 'wb+');
42 $stream->write($message);
43 return self::fromStream($stream);
47 * Deserialize a request stream to a request instance.
49 * @param StreamInterface $stream
51 * @throws UnexpectedValueException when errors occur parsing the message.
53 public static function fromStream(StreamInterface $stream)
55 if (! $stream->isReadable() || ! $stream->isSeekable()) {
56 throw new InvalidArgumentException('Message stream must be both readable and seekable');
61 list($method, $requestTarget, $version) = self::getRequestLine($stream);
62 $uri = self::createUriFromRequestTarget($requestTarget);
64 list($headers, $body) = self::splitStream($stream);
66 return (new Request($uri, $method, $body, $headers))
67 ->withProtocolVersion($version)
68 ->withRequestTarget($requestTarget);
72 * Serialize a request message to a string.
74 * @param RequestInterface $request
77 public static function toString(RequestInterface $request)
79 $httpMethod = $request->getMethod();
80 if (empty($httpMethod)) {
81 throw new UnexpectedValueException('Object can not be serialized because HTTP method is empty');
83 $headers = self::serializeHeaders($request->getHeaders());
84 $body = (string) $request->getBody();
85 $format = '%s %s HTTP/%s%s%s';
87 if (! empty($headers)) {
88 $headers = "\r\n" . $headers;
91 $headers .= "\r\n\r\n";
97 $request->getRequestTarget(),
98 $request->getProtocolVersion(),
105 * Retrieve the components of the request line.
107 * Retrieves the first line of the stream and parses it, raising an
108 * exception if it does not follow specifications; if valid, returns a list
109 * with the method, target, and version, in that order.
111 * @param StreamInterface $stream
114 private static function getRequestLine(StreamInterface $stream)
116 $requestLine = self::getLine($stream);
119 '#^(?P<method>[!\#$%&\'*+.^_`|~a-zA-Z0-9-]+) (?P<target>[^\s]+) HTTP/(?P<version>[1-9]\d*\.\d+)$#',
123 throw new UnexpectedValueException('Invalid request line detected');
126 return [$matches['method'], $matches['target'], $matches['version']];
130 * Create and return a Uri instance based on the provided request target.
132 * If the request target is of authority or asterisk form, an empty Uri
133 * instance is returned; otherwise, the value is used to create and return
134 * a new Uri instance.
136 * @param string $requestTarget
139 private static function createUriFromRequestTarget($requestTarget)
141 if (preg_match('#^https?://#', $requestTarget)) {
142 return new Uri($requestTarget);
145 if (preg_match('#^(\*|[^/])#', $requestTarget)) {
149 return new Uri($requestTarget);