@@ -14,6 +14,14 @@ private function __construct(object $value){
14
14
$ this ->value = $ value ;
15
15
}
16
16
17
+
18
+ /**
19
+ * @return string|null
20
+ */
21
+ public function getJSONRPC (): ?string {
22
+ return $ this ->hasJSONRPC ()?$ this ->value ->jsonrpc :null ;
23
+ }
24
+
17
25
/**
18
26
* @param bool $strictId
19
27
* @return mixed|null
@@ -71,6 +79,13 @@ public function getErrorData(){
71
79
return $ this ->getError ()->data ?? null ;
72
80
}
73
81
82
+ /**
83
+ * @return bool
84
+ */
85
+ public function hasJSONRPC (): bool {
86
+ return property_exists ($ this ->value ,'jsonrpc ' ) && $ this ->value ->jsonrpc !==null ;
87
+ }
88
+
74
89
/**
75
90
* @param bool $strictId
76
91
* @return bool
@@ -129,6 +144,10 @@ public function isResponse(): bool{
129
144
return property_exists ($ this ->value ,'result ' ) || property_exists ($ this ->value ,'error ' );
130
145
}
131
146
147
+ public function isVersion2 (): bool {
148
+ return $ this ->getJSONRPC ()==='2.0 ' ;
149
+ }
150
+
132
151
/**
133
152
* @return object
134
153
*/
@@ -150,6 +169,28 @@ public static function createRequestMessageV1($id,string $method,array $params=[
150
169
]);
151
170
}
152
171
172
+ /**
173
+ * @param $id
174
+ * @param string $method
175
+ * @param object|array|null $params
176
+ * @return RequestMessage
177
+ * @throws JSONRPCException
178
+ */
179
+ public static function createRequestMessageV2 ($ id ,string $ method ,$ params =null ): RequestMessage {
180
+ $ arr = [
181
+ 'jsonrpc ' => '2.0 ' ,
182
+ 'id ' => $ id ,
183
+ 'method ' => $ method ,
184
+ ];
185
+
186
+ if (is_object ($ params ) || is_array ($ params )){
187
+ $ arr ['params ' ] = $ params ;
188
+ }elseif (!is_null ($ params )){
189
+ throw new JSONRPCException ('[V2] The "params" property in request MUST be an object, array or null. ' );
190
+ }
191
+ return new RequestMessage ((object ) $ arr );
192
+ }
193
+
153
194
/**
154
195
* @param string $method
155
196
* @param array $params
@@ -163,6 +204,26 @@ public static function createNotificationMessageV1(string $method,array $params=
163
204
]);
164
205
}
165
206
207
+ /**
208
+ * @param string $method
209
+ * @param object|array|null $params
210
+ * @return NotificationMessage
211
+ * @throws JSONRPCException
212
+ */
213
+ public static function createNotificationMessageV2 (string $ method ,$ params =null ): NotificationMessage {
214
+ $ arr = [
215
+ 'jsonrpc ' => '2.0 ' ,
216
+ 'method ' => $ method ,
217
+ ];
218
+
219
+ if (is_object ($ params ) || is_array ($ params )){
220
+ $ arr ['params ' ] = $ params ;
221
+ }elseif (!is_null ($ params )){
222
+ throw new JSONRPCException ('[V2] The "params" property in request MUST be an object, array or null. ' );
223
+ }
224
+ return new NotificationMessage ((object ) $ arr );
225
+ }
226
+
166
227
/**
167
228
* @param $id
168
229
* @param $result
@@ -184,6 +245,32 @@ public static function createResponseMessageV1($id,$result=null,$error=null): Re
184
245
]);
185
246
}
186
247
248
+ /**
249
+ * @param $id
250
+ * @param $result
251
+ * @param object|string|null $error
252
+ * @return RequestMessage
253
+ * @throws JSONRPCException
254
+ */
255
+ public static function createResponseMessageV2 ($ id ,$ result =null ,$ error =null ): RequestMessage {
256
+ if (!is_null ($ result ) && !is_null ($ error )){
257
+ throw new JSONRPCException ('[V2] Only one property "result" or "error" can be non null. ' );
258
+ }
259
+ if (!is_object ($ error ) && !is_null ($ error )){
260
+ throw new JSONRPCException ('[V1] The "error" property in request MUST be an object or null. ' );
261
+ }
262
+ $ arr = [
263
+ 'jsonrpc ' => '2.0 ' ,
264
+ 'id ' => $ id ,
265
+ ];
266
+ if (!is_null ($ error )){
267
+ $ arr ['error ' ] = $ error ;
268
+ }else {
269
+ $ arr ['result ' ] = $ result ;
270
+ }
271
+ return new RequestMessage ((object ) $ arr );
272
+ }
273
+
187
274
/**
188
275
* @param $object
189
276
* @return bool
0 commit comments