Skip to content

Commit 4e57c09

Browse files
committed
Implement version 2.0
1 parent 26eecd8 commit 4e57c09

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/Message.php

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,46 @@ private static function handleMessage(object $message,bool $strictId=true){
148148
/**
149149
* @param object $message
150150
* @param bool $strictId
151-
* @return null
151+
* @return Message
152152
* @throws JSONRPCException
153153
*/
154154
private static function handleMessageV2(object $message,bool $strictId=true){
155155
if(self::isRequestMessage($message)){
156156
self::validateMethodProperty($message);
157-
return null;
157+
if(property_exists($message,'params') && !is_array($message->params) && !is_object($message->params)){
158+
throw new JSONRPCException('[V2] The "params" property MUST be an array or object if present.');
159+
}
160+
if(property_exists($message,'id') && !is_string($message->id) && !is_numeric($message->id) && !is_null($message->id)){
161+
throw new JSONRPCException('[V2] The "params" property MUST be an string, number or null if present.');
162+
}
163+
if(property_exists($message,'id') && ($strictId?($message->id!==null):($message->id))){
164+
return new RequestMessage($message);
165+
}else{
166+
return new NotificationMessage($message);
167+
}
158168
}elseif(self::isResponseMessage($message)){
159-
return null;
169+
if(property_exists($message,'result') && property_exists($message,'error')){
170+
throw new JSONRPCException('[V2] Only one property "result" or "error" can be present.');
171+
}
172+
if(property_exists($message,'error')){
173+
if(!property_exists($message->error,'code')){
174+
throw new JSONRPCException('[V2] The error object MUST have a "code" property.');
175+
}
176+
if(!property_exists($message->error,'message')){
177+
throw new JSONRPCException('[V2] The error object MUST have a "message" property.');
178+
}
179+
if(!is_int($message->error->code)){
180+
throw new JSONRPCException('[V2] The "code" property of the error object MUST be an integer.');
181+
}
182+
if(!is_string($message->error->message)){
183+
throw new JSONRPCException('[V2] The "message" property of the error object MUST be a string.');
184+
}
185+
}
186+
if(property_exists($message,'id')){
187+
return new ResponseMessage($message);
188+
}else{
189+
throw new JSONRPCException('[V2] Missing "id" property in response.');
190+
}
160191
}else{
161192
throw new JSONRPCException('[V2] Unknown message type.');
162193
}

0 commit comments

Comments
 (0)