comparison vendor/zendframework/zend-diactoros/src/Request/ArraySerializer.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children 5311817fb629
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
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 /**
16 * Serialize or deserialize request messages to/from arrays.
17 *
18 * This class provides functionality for serializing a RequestInterface instance
19 * to an array, as well as the reverse operation of creating a Request instance
20 * from an array representing a message.
21 */
22 final class ArraySerializer
23 {
24 /**
25 * Serialize a request message to an array.
26 *
27 * @param RequestInterface $request
28 * @return array
29 */
30 public static function toArray(RequestInterface $request)
31 {
32 return [
33 'method' => $request->getMethod(),
34 'request_target' => $request->getRequestTarget(),
35 'uri' => (string) $request->getUri(),
36 'protocol_version' => $request->getProtocolVersion(),
37 'headers' => $request->getHeaders(),
38 'body' => (string) $request->getBody(),
39 ];
40 }
41
42 /**
43 * Deserialize a request array to a request instance.
44 *
45 * @param array $serializedRequest
46 * @return Request
47 * @throws UnexpectedValueException when cannot deserialize response
48 */
49 public static function fromArray(array $serializedRequest)
50 {
51 try {
52 $uri = self::getValueFromKey($serializedRequest, 'uri');
53 $method = self::getValueFromKey($serializedRequest, 'method');
54 $body = new Stream('php://memory', 'wb+');
55 $body->write(self::getValueFromKey($serializedRequest, 'body'));
56 $headers = self::getValueFromKey($serializedRequest, 'headers');
57 $requestTarget = self::getValueFromKey($serializedRequest, 'request_target');
58 $protocolVersion = self::getValueFromKey($serializedRequest, 'protocol_version');
59
60 return (new Request($uri, $method, $body, $headers))
61 ->withRequestTarget($requestTarget)
62 ->withProtocolVersion($protocolVersion);
63 } catch (\Exception $exception) {
64 throw new UnexpectedValueException('Cannot deserialize request', null, $exception);
65 }
66 }
67
68 /**
69 * @param array $data
70 * @param string $key
71 * @param string $message
72 * @return mixed
73 * @throws UnexpectedValueException
74 */
75 private static function getValueFromKey(array $data, $key, $message = null)
76 {
77 if (isset($data[$key])) {
78 return $data[$key];
79 }
80 if ($message === null) {
81 $message = sprintf('Missing "%s" key in serialized request', $key);
82 }
83 throw new UnexpectedValueException($message);
84 }
85 }