コード例 #1
0
ファイル: Server.php プロジェクト: jdolieslager/jsonrpc
 /**
  * Create Request object from Raw Request
  *
  * @param string $string
  * @return Collection\Request | Collection\Response on error
  */
 public function createRequestFromRawRequest($string)
 {
     // Process through the layer before processing
     $string = $this->protocolLayerStack->handleRequest($string);
     $collection = new Collection\Request();
     try {
         // Decode the data
         $data = @json_decode($string, true);
         // No array means decoding failed
         if (is_array($data) === false) {
             throw new Exception\InvalidRequest('Parse error', static::PARSE_ERROR);
         }
         // Make it a batch request
         if (Common\ArrayUtils::isAssociative($data)) {
             $data = array($data);
         }
         foreach ($data as $requestData) {
             // Always hace request object
             $request = new Entity\Request();
             // Append the request object
             $collection->append($request);
             // Next checks expects an array
             if (is_array($requestData) === false) {
                 $requestData = array();
             }
             // Get the available arguments
             $jsonRpc = $this->getArrayItem('jsonrpc', $requestData, null);
             $method = $this->getArrayItem('method', $requestData, null);
             $params = $this->getArrayItem('params', $requestData, array());
             $id = $this->getArrayItem('id', $requestData, null);
             // Set the values on the request object
             $request->setJsonrpc($jsonRpc)->setId($id)->setMethod($method);
             // Set all the parameters
             foreach ($params as $offset => $value) {
                 $request->addParam($value, $offset);
             }
         }
     } catch (\Exception $e) {
         // Error occured and create
         $response = $this->createResponseFromException(new Entity\Request(), $e);
         // Create collection response object
         $responseCollection = new Collection\Response();
         // Attach the single response error (parse error);
         $responseCollection->append($response);
         // Return the response collection
         return $responseCollection;
     }
     return $collection;
 }
コード例 #2
0
ファイル: Client.php プロジェクト: jdolieslager/jsonrpc
 /**
  * Prepare the request for sending
  *
  * @param Collection\Request $request
  * @return string A JSON encoded JSON RPC string
  */
 protected function prepareRequest(Collection\Request $request)
 {
     $data = $request->getArrayCopy();
     foreach ($data as $index => $value) {
         if ($value['jsonrpc'] === null) {
             unset($data[$index]['jsonrpc']);
         }
         if (empty($value['params'])) {
             unset($data[$index]['params']);
         }
         if ($value['id'] === null) {
             unset($data[$index]['id']);
         }
     }
     // On single request we do not send in batch mode
     if ($request->isBatch() === false) {
         $data = array_shift($data);
     }
     $json = @json_encode($data);
     if ($json === false) {
         throw new Exception\RuntimeException('Could not encode request data', 1);
     }
     return $json;
 }
コード例 #3
0
ファイル: RequestTest.php プロジェクト: jdolieslager/jsonrpc
 public function testPerformCallbackFailure()
 {
     $this->setExpectedException('\\Jdolieslager\\JsonRpc\\Exception\\InvalidArgument');
     $collection = new Request();
     $response = new Response();
     $response->setId(1);
     $collection->performCallback($response);
 }