/** * @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); }
public function testFromArrayWithInvalidArrayValue() { $batch = [['method' => 'foo', 'id' => 123]]; $this->setExpectedException(InvalidArgumentException::class); BatchRequest::fromArray($batch); }