@@ -148,15 +148,46 @@ private static function handleMessage(object $message,bool $strictId=true){
148
148
/**
149
149
* @param object $message
150
150
* @param bool $strictId
151
- * @return null
151
+ * @return Message
152
152
* @throws JSONRPCException
153
153
*/
154
154
private static function handleMessageV2 (object $ message ,bool $ strictId =true ){
155
155
if (self ::isRequestMessage ($ message )){
156
156
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
+ }
158
168
}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
+ }
160
191
}else {
161
192
throw new JSONRPCException ('[V2] Unknown message type. ' );
162
193
}
0 commit comments