示例#1
0
 public function testBatch()
 {
     $collection = new Request();
     $this->assertEquals(false, $collection->isBatch());
     $entity = new EntityRequest();
     $collection->append($entity);
     $this->assertEquals(false, $collection->isBatch());
     $entity = new EntityRequest();
     $collection->offsetSet('will_not_be_used', $entity);
     $this->assertEquals(true, $collection->isBatch());
 }
示例#2
0
 /**
  * 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;
 }