Skip to content

Commit 3563fd7

Browse files
committed
feat: add new method in ElasticSearch class
1 parent 62d4bb5 commit 3563fd7

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

src/Response/Elasticsearch.php

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
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

1515
namespace Elastic\Elasticsearch\Response;
1616

@@ -36,11 +36,10 @@
3636
*/
3737
class 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+
}

tests/Response/ElasticsearchTest.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
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

1515
namespace Elastic\Elasticsearch\Tests\Response;
1616

@@ -26,6 +26,7 @@
2626
use PHPUnit\Framework\TestCase;
2727
use Psr\Http\Message\ResponseInterface;
2828
use stdClass;
29+
2930
class ElasticsearchTest extends TestCase
3031
{
3132
protected Psr17Factory $psr17Factory;
@@ -38,7 +39,7 @@ public function setUp(): void
3839
{
3940
$this->psr17Factory = new Psr17Factory();
4041
$this->elasticsearch = new Elasticsearch();
41-
42+
4243
$this->response200 = $this->psr17Factory->createResponse(200)
4344
->withHeader('X-Elastic-Product', 'Elasticsearch')
4445
->withHeader('Content-Type', 'application/json');
@@ -86,11 +87,8 @@ public function testAsBoolIsTrueWith200()
8687

8788
public function testAsBoolIsFalseWith400()
8889
{
89-
try {
90-
$this->elasticsearch->setResponse($this->response400);
91-
} catch (ClientResponseException $e) {
92-
$this->assertFalse($this->elasticsearch->asBool());
93-
}
90+
$this->elasticsearch->setResponse($this->response400, false);
91+
$this->assertFalse($this->elasticsearch->asBool());
9492
}
9593

9694
/**
@@ -141,9 +139,9 @@ public function testSetResponseWith400AndThrowFalseDoesNotThrowException()
141139
$this->elasticsearch->setResponse($this->response400, false);
142140
}
143141

144-
/**
145-
* @doesNotPerformAssertions
146-
*/
142+
/**
143+
* @doesNotPerformAssertions
144+
*/
147145
public function testSetResponseWith500AndThrowFalseDoesNotThrowException()
148146
{
149147
$this->elasticsearch->setResponse($this->response500, false);
@@ -317,4 +315,15 @@ public function testIsServerlessFalseIfNotServerlessResponse()
317315
$this->elasticsearch->setResponse($this->response200);
318316
$this->assertFalse($this->elasticsearch->isServerless());
319317
}
320-
}
318+
319+
public function testCacheIsClearedOnSetResponse()
320+
{
321+
$firstBody = $this->psr17Factory->createStream(json_encode(['foo' => 'bar']));
322+
$this->elasticsearch->setResponse($this->response200->withBody($firstBody));
323+
$this->assertSame('bar', $this->elasticsearch->asArray()['foo']);
324+
325+
$secondBody = $this->psr17Factory->createStream(json_encode(['foo' => 'baz']));
326+
$this->elasticsearch->setResponse($this->response200->withBody($secondBody));
327+
$this->assertSame('baz', $this->elasticsearch->asArray()['foo']);
328+
}
329+
}

0 commit comments

Comments
 (0)