response = $response; } /** * Gets the response object. * * @return ResponseInterface */ public function getResponse() { return $this->response; } /** * Asserts the HTTP response status code. * * @param string|string[] $status Expected status code(s) * * @throws \RuntimeException If status code does not match * * @return self */ public function status($status) { $httpStatus = (string) $this->response->getStatusCode(); if (is_array($status) && !in_array($httpStatus, $status)) { throw new \RuntimeException( "Unexpected response status code: {$httpStatus}" ); } if (is_string($status) && $httpStatus !== $status) { throw new \RuntimeException( "Unexpected response status code: {$httpStatus}" ); } return $this; } /** * Asserts the Content-Type header. * * @param string $mediaType Expected media type * * @throws \RuntimeException If Content-Type header is missing * @throws \RuntimeException If Content-Type header does not match * * @return self */ public function contentType($mediaType) { if (!$this->response->hasHeader('Content-Type')) { throw new \RuntimeException('Response is missing a Content-Type header'); } $contentType = $this->response->getHeader('Content-Type'); if ($contentType !== $mediaType) { throw new \RuntimeException( "Unexpected Content-Type header received: {$contentType}" ); } return $this; } /** * Get the decoded JSON response. * * @throws \RuntimeException If the response body is not in JSON format * @throws \InvalidArgumentException If the JSON cannot be parsed * * @return array */ public function getJson() { return $this->response->json(); } /** * Gets the Location header. * * @throws \RuntimeException If the Location header is missing * * @return string */ public function getLocation() { if (!$this->response->hasHeader('Location')) { throw new \RuntimeException('Response is missing a Location header'); } return $this->response->getHeader('Location'); } }