1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chadicus\Marvel\Api\Cache; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Response; |
6
|
|
|
use Chadicus\Psr\SimpleCache\SerializerInterface; |
7
|
|
|
use DominionEnterprises\Util; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Provides serialization from mongo documents to PSR-7 response objects. |
11
|
|
|
*/ |
12
|
|
|
final class Serializer implements SerializerInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Unserializes cached data into the original state. |
16
|
|
|
* |
17
|
|
|
* @param array $data The data to unserialize. |
18
|
|
|
* |
19
|
|
|
* @return Response |
20
|
|
|
*/ |
21
|
|
|
public function unserialize(array $data) |
22
|
|
|
{ |
23
|
|
|
return new Response( |
24
|
|
|
$data['statusCode'], |
25
|
|
|
$data['headers'], |
26
|
|
|
$data['body'], |
27
|
|
|
$data['protocolVersion'], |
28
|
|
|
$data['reasonPhrase'] |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Serializes the given data for storage in caching. |
34
|
|
|
* |
35
|
|
|
* @param mixed $value The data to serialize for caching. |
36
|
|
|
* |
37
|
|
|
* @return array The result of serializing the given $data. |
38
|
|
|
* |
39
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException Thrown if the given value is not a PSR-7 Response instance. |
40
|
|
|
*/ |
41
|
|
|
public function serialize($value) : array |
42
|
|
|
{ |
43
|
|
|
Util::ensure( |
44
|
|
|
true, |
45
|
|
|
is_a($value, '\\Psr\\Http\\Message\\ResponseInterface'), |
46
|
|
|
'\\Chadicus\\Psr\\SimpleCache\\InvalidArgumentException', |
47
|
|
|
['$value must be an instance of \\Psr\\Http\\Message\\ResponseInterface'] |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
return [ |
51
|
|
|
'statusCode' => $value->getStatusCode(), |
52
|
|
|
'headers' => $value->getHeaders(), |
53
|
|
|
'body' => (string)$value->getBody(), |
54
|
|
|
'protocolVersion' => $value->getProtocolVersion(), |
55
|
|
|
'reasonPhrase' => $value->getReasonPhrase(), |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|