Skip to content

Commit dab7433

Browse files
committed
Add create functions of version 2.0
1 parent c660efe commit dab7433

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/Message.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ private function __construct(object $value){
1414
$this->value = $value;
1515
}
1616

17+
18+
/**
19+
* @return string|null
20+
*/
21+
public function getJSONRPC(): ?string{
22+
return $this->hasJSONRPC()?$this->value->jsonrpc:null;
23+
}
24+
1725
/**
1826
* @param bool $strictId
1927
* @return mixed|null
@@ -71,6 +79,13 @@ public function getErrorData(){
7179
return $this->getError()->data ?? null;
7280
}
7381

82+
/**
83+
* @return bool
84+
*/
85+
public function hasJSONRPC(): bool{
86+
return property_exists($this->value,'jsonrpc') && $this->value->jsonrpc!==null;
87+
}
88+
7489
/**
7590
* @param bool $strictId
7691
* @return bool
@@ -129,6 +144,10 @@ public function isResponse(): bool{
129144
return property_exists($this->value,'result') || property_exists($this->value,'error');
130145
}
131146

147+
public function isVersion2(): bool{
148+
return $this->getJSONRPC()==='2.0';
149+
}
150+
132151
/**
133152
* @return object
134153
*/
@@ -150,6 +169,28 @@ public static function createRequestMessageV1($id,string $method,array $params=[
150169
]);
151170
}
152171

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+
153194
/**
154195
* @param string $method
155196
* @param array $params
@@ -163,6 +204,26 @@ public static function createNotificationMessageV1(string $method,array $params=
163204
]);
164205
}
165206

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+
166227
/**
167228
* @param $id
168229
* @param $result
@@ -184,6 +245,32 @@ public static function createResponseMessageV1($id,$result=null,$error=null): Re
184245
]);
185246
}
186247

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+
187274
/**
188275
* @param $object
189276
* @return bool

tests/MessageTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,21 @@ public function testIsResponse(){
394394
$this->assertTrue(Message::createResponseMessageV1(123,'myResult')->isResponse());
395395
}
396396

397+
/**
398+
* @return void
399+
* @throws JSONRPCException
400+
*/
401+
public function testIsVersion2(){
402+
$this->assertFalse(Message::createRequestMessageV1(123,'myMethod')->isVersion2());
403+
$this->assertTrue(Message::createRequestMessageV2(123,'myMethod')->isVersion2());
404+
405+
$this->assertFalse(Message::createNotificationMessageV1('myMethod')->isVersion2());
406+
$this->assertTrue(Message::createNotificationMessageV2('myMethod')->isVersion2());
407+
408+
$this->assertFalse(Message::createResponseMessageV1(123,'myMethod')->isVersion2());
409+
$this->assertTrue(Message::createResponseMessageV2(123,'myMethod')->isVersion2());
410+
}
411+
397412
/**
398413
* @return void
399414
* @throws JSONRPCException

0 commit comments

Comments
 (0)