Skip to content

Commit

Permalink
Isolated the JSON encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
smortensen committed Jul 19, 2019
1 parent 4465221 commit 85eceae
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ public function __construct(Evaluator $evaluator)
*/
public function reply($json)
{
$output = $this->processInput($json);
if ($this->getInput($json, $input)) {
$output = $this->processInput($input);
} else {
$output = $this->parseError();
}

if ($output === null) {
return null;
Expand All @@ -70,31 +74,32 @@ public function reply($json)
return json_encode($output);
}

private function getInput($json, &$input)
{
if (!is_string($json)) {
return false;
}

$input = json_decode($json, true);

return is_array($input);
}

/**
* Processes the user input, and prepares a response (if necessary).
*
* @param string $json
* Single request object, or an array of request objects, as a JSON string.
* @param array $input
* Single request object, or an array of request objects.
*
* @return array|null
* Returns a response object (or an error object) when a query is made.
* Returns an array of response/error objects when multiple queries are made.
* Returns null when no response is necessary.
*/
private function processInput($json)
private function processInput($input)
{
if (!is_string($json)) {
return self::parseError();
}

$input = json_decode($json, true);

if (!is_array($input)) {
return self::parseError();
}

if (count($input) === 0) {
return self::requestError();
return $this->requestError();
}

if (isset($input[0])) {
Expand Down Expand Up @@ -147,7 +152,7 @@ private function processBatchRequests($input)
private function processRequest($request)
{
if (!is_array($request)) {
return self::requestError();
return $this->requestError();
}

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

if (($id !== null) && !is_int($id) && !is_float($id) && !is_string($id)) {
return self::requestError();
return $this->requestError();
}

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

if ($version !== self::VERSION) {
return self::requestError($id);
return $this->requestError($id);
}

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

if (!is_string($method)) {
return self::requestError($id);
return $this->requestError($id);
}

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

if (!is_array($arguments)) {
return self::requestError($id);
return $this->requestError($id);
}
} else {
$arguments = array();
Expand Down Expand Up @@ -210,13 +215,13 @@ private function processQuery($id, $method, $arguments)
{
try {
$result = $this->evaluator->evaluate($method, $arguments);
return self::response($id, $result);
return $this->response($id, $result);
} catch (Exception $exception) {
$code = $exception->getCode();
$message = $exception->getMessage();
$data = $exception->getData();

return self::error($id, $code, $message, $data);
return $this->error($id, $code, $message, $data);
}
}

Expand Down Expand Up @@ -244,9 +249,9 @@ private function processNotification($method, $arguments)
* @return array
* Returns an error object.
*/
private static function parseError()
private function parseError()
{
return self::error(null, -32700, 'Parse error');
return $this->error(null, -32700, 'Parse error');
}

/**
Expand All @@ -260,9 +265,9 @@ private static function parseError()
* @return array
* Returns an error object.
*/
private static function requestError($id = null)
private function requestError($id = null)
{
return self::error($id, -32600, 'Invalid Request');
return $this->error($id, -32600, 'Invalid Request');
}

/**
Expand All @@ -285,7 +290,7 @@ private static function requestError($id = null)
* @return array
* Returns an error object.
*/
private static function error($id, $code, $message, $data = null)
private function error($id, $code, $message, $data = null)
{
$error = array(
'code' => $code,
Expand Down Expand Up @@ -316,7 +321,7 @@ private static function error($id, $code, $message, $data = null)
* @return array
* Returns a response object.
*/
private static function response($id, $result)
private function response($id, $result)
{
return array(
'jsonrpc' => self::VERSION,
Expand Down

0 comments on commit 85eceae

Please sign in to comment.