1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ScayTrase\Api\JsonRpc; |
4
|
|
|
|
5
|
|
|
use ScayTrase\Api\Rpc\RpcRequestInterface; |
6
|
|
|
|
7
|
|
|
final class JsonRpcRequest implements JsonRpcRequestInterface |
8
|
|
|
{ |
9
|
|
|
/** @var string */ |
10
|
|
|
private $id; |
11
|
|
|
/** @var string */ |
12
|
|
|
private $method; |
13
|
|
|
/** @var \stdClass|array|null */ |
14
|
|
|
private $parameters; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* JsonRpcRequest constructor. |
18
|
|
|
* |
19
|
|
|
* @param string $method |
20
|
|
|
* @param \stdClass|array|null $parameters |
21
|
|
|
* @param string $id |
22
|
|
|
* |
23
|
|
|
* @throws \InvalidArgumentException |
24
|
|
|
*/ |
25
|
16 |
|
public function __construct($method, $parameters = null, $id) |
26
|
|
|
{ |
27
|
16 |
|
$this->method = (string)$method; |
28
|
16 |
|
$this->parameters = $parameters; |
29
|
16 |
|
$this->id = (string)$id; |
30
|
|
|
|
31
|
16 |
|
if (empty($this->id)) { |
32
|
|
|
throw new \InvalidArgumentException( |
33
|
|
|
'ID should not be empty for JSON-RPC request, use notification instead' |
34
|
|
|
); |
35
|
|
|
} |
36
|
16 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param RpcRequestInterface $request |
40
|
|
|
* @param string $id |
41
|
|
|
* |
42
|
|
|
* @return static |
43
|
|
|
* |
44
|
|
|
* @throws \InvalidArgumentException |
45
|
|
|
*/ |
46
|
1 |
|
public static function fromRpcRequest(RpcRequestInterface $request, $id) |
47
|
|
|
{ |
48
|
1 |
|
return new static($request->getMethod(), $request->getParameters(), $id); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** {@inheritdoc} */ |
52
|
4 |
|
public function isNotification() |
53
|
|
|
{ |
54
|
4 |
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** {@inheritdoc} */ |
58
|
16 |
|
public function getId() |
59
|
|
|
{ |
60
|
16 |
|
return $this->id; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** {@inheritdoc} */ |
64
|
16 |
|
public function getMethod() |
65
|
|
|
{ |
66
|
16 |
|
return $this->method; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** {@inheritdoc} */ |
70
|
16 |
|
public function getParameters() |
71
|
|
|
{ |
72
|
16 |
|
return $this->parameters; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
16 |
|
public function jsonSerialize() |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
16 |
|
self::VERSION_FIELD => $this->getVersion(), |
82
|
16 |
|
self::ID_FIELD => $this->getId(), |
83
|
16 |
|
self::METHOD_FIELD => $this->getMethod(), |
84
|
16 |
|
self::PARAMETERS_FIELD => $this->getParameters(), |
85
|
16 |
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns version of the JSON-RPC request |
90
|
|
|
* |
91
|
|
|
* A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0". |
92
|
|
|
* |
93
|
|
|
* @return string JSON-RPC version |
94
|
|
|
*/ |
95
|
16 |
|
public function getVersion() |
96
|
|
|
{ |
97
|
16 |
|
return JsonRpcClient::VERSION; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|