@@ -61,7 +61,11 @@ public function __construct(Evaluator $evaluator)
61
61
*/
62
62
public function reply ($ json )
63
63
{
64
- $ output = $ this ->processInput ($ json );
64
+ if ($ this ->getInput ($ json , $ input )) {
65
+ $ output = $ this ->processInput ($ input );
66
+ } else {
67
+ $ output = $ this ->parseError ();
68
+ }
65
69
66
70
if ($ output === null ) {
67
71
return null ;
@@ -70,31 +74,32 @@ public function reply($json)
70
74
return json_encode ($ output );
71
75
}
72
76
77
+ private function getInput ($ json , &$ input )
78
+ {
79
+ if (!is_string ($ json )) {
80
+ return false ;
81
+ }
82
+
83
+ $ input = json_decode ($ json , true );
84
+
85
+ return is_array ($ input );
86
+ }
87
+
73
88
/**
74
89
* Processes the user input, and prepares a response (if necessary).
75
90
*
76
- * @param string $json
77
- * Single request object, or an array of request objects, as a JSON string .
91
+ * @param array $input
92
+ * Single request object, or an array of request objects.
78
93
*
79
94
* @return array|null
80
95
* Returns a response object (or an error object) when a query is made.
81
96
* Returns an array of response/error objects when multiple queries are made.
82
97
* Returns null when no response is necessary.
83
98
*/
84
- private function processInput ($ json )
99
+ private function processInput ($ input )
85
100
{
86
- if (!is_string ($ json )) {
87
- return self ::parseError ();
88
- }
89
-
90
- $ input = json_decode ($ json , true );
91
-
92
- if (!is_array ($ input )) {
93
- return self ::parseError ();
94
- }
95
-
96
101
if (count ($ input ) === 0 ) {
97
- return self :: requestError ();
102
+ return $ this -> requestError ();
98
103
}
99
104
100
105
if (isset ($ input [0 ])) {
@@ -147,7 +152,7 @@ private function processBatchRequests($input)
147
152
private function processRequest ($ request )
148
153
{
149
154
if (!is_array ($ request )) {
150
- return self :: requestError ();
155
+ return $ this -> requestError ();
151
156
}
152
157
153
158
// The presence of the 'id' key indicates that a response is expected
@@ -156,27 +161,27 @@ private function processRequest($request)
156
161
$ id = &$ request ['id ' ];
157
162
158
163
if (($ id !== null ) && !is_int ($ id ) && !is_float ($ id ) && !is_string ($ id )) {
159
- return self :: requestError ();
164
+ return $ this -> requestError ();
160
165
}
161
166
162
167
$ version = &$ request ['jsonrpc ' ];
163
168
164
169
if ($ version !== self ::VERSION ) {
165
- return self :: requestError ($ id );
170
+ return $ this -> requestError ($ id );
166
171
}
167
172
168
173
$ method = &$ request ['method ' ];
169
174
170
175
if (!is_string ($ method )) {
171
- return self :: requestError ($ id );
176
+ return $ this -> requestError ($ id );
172
177
}
173
178
174
179
// The 'params' key is optional, but must be non-null when provided
175
180
if (array_key_exists ('params ' , $ request )) {
176
181
$ arguments = $ request ['params ' ];
177
182
178
183
if (!is_array ($ arguments )) {
179
- return self :: requestError ($ id );
184
+ return $ this -> requestError ($ id );
180
185
}
181
186
} else {
182
187
$ arguments = array ();
@@ -210,13 +215,13 @@ private function processQuery($id, $method, $arguments)
210
215
{
211
216
try {
212
217
$ result = $ this ->evaluator ->evaluate ($ method , $ arguments );
213
- return self :: response ($ id , $ result );
218
+ return $ this -> response ($ id , $ result );
214
219
} catch (Exception $ exception ) {
215
220
$ code = $ exception ->getCode ();
216
221
$ message = $ exception ->getMessage ();
217
222
$ data = $ exception ->getData ();
218
223
219
- return self :: error ($ id , $ code , $ message , $ data );
224
+ return $ this -> error ($ id , $ code , $ message , $ data );
220
225
}
221
226
}
222
227
@@ -244,9 +249,9 @@ private function processNotification($method, $arguments)
244
249
* @return array
245
250
* Returns an error object.
246
251
*/
247
- private static function parseError ()
252
+ private function parseError ()
248
253
{
249
- return self :: error (null , -32700 , 'Parse error ' );
254
+ return $ this -> error (null , -32700 , 'Parse error ' );
250
255
}
251
256
252
257
/**
@@ -260,9 +265,9 @@ private static function parseError()
260
265
* @return array
261
266
* Returns an error object.
262
267
*/
263
- private static function requestError ($ id = null )
268
+ private function requestError ($ id = null )
264
269
{
265
- return self :: error ($ id , -32600 , 'Invalid Request ' );
270
+ return $ this -> error ($ id , -32600 , 'Invalid Request ' );
266
271
}
267
272
268
273
/**
@@ -285,7 +290,7 @@ private static function requestError($id = null)
285
290
* @return array
286
291
* Returns an error object.
287
292
*/
288
- private static function error ($ id , $ code , $ message , $ data = null )
293
+ private function error ($ id , $ code , $ message , $ data = null )
289
294
{
290
295
$ error = array (
291
296
'code ' => $ code ,
@@ -316,7 +321,7 @@ private static function error($id, $code, $message, $data = null)
316
321
* @return array
317
322
* Returns a response object.
318
323
*/
319
- private static function response ($ id , $ result )
324
+ private function response ($ id , $ result )
320
325
{
321
326
return array (
322
327
'jsonrpc ' => self ::VERSION ,
0 commit comments