Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions src/Response/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Elasticsearch B.V licenses this file to you under the MIT License.
* See the LICENSE file in the project root for more information.
*/
declare(strict_types = 1);
declare(strict_types=1);

namespace Elastic\Elasticsearch\Response;

Expand All @@ -36,11 +36,10 @@
*/
class Elasticsearch implements ElasticsearchInterface, ResponseInterface, ArrayAccess
{
const HEADER_CHECK = 'X-Elastic-Product';
const PRODUCT_NAME = 'Elasticsearch';

use ProductCheckTrait;
use MessageResponseTrait;
public const HEADER_CHECK = 'X-Elastic-Product';
public const PRODUCT_NAME = 'Elasticsearch';

protected array $asArray;
protected object $asObject;
Expand All @@ -67,6 +66,10 @@ public function setResponse(ResponseInterface $response, bool $throwException =
// Check for Serverless response
$this->serverless = $this->isServerlessResponse($response);
$this->response = $response;

unset($this->asArray, $this->asObject);
$this->asString = '';

$status = $response->getStatusCode();
if ($throwException && $status > 399 && $status < 500) {
$error = new ClientResponseException(
Expand Down Expand Up @@ -104,14 +107,14 @@ public function isServerless(): bool
*/
public function asBool(): bool
{
return $this->response->getStatusCode() >=200 && $this->response->getStatusCode() < 300;
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
Expand Down Expand Up @@ -147,7 +150,7 @@ public function asArray(): array
* 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
Expand Down Expand Up @@ -197,7 +200,7 @@ public function __toString(): string

/**
* Access the body content as object properties
*
*
* @see https://www.php.net/manual/en/language.oop5.overloading.php#object.get
*/
public function __get($name)
Expand All @@ -207,17 +210,17 @@ public function __get($name)

/**
* 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
*
* @return mixed
Expand All @@ -230,7 +233,7 @@ public function offsetGet($offset)

/**
* ArrayAccess interface
*
*
* @see https://www.php.net/manual/en/class.arrayaccess.php
*/
public function offsetSet($offset, $value): void
Expand All @@ -240,7 +243,7 @@ public function offsetSet($offset, $value): void

/**
* ArrayAccess interface
*
*
* @see https://www.php.net/manual/en/class.arrayaccess.php
*/
public function offsetUnset($offset): void
Expand All @@ -251,11 +254,11 @@ public function offsetUnset($offset): void
/**
* Map the response body to an object of a specific class
* by default the class is the PHP standard one (stdClass)
*
*
* This mapping works only for ES|QL results (with columns and values)
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/esql.html
*
* @return object[]
*
* @return object[]
*/
public function mapTo(string $class = stdClass::class): array
{
Expand All @@ -264,13 +267,13 @@ public function mapTo(string $class = stdClass::class): array
throw new UnknownContentTypeException(sprintf(
"The response is not a valid ES|QL result. I cannot mapTo(\"%s\")",
$class
));
));
}
$iterator = [];
$ncol = count($response['columns']);
foreach ($response['values'] as $value) {
$obj = new $class;
for ($i=0; $i < $ncol; $i++) {
$obj = new $class();
for ($i = 0; $i < $ncol; $i++) {
$field = Utility::formatVariableName($response['columns'][$i]['name']);
if ($class !== stdClass::class && !property_exists($obj, $field)) {
continue;
Expand Down Expand Up @@ -306,4 +309,4 @@ public function mapTo(string $class = stdClass::class): array
}
return $iterator;
}
}
}
Loading