コード例 #1
0
 /**
  * Construct a Response from JSON-RPC style result, error, and JSON-RPC request.
  *
  * @param mixed $result The result of the remote procedure call.
  * @param Exception $error The error, as an exception. (Requires code and message.)
  * @param JsonRpcRequest $request The request that led to the generation of this response.
  */
 public function __construct($result, Exception $error = null, JsonRpcRequest $request = null)
 {
     $response = new stdClass();
     /* JSON-RPC spec: either error or result, never both. */
     if ($error) {
         $response->error = (object) array('code' => $error->getCode(), 'message' => $error->getMessage());
     } else {
         $response->result = $result;
     }
     if ($request) {
         /* 1.0: omit version, 2.0 and newer, echo back version. */
         if ($request->getVersion() != "1.0") {
             $response->jsonrpc = $request->getVersion();
         }
         /* For notifications (null id), return nothing. Otherwise, pass back the id. */
         if ($request->getIdentifier() === null) {
             $response = "";
         } else {
             $response->id = $request->getIdentifier();
         }
     }
     parent::__construct($response);
 }
コード例 #2
0
 /**
  * An overview of how to use the JsonRpcRequest class.
  * @test
  */
 public function synopsis()
 {
     /* Handles JSON-RPC 1.0 requests. */
     $request = new JsonRpcRequest('MyService', (object) array('method' => 'remoteProcedure', 'params' => array(1, 2, 3), 'id' => 'uniqueidentifier'));
     $this->assertEquals('POST', $request->getVerb());
     $this->assertEquals('remoteProcedure', $request->getMethod());
     $this->assertEquals('1.0', $request->getVersion());
     $this->assertEquals(array(1, 2, 3), $request->getParameters());
     $this->assertEquals('uniqueidentifier', $request->getIdentifier());
     $this->assertNull($request->getPost('anything'));
     // Post should be ignored.
     /* Handles JSON-RPC 2.0 requests. */
     $request = new JsonRpcRequest('MyService', (object) array('jsonrpc' => '2.0', 'method' => 'remoteProcedure', 'id' => 'uniqueidentifier'));
     $this->assertEquals('2.0', $request->getVersion());
     $this->assertEquals(array(), $request->getParameters());
     /* Catches invalid request format. */
     $request = new JsonRpcRequest('MyService', (object) array('jsonrpc' => '2.0', 'method' => null));
     $this->assertFalse($request->isValid());
 }