Esempio n. 1
0
 /**
  * @param ServerRequestInterface $ServerRequest
  * @return BatchRequest|Request
  * @throws CouldNotParse
  */
 public static function fromHttpRequest(ServerRequestInterface $ServerRequest)
 {
     $Body = $ServerRequest->getBody();
     $jsonString = $Body->__toString();
     $data = json_decode($jsonString, true);
     if (is_null($data) && strtolower($jsonString) != 'null') {
         throw new CouldNotParse();
     }
     if (!is_array($data)) {
         throw new InvalidArgumentException('Request should be an object or an array of objects');
     }
     // If the first element is an array, treat the data as a batch request
     if (count($data) > 0 && is_array(reset($data))) {
         return BatchRequest::fromArray($data);
     }
     return self::fromArray($data);
 }
Esempio n. 2
0
 /**
  * @param BatchRequest $Request
  * @return BatchResponse
  */
 public function handleBatchRequest(BatchRequest $Request)
 {
     $responses = [];
     foreach ($Request->getRequests() as $Request) {
         $responses[] = $this->handleRequest($Request);
     }
     return new BatchResponse($responses);
 }
Esempio n. 3
0
 public function testFromArrayWithInvalidArrayValue()
 {
     $batch = [['method' => 'foo', 'id' => 123]];
     $this->setExpectedException(InvalidArgumentException::class);
     BatchRequest::fromArray($batch);
 }