f5e25eba2b86090fdae25f235ff235813f6d8078
[yaffs-website] / vendor / zendframework / zend-diactoros / src / Request / ArraySerializer.php
1 <?php
2 /**
3  * @see       http://github.com/zendframework/zend-diactoros for the canonical source repository
4  * @copyright Copyright (c) 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\Request;
9
10 use Psr\Http\Message\RequestInterface;
11 use UnexpectedValueException;
12 use Zend\Diactoros\Request;
13 use Zend\Diactoros\Stream;
14
15 use function sprintf;
16
17 /**
18  * Serialize or deserialize request messages to/from arrays.
19  *
20  * This class provides functionality for serializing a RequestInterface instance
21  * to an array, as well as the reverse operation of creating a Request instance
22  * from an array representing a message.
23  */
24 final class ArraySerializer
25 {
26     /**
27      * Serialize a request message to an array.
28      *
29      * @param RequestInterface $request
30      * @return array
31      */
32     public static function toArray(RequestInterface $request)
33     {
34         return [
35             'method'           => $request->getMethod(),
36             'request_target'   => $request->getRequestTarget(),
37             'uri'              => (string) $request->getUri(),
38             'protocol_version' => $request->getProtocolVersion(),
39             'headers'          => $request->getHeaders(),
40             'body'             => (string) $request->getBody(),
41         ];
42     }
43
44     /**
45      * Deserialize a request array to a request instance.
46      *
47      * @param array $serializedRequest
48      * @return Request
49      * @throws UnexpectedValueException when cannot deserialize response
50      */
51     public static function fromArray(array $serializedRequest)
52     {
53         try {
54             $uri             = self::getValueFromKey($serializedRequest, 'uri');
55             $method          = self::getValueFromKey($serializedRequest, 'method');
56             $body            = new Stream('php://memory', 'wb+');
57             $body->write(self::getValueFromKey($serializedRequest, 'body'));
58             $headers         = self::getValueFromKey($serializedRequest, 'headers');
59             $requestTarget   = self::getValueFromKey($serializedRequest, 'request_target');
60             $protocolVersion = self::getValueFromKey($serializedRequest, 'protocol_version');
61
62             return (new Request($uri, $method, $body, $headers))
63                 ->withRequestTarget($requestTarget)
64                 ->withProtocolVersion($protocolVersion);
65         } catch (\Exception $exception) {
66             throw new UnexpectedValueException('Cannot deserialize request', null, $exception);
67         }
68     }
69
70     /**
71      * @param array $data
72      * @param string $key
73      * @param string $message
74      * @return mixed
75      * @throws UnexpectedValueException
76      */
77     private static function getValueFromKey(array $data, $key, $message = null)
78     {
79         if (isset($data[$key])) {
80             return $data[$key];
81         }
82         if ($message === null) {
83             $message = sprintf('Missing "%s" key in serialized request', $key);
84         }
85         throw new UnexpectedValueException($message);
86     }
87 }