1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\JsonRpcServer\App\Serialization; |
3
|
|
|
|
4
|
|
|
use Yoanm\JsonRpcServer\Domain\Exception\JsonRpcInvalidRequestException; |
5
|
|
|
use Yoanm\JsonRpcServer\Domain\Model\JsonRpcRequest; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class JsonRpcRequestDenormalizer |
9
|
|
|
*/ |
10
|
|
|
class JsonRpcRequestDenormalizer |
11
|
|
|
{ |
12
|
|
|
const KEY_JSON_RPC = 'jsonrpc'; |
13
|
|
|
const KEY_ID = 'id'; |
14
|
|
|
const KEY_METHOD = 'method'; |
15
|
|
|
const KEY_PARAM_LIST = 'params'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param mixed $item Should be an array |
19
|
|
|
* |
20
|
|
|
* @return JsonRpcRequest |
21
|
|
|
* |
22
|
|
|
* @throws JsonRpcInvalidRequestException |
23
|
|
|
*/ |
24
|
39 |
|
public function denormalize($item) : JsonRpcRequest |
25
|
|
|
{ |
26
|
39 |
|
$this->validateArray($item, 'Item must be an array'); |
27
|
|
|
|
28
|
|
|
// Validate json-rpc and method keys |
29
|
36 |
|
$this->validateRequiredKey($item, self::KEY_JSON_RPC); |
30
|
32 |
|
$this->validateRequiredKey($item, self::KEY_METHOD); |
31
|
|
|
|
32
|
29 |
|
$request = new JsonRpcRequest( |
33
|
29 |
|
$item[self::KEY_JSON_RPC], |
34
|
29 |
|
$item[self::KEY_METHOD] |
35
|
29 |
|
); |
36
|
|
|
|
37
|
29 |
|
$this->bindIdIfProvided($request, $item); |
38
|
29 |
|
$this->bindParamListIfProvided($request, $item); |
39
|
|
|
|
40
|
23 |
|
return $request; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param JsonRpcRequest $request |
45
|
|
|
* @param array $item |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
29 |
|
protected function bindIdIfProvided(JsonRpcRequest $request, array $item) : void |
50
|
|
|
{ |
51
|
|
|
/** If no id defined => request is a notification */ |
52
|
29 |
|
if (isset($item[self::KEY_ID])) { |
53
|
20 |
|
$request->setId($item[self::KEY_ID]); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param JsonRpcRequest $request |
59
|
|
|
* @param array $item |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
* |
63
|
|
|
* @throws JsonRpcInvalidRequestException |
64
|
|
|
*/ |
65
|
29 |
|
protected function bindParamListIfProvided(JsonRpcRequest $request, array $item) : void |
66
|
|
|
{ |
67
|
29 |
|
if (isset($item[self::KEY_PARAM_LIST])) { |
68
|
15 |
|
$this->validateArray($item[self::KEY_PARAM_LIST], 'Parameter list must be an array'); |
69
|
9 |
|
$request->setParamList($item[self::KEY_PARAM_LIST]); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param mixed $value |
75
|
|
|
* @param string $errorDescription |
76
|
|
|
* |
77
|
|
|
* @return void |
78
|
|
|
* |
79
|
|
|
* @throws JsonRpcInvalidRequestException |
80
|
|
|
*/ |
81
|
39 |
|
private function validateArray($value, string $errorDescription) : void |
82
|
|
|
{ |
83
|
39 |
|
if (!is_array($value)) { |
84
|
10 |
|
throw new JsonRpcInvalidRequestException($value, $errorDescription); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $item |
90
|
|
|
* @param string $key |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
* |
94
|
|
|
* @throws JsonRpcInvalidRequestException |
95
|
|
|
*/ |
96
|
36 |
|
private function validateRequiredKey(array $item, string $key) : void |
97
|
|
|
{ |
98
|
36 |
|
if (!isset($item[$key])) { |
99
|
9 |
|
throw new JsonRpcInvalidRequestException( |
100
|
9 |
|
$item, |
101
|
9 |
|
sprintf('"%s" is a required key', $key) |
102
|
9 |
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|