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