productCheck($response); $this->response = $response; $status = $response->getStatusCode(); if ($throwException && $status > 399 && $status < 500) { $error = new ClientResponseException( sprintf("%s %s: %s", $status, $response->getReasonPhrase(), (string) $response->getBody()), $status ); throw $error->setResponse($response); } elseif ($throwException && $status > 499 && $status < 600) { $error = new ServerResponseException( sprintf("%s %s: %s", $status, $response->getReasonPhrase(), (string) $response->getBody()), $status ); throw $error->setResponse($response); } } /** * Return true if status code is 2xx */ public function asBool(): bool { return $this->response->getStatusCode() >=200 && $this->response->getStatusCode() < 300; } /** * Converts the body content to array, if possible. * Otherwise, it throws an UnknownContentTypeException * if Content-Type is not specified or unknown. * * @throws UnknownContentTypeException */ public function asArray(): array { if (isset($this->asArray)) { return $this->asArray; } if (!$this->response->hasHeader('Content-Type')) { throw new UnknownContentTypeException('No Content-Type specified in the response'); } $contentType = $this->response->getHeaderLine('Content-Type'); if (strpos($contentType, 'application/json') !== false || strpos($contentType, 'application/vnd.elasticsearch+json') !== false) { $this->asArray = JsonSerializer::unserialize($this->asString()); return $this->asArray; } if (strpos($contentType, 'application/x-ndjson') !== false || strpos($contentType, 'application/vnd.elasticsearch+x-ndjson') !== false) { $this->asArray = NDJsonSerializer::unserialize($this->asString()); return $this->asArray; } if (strpos($contentType, 'text/csv') !== false) { $this->asArray = CsvSerializer::unserialize($this->asString()); return $this->asArray; } throw new UnknownContentTypeException(sprintf( "Cannot deserialize the reponse as array with Content-Type: %s", $contentType )); } /** * Converts the body content to object, if possible. * Otherwise, it throws an UnknownContentTypeException * if Content-Type is not specified or unknown. * * @throws UnknownContentTypeException */ public function asObject(): object { if (isset($this->asObject)) { return $this->asObject; } $contentType = $this->response->getHeaderLine('Content-Type'); if (strpos($contentType, 'application/json') !== false || strpos($contentType, 'application/vnd.elasticsearch+json') !== false) { $this->asObject = JsonSerializer::unserialize($this->asString(), ['type' => 'object']); return $this->asObject; } if (strpos($contentType, 'application/x-ndjson') !== false || strpos($contentType, 'application/vnd.elasticsearch+x-ndjson') !== false) { $this->asObject = NDJsonSerializer::unserialize($this->asString(), ['type' => 'object']); return $this->asObject; } if (strpos($contentType, 'text/xml') !== false || strpos($contentType, 'application/xml') !== false) { $this->asObject = XmlSerializer::unserialize($this->asString()); return $this->asObject; } throw new UnknownContentTypeException(sprintf( "Cannot deserialize the reponse as object with Content-Type: %s", $contentType )); } /** * Converts the body content to string */ public function asString(): string { if (empty($this->asString)) { $this->asString = (string) $this->response->getBody(); } return $this->asString; } /** * Converts the body content to string */ public function __toString(): string { return $this->asString(); } /** * Access the body content as object properties * * @see https://www.php.net/manual/en/language.oop5.overloading.php#object.get */ public function __get($name) { return $this->asObject()->$name ?? null; } /** * ArrayAccess interface * * @see https://www.php.net/manual/en/class.arrayaccess.php */ public function offsetExists($offset): bool { return isset($this->asArray()[$offset]); } /** * ArrayAccess interface * * @see https://www.php.net/manual/en/class.arrayaccess.php */ #[\ReturnTypeWillChange] public function offsetGet($offset) { return $this->asArray()[$offset]; } /** * ArrayAccess interface * * @see https://www.php.net/manual/en/class.arrayaccess.php */ public function offsetSet($offset, $value): void { throw new ArrayAccessException('The array is reading only'); } /** * ArrayAccess interface * * @see https://www.php.net/manual/en/class.arrayaccess.php */ public function offsetUnset($offset): void { throw new ArrayAccessException('The array is reading only'); } }