|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\JsonRpcServer\App\Serialization; |
|
3
|
|
|
|
|
4
|
|
|
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInvalidRequestException; |
|
5
|
|
|
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcParseErrorException; |
|
6
|
|
|
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcCall; |
|
7
|
|
|
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcCallResponse; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class JsonRpcCallSerializer |
|
11
|
|
|
*/ |
|
12
|
|
|
class JsonRpcCallSerializer |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var JsonRpcCallDenormalizer */ |
|
15
|
|
|
private $callDenormalizer; |
|
16
|
|
|
/** @var JsonRpcCallResponseNormalizer */ |
|
17
|
|
|
private $callResponseNormalizer; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param JsonRpcCallDenormalizer $callDenormalizer |
|
21
|
|
|
* @param JsonRpcCallResponseNormalizer $callResponseNormalizer |
|
22
|
|
|
*/ |
|
23
|
27 |
|
public function __construct( |
|
24
|
|
|
JsonRpcCallDenormalizer $callDenormalizer, |
|
25
|
|
|
JsonRpcCallResponseNormalizer $callResponseNormalizer |
|
26
|
|
|
) { |
|
27
|
27 |
|
$this->callDenormalizer = $callDenormalizer; |
|
28
|
27 |
|
$this->callResponseNormalizer = $callResponseNormalizer; |
|
29
|
27 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param string $content |
|
33
|
|
|
* |
|
34
|
|
|
* @return JsonRpcCall |
|
35
|
|
|
*/ |
|
36
|
7 |
|
public function deserialize(string $content) : JsonRpcCall |
|
37
|
|
|
{ |
|
38
|
7 |
|
return $this->denormalize( |
|
39
|
7 |
|
$this->decode($content) |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param JsonRpcCallResponse $jsonRpcCallReponse |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function serialize(JsonRpcCallResponse $jsonRpcCallReponse) : string |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->encode( |
|
51
|
|
|
$this->normalize($jsonRpcCallReponse) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param mixed $normalizedContent Could be an array or null for instance |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public function encode($normalizedContent) : string |
|
61
|
|
|
{ |
|
62
|
|
|
return json_encode($normalizedContent); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $requestContent |
|
67
|
|
|
* |
|
68
|
|
|
* @return array Decoded content |
|
69
|
|
|
* |
|
70
|
|
|
* @throws JsonRpcParseErrorException |
|
71
|
|
|
* @throws JsonRpcInvalidRequestException |
|
72
|
|
|
*/ |
|
73
|
20 |
|
public function decode(string $requestContent) : array |
|
74
|
|
|
{ |
|
75
|
20 |
|
$decodedContent = json_decode($requestContent, true); |
|
76
|
|
|
|
|
77
|
|
|
// Check if parsing is ok => Parse error |
|
78
|
20 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
|
79
|
1 |
|
throw new JsonRpcParseErrorException($requestContent, json_last_error(), json_last_error_msg()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// Content must be either an array (normal request) or an array of array (batch request) |
|
83
|
|
|
// => so must be an array |
|
84
|
|
|
// In case it's a batch call, at least one sub request must exist |
|
85
|
|
|
// and in case not, some required properties must exist |
|
86
|
|
|
// => array must have at least one child |
|
87
|
19 |
|
if (!is_array($decodedContent) || count($decodedContent) === 0) { |
|
88
|
5 |
|
throw new JsonRpcInvalidRequestException($requestContent); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
14 |
|
return $decodedContent; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param array $decodedContent |
|
96
|
|
|
* |
|
97
|
|
|
* @return JsonRpcCall |
|
98
|
|
|
*/ |
|
99
|
14 |
|
public function denormalize(array $decodedContent) : JsonRpcCall |
|
100
|
|
|
{ |
|
101
|
14 |
|
return $this->callDenormalizer->denormalize($decodedContent); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param JsonRpcCallResponse $jsonRpcCallResponse |
|
106
|
|
|
* |
|
107
|
|
|
* @return array|null |
|
108
|
|
|
*/ |
|
109
|
|
|
public function normalize(JsonRpcCallResponse $jsonRpcCallResponse) |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->callResponseNormalizer->normalize($jsonRpcCallResponse); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|