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