Skip to content

Commit ee9ff4a

Browse files
committed
Revert "feat: add new method in ElasticSearch class"
This reverts commit 3563fd7.
1 parent 3338a82 commit ee9ff4a

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

src/Response/Elasticsearch.php

Lines changed: 20 additions & 23 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,10 +36,11 @@
3636
*/
3737
class Elasticsearch implements ElasticsearchInterface, ResponseInterface, ArrayAccess
3838
{
39+
const HEADER_CHECK = 'X-Elastic-Product';
40+
const PRODUCT_NAME = 'Elasticsearch';
41+
3942
use ProductCheckTrait;
4043
use MessageResponseTrait;
41-
public const HEADER_CHECK = 'X-Elastic-Product';
42-
public const PRODUCT_NAME = 'Elasticsearch';
4344

4445
protected array $asArray;
4546
protected object $asObject;
@@ -66,10 +67,6 @@ public function setResponse(ResponseInterface $response, bool $throwException =
6667
// Check for Serverless response
6768
$this->serverless = $this->isServerlessResponse($response);
6869
$this->response = $response;
69-
70-
unset($this->asArray, $this->asObject);
71-
$this->asString = '';
72-
7370
$status = $response->getStatusCode();
7471
if ($throwException && $status > 399 && $status < 500) {
7572
$error = new ClientResponseException(
@@ -107,14 +104,14 @@ public function isServerless(): bool
107104
*/
108105
public function asBool(): bool
109106
{
110-
return $this->response->getStatusCode() >= 200 && $this->response->getStatusCode() < 300;
107+
return $this->response->getStatusCode() >=200 && $this->response->getStatusCode() < 300;
111108
}
112109

113110
/**
114111
* Converts the body content to array, if possible.
115112
* Otherwise, it throws an UnknownContentTypeException
116113
* if Content-Type is not specified or unknown.
117-
*
114+
*
118115
* @throws UnknownContentTypeException
119116
*/
120117
public function asArray(): array
@@ -150,7 +147,7 @@ public function asArray(): array
150147
* Converts the body content to object, if possible.
151148
* Otherwise, it throws an UnknownContentTypeException
152149
* if Content-Type is not specified or unknown.
153-
*
150+
*
154151
* @throws UnknownContentTypeException
155152
*/
156153
public function asObject(): object
@@ -200,7 +197,7 @@ public function __toString(): string
200197

201198
/**
202199
* Access the body content as object properties
203-
*
200+
*
204201
* @see https://www.php.net/manual/en/language.oop5.overloading.php#object.get
205202
*/
206203
public function __get($name)
@@ -210,17 +207,17 @@ public function __get($name)
210207

211208
/**
212209
* ArrayAccess interface
213-
*
210+
*
214211
* @see https://www.php.net/manual/en/class.arrayaccess.php
215212
*/
216213
public function offsetExists($offset): bool
217214
{
218215
return isset($this->asArray()[$offset]);
219216
}
220-
217+
221218
/**
222219
* ArrayAccess interface
223-
*
220+
*
224221
* @see https://www.php.net/manual/en/class.arrayaccess.php
225222
*
226223
* @return mixed
@@ -233,7 +230,7 @@ public function offsetGet($offset)
233230

234231
/**
235232
* ArrayAccess interface
236-
*
233+
*
237234
* @see https://www.php.net/manual/en/class.arrayaccess.php
238235
*/
239236
public function offsetSet($offset, $value): void
@@ -243,7 +240,7 @@ public function offsetSet($offset, $value): void
243240

244241
/**
245242
* ArrayAccess interface
246-
*
243+
*
247244
* @see https://www.php.net/manual/en/class.arrayaccess.php
248245
*/
249246
public function offsetUnset($offset): void
@@ -254,11 +251,11 @@ public function offsetUnset($offset): void
254251
/**
255252
* Map the response body to an object of a specific class
256253
* by default the class is the PHP standard one (stdClass)
257-
*
254+
*
258255
* This mapping works only for ES|QL results (with columns and values)
259256
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/esql.html
260-
*
261-
* @return object[]
257+
*
258+
* @return object[]
262259
*/
263260
public function mapTo(string $class = stdClass::class): array
264261
{
@@ -267,13 +264,13 @@ public function mapTo(string $class = stdClass::class): array
267264
throw new UnknownContentTypeException(sprintf(
268265
"The response is not a valid ES|QL result. I cannot mapTo(\"%s\")",
269266
$class
270-
));
267+
));
271268
}
272269
$iterator = [];
273270
$ncol = count($response['columns']);
274271
foreach ($response['values'] as $value) {
275-
$obj = new $class();
276-
for ($i = 0; $i < $ncol; $i++) {
272+
$obj = new $class;
273+
for ($i=0; $i < $ncol; $i++) {
277274
$field = Utility::formatVariableName($response['columns'][$i]['name']);
278275
if ($class !== stdClass::class && !property_exists($obj, $field)) {
279276
continue;
@@ -309,4 +306,4 @@ public function mapTo(string $class = stdClass::class): array
309306
}
310307
return $iterator;
311308
}
312-
}
309+
}

tests/Response/ElasticsearchTest.php

Lines changed: 11 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\Tests\Response;
1616

@@ -26,7 +26,6 @@
2626
use PHPUnit\Framework\TestCase;
2727
use Psr\Http\Message\ResponseInterface;
2828
use stdClass;
29-
3029
class ElasticsearchTest extends TestCase
3130
{
3231
protected Psr17Factory $psr17Factory;
@@ -39,7 +38,7 @@ public function setUp(): void
3938
{
4039
$this->psr17Factory = new Psr17Factory();
4140
$this->elasticsearch = new Elasticsearch();
42-
41+
4342
$this->response200 = $this->psr17Factory->createResponse(200)
4443
->withHeader('X-Elastic-Product', 'Elasticsearch')
4544
->withHeader('Content-Type', 'application/json');
@@ -87,8 +86,11 @@ public function testAsBoolIsTrueWith200()
8786

8887
public function testAsBoolIsFalseWith400()
8988
{
90-
$this->elasticsearch->setResponse($this->response400, false);
91-
$this->assertFalse($this->elasticsearch->asBool());
89+
try {
90+
$this->elasticsearch->setResponse($this->response400);
91+
} catch (ClientResponseException $e) {
92+
$this->assertFalse($this->elasticsearch->asBool());
93+
}
9294
}
9395

9496
/**
@@ -139,9 +141,9 @@ public function testSetResponseWith400AndThrowFalseDoesNotThrowException()
139141
$this->elasticsearch->setResponse($this->response400, false);
140142
}
141143

142-
/**
143-
* @doesNotPerformAssertions
144-
*/
144+
/**
145+
* @doesNotPerformAssertions
146+
*/
145147
public function testSetResponseWith500AndThrowFalseDoesNotThrowException()
146148
{
147149
$this->elasticsearch->setResponse($this->response500, false);
@@ -315,15 +317,4 @@ public function testIsServerlessFalseIfNotServerlessResponse()
315317
$this->elasticsearch->setResponse($this->response200);
316318
$this->assertFalse($this->elasticsearch->isServerless());
317319
}
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-
}
320+
}

0 commit comments

Comments
 (0)