Skip to content

Commit 85eceae

Browse files
committed
Isolated the JSON encoding
1 parent 4465221 commit 85eceae

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

src/Server.php

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ public function __construct(Evaluator $evaluator)
6161
*/
6262
public function reply($json)
6363
{
64-
$output = $this->processInput($json);
64+
if ($this->getInput($json, $input)) {
65+
$output = $this->processInput($input);
66+
} else {
67+
$output = $this->parseError();
68+
}
6569

6670
if ($output === null) {
6771
return null;
@@ -70,31 +74,32 @@ public function reply($json)
7074
return json_encode($output);
7175
}
7276

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+
7388
/**
7489
* Processes the user input, and prepares a response (if necessary).
7590
*
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.
7893
*
7994
* @return array|null
8095
* Returns a response object (or an error object) when a query is made.
8196
* Returns an array of response/error objects when multiple queries are made.
8297
* Returns null when no response is necessary.
8398
*/
84-
private function processInput($json)
99+
private function processInput($input)
85100
{
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-
96101
if (count($input) === 0) {
97-
return self::requestError();
102+
return $this->requestError();
98103
}
99104

100105
if (isset($input[0])) {
@@ -147,7 +152,7 @@ private function processBatchRequests($input)
147152
private function processRequest($request)
148153
{
149154
if (!is_array($request)) {
150-
return self::requestError();
155+
return $this->requestError();
151156
}
152157

153158
// The presence of the 'id' key indicates that a response is expected
@@ -156,27 +161,27 @@ private function processRequest($request)
156161
$id = &$request['id'];
157162

158163
if (($id !== null) && !is_int($id) && !is_float($id) && !is_string($id)) {
159-
return self::requestError();
164+
return $this->requestError();
160165
}
161166

162167
$version = &$request['jsonrpc'];
163168

164169
if ($version !== self::VERSION) {
165-
return self::requestError($id);
170+
return $this->requestError($id);
166171
}
167172

168173
$method = &$request['method'];
169174

170175
if (!is_string($method)) {
171-
return self::requestError($id);
176+
return $this->requestError($id);
172177
}
173178

174179
// The 'params' key is optional, but must be non-null when provided
175180
if (array_key_exists('params', $request)) {
176181
$arguments = $request['params'];
177182

178183
if (!is_array($arguments)) {
179-
return self::requestError($id);
184+
return $this->requestError($id);
180185
}
181186
} else {
182187
$arguments = array();
@@ -210,13 +215,13 @@ private function processQuery($id, $method, $arguments)
210215
{
211216
try {
212217
$result = $this->evaluator->evaluate($method, $arguments);
213-
return self::response($id, $result);
218+
return $this->response($id, $result);
214219
} catch (Exception $exception) {
215220
$code = $exception->getCode();
216221
$message = $exception->getMessage();
217222
$data = $exception->getData();
218223

219-
return self::error($id, $code, $message, $data);
224+
return $this->error($id, $code, $message, $data);
220225
}
221226
}
222227

@@ -244,9 +249,9 @@ private function processNotification($method, $arguments)
244249
* @return array
245250
* Returns an error object.
246251
*/
247-
private static function parseError()
252+
private function parseError()
248253
{
249-
return self::error(null, -32700, 'Parse error');
254+
return $this->error(null, -32700, 'Parse error');
250255
}
251256

252257
/**
@@ -260,9 +265,9 @@ private static function parseError()
260265
* @return array
261266
* Returns an error object.
262267
*/
263-
private static function requestError($id = null)
268+
private function requestError($id = null)
264269
{
265-
return self::error($id, -32600, 'Invalid Request');
270+
return $this->error($id, -32600, 'Invalid Request');
266271
}
267272

268273
/**
@@ -285,7 +290,7 @@ private static function requestError($id = null)
285290
* @return array
286291
* Returns an error object.
287292
*/
288-
private static function error($id, $code, $message, $data = null)
293+
private function error($id, $code, $message, $data = null)
289294
{
290295
$error = array(
291296
'code' => $code,
@@ -316,7 +321,7 @@ private static function error($id, $code, $message, $data = null)
316321
* @return array
317322
* Returns a response object.
318323
*/
319-
private static function response($id, $result)
324+
private function response($id, $result)
320325
{
321326
return array(
322327
'jsonrpc' => self::VERSION,

0 commit comments

Comments
 (0)