1010 * Elasticsearch B.V licenses this file to you under the MIT License.
1111 * See the LICENSE file in the project root for more information.
1212 */
13- declare (strict_types = 1 );
13+ declare (strict_types= 1 );
1414
1515namespace Elastic \Elasticsearch \Response ;
1616
3636 */
3737class Elasticsearch implements ElasticsearchInterface, ResponseInterface, ArrayAccess
3838{
39- const HEADER_CHECK = 'X-Elastic-Product ' ;
40- const PRODUCT_NAME = 'Elasticsearch ' ;
41-
4239 use ProductCheckTrait;
4340 use MessageResponseTrait;
41+ public const HEADER_CHECK = 'X-Elastic-Product ' ;
42+ public const PRODUCT_NAME = 'Elasticsearch ' ;
4443
4544 protected array $ asArray ;
4645 protected object $ asObject ;
@@ -67,6 +66,10 @@ public function setResponse(ResponseInterface $response, bool $throwException =
6766 // Check for Serverless response
6867 $ this ->serverless = $ this ->isServerlessResponse ($ response );
6968 $ this ->response = $ response ;
69+
70+ unset($ this ->asArray , $ this ->asObject );
71+ $ this ->asString = '' ;
72+
7073 $ status = $ response ->getStatusCode ();
7174 if ($ throwException && $ status > 399 && $ status < 500 ) {
7275 $ error = new ClientResponseException (
@@ -104,14 +107,14 @@ public function isServerless(): bool
104107 */
105108 public function asBool (): bool
106109 {
107- return $ this ->response ->getStatusCode () >=200 && $ this ->response ->getStatusCode () < 300 ;
110+ return $ this ->response ->getStatusCode () >= 200 && $ this ->response ->getStatusCode () < 300 ;
108111 }
109112
110113 /**
111114 * Converts the body content to array, if possible.
112115 * Otherwise, it throws an UnknownContentTypeException
113116 * if Content-Type is not specified or unknown.
114- *
117+ *
115118 * @throws UnknownContentTypeException
116119 */
117120 public function asArray (): array
@@ -147,7 +150,7 @@ public function asArray(): array
147150 * Converts the body content to object, if possible.
148151 * Otherwise, it throws an UnknownContentTypeException
149152 * if Content-Type is not specified or unknown.
150- *
153+ *
151154 * @throws UnknownContentTypeException
152155 */
153156 public function asObject (): object
@@ -197,7 +200,7 @@ public function __toString(): string
197200
198201 /**
199202 * Access the body content as object properties
200- *
203+ *
201204 * @see https://www.php.net/manual/en/language.oop5.overloading.php#object.get
202205 */
203206 public function __get ($ name )
@@ -207,17 +210,17 @@ public function __get($name)
207210
208211 /**
209212 * ArrayAccess interface
210- *
213+ *
211214 * @see https://www.php.net/manual/en/class.arrayaccess.php
212215 */
213216 public function offsetExists ($ offset ): bool
214217 {
215218 return isset ($ this ->asArray ()[$ offset ]);
216219 }
217-
220+
218221 /**
219222 * ArrayAccess interface
220- *
223+ *
221224 * @see https://www.php.net/manual/en/class.arrayaccess.php
222225 *
223226 * @return mixed
@@ -230,7 +233,7 @@ public function offsetGet($offset)
230233
231234 /**
232235 * ArrayAccess interface
233- *
236+ *
234237 * @see https://www.php.net/manual/en/class.arrayaccess.php
235238 */
236239 public function offsetSet ($ offset , $ value ): void
@@ -240,7 +243,7 @@ public function offsetSet($offset, $value): void
240243
241244 /**
242245 * ArrayAccess interface
243- *
246+ *
244247 * @see https://www.php.net/manual/en/class.arrayaccess.php
245248 */
246249 public function offsetUnset ($ offset ): void
@@ -251,11 +254,11 @@ public function offsetUnset($offset): void
251254 /**
252255 * Map the response body to an object of a specific class
253256 * by default the class is the PHP standard one (stdClass)
254- *
257+ *
255258 * This mapping works only for ES|QL results (with columns and values)
256259 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/esql.html
257- *
258- * @return object[]
260+ *
261+ * @return object[]
259262 */
260263 public function mapTo (string $ class = stdClass::class): array
261264 {
@@ -264,13 +267,13 @@ public function mapTo(string $class = stdClass::class): array
264267 throw new UnknownContentTypeException (sprintf (
265268 "The response is not a valid ES|QL result. I cannot mapTo( \"%s \") " ,
266269 $ class
267- ));
270+ ));
268271 }
269272 $ iterator = [];
270273 $ ncol = count ($ response ['columns ' ]);
271274 foreach ($ response ['values ' ] as $ value ) {
272- $ obj = new $ class ;
273- for ($ i= 0 ; $ i < $ ncol ; $ i ++) {
275+ $ obj = new $ class() ;
276+ for ($ i = 0 ; $ i < $ ncol ; $ i ++) {
274277 $ field = Utility::formatVariableName ($ response ['columns ' ][$ i ]['name ' ]);
275278 if ($ class !== stdClass::class && !property_exists ($ obj , $ field )) {
276279 continue ;
@@ -306,4 +309,4 @@ public function mapTo(string $class = stdClass::class): array
306309 }
307310 return $ iterator ;
308311 }
309- }
312+ }
0 commit comments