Skip to content

Commit

Permalink
small bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cranetm committed Dec 9, 2014
1 parent 2e65be2 commit 3722113
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
38 changes: 12 additions & 26 deletions JsonRpc2/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ class Controller extends \yii\web\Controller
/** @var \stdClass Contains parsed JSON-RPC 2.0 request object*/
private $requestObject;

/** @var array Use as 'result' when Action returns null */
private $defaultResult = [
"success" => true
];

/**
* Validates, runs Action and returns result in JSON-RPC 2.0 format
* @param string $id the ID of the action to be executed.
Expand All @@ -49,9 +44,14 @@ public function runAction($id, $params = [])
$isBatch = is_array($requestObject);
$requests = $isBatch ? $requestObject : [$requestObject];
$resultData = null;
foreach ($requests as $request) {
if($response = $this->getActionResponse($request))
$resultData[] = $response;
if (empty($requests)) {
$isBatch = false;
$resultData = [Helper::formatResponse(null, new Exception("Invalid Request", Exception::INVALID_REQUEST))];
} else {
foreach ($requests as $request) {
if($response = $this->getActionResponse($request))
$resultData[] = $response;
}
}

$response = new Response();
Expand All @@ -69,8 +69,7 @@ public function runAction($id, $params = [])
*/
private function getActionResponse($requestObject)
{
$error = null;
$result = [];
$result = $error = null;
try {
$this->parseAndValidateRequestObject($requestObject);
ob_start();
Expand All @@ -85,22 +84,10 @@ private function getActionResponse($requestObject)
$error = new Exception("Internal error", Exception::INTERNAL_ERROR);
}

$responseData = [];
if (!isset($this->requestObject->id) && (empty($error) || $error->getCode() != Exception::PARSE_ERROR))
return $responseData;
return null;

$responseData = [
'jsonrpc' => '2.0',
'id' => !empty($this->requestObject->id)? $this->requestObject->id : null,
];

if (!empty($error))
$responseData['error'] = $error->toArray();

if (!empty($result) || is_array($result))
$responseData['result'] = $result;

return $responseData;
return Helper::formatResponse($result, $error, !empty($this->requestObject->id)? $this->requestObject->id : null);
}

/**
Expand Down Expand Up @@ -297,8 +284,7 @@ private function validateResult($result)
$this->methodInfo['return']['isNullable'],
$this->methodInfo['return']['restrictions']
);
} else if (empty($result))
$result = $this->defaultResult;
}

return $result;
}
Expand Down
31 changes: 30 additions & 1 deletion JsonRpc2/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,36 @@

namespace JsonRpc2;

class Helper {
class Helper
{
/** @var array Use as 'result' when method returns null */
private static $defaultResult = [
"success" => true
];

/**
* Formats and returns
* @param null $result
* @param \JsonRpc2\Exception|null $error
* @param null $id
* @return array
*/
public static function formatResponse ($result = null, Exception $error = null, $id = null)
{
$response = [
'jsonrpc' => '2.0',
'id' => $id,
];

if (!empty($error))
$response['error'] = $error->toArray();
else if (null === $result)
$response['result'] = self::$defaultResult;
else
$response['result'] = $result;

return $response;
}

/**
* Recursively brings value to type
Expand Down

0 comments on commit 3722113

Please sign in to comment.