Skip to content

Commit a505827

Browse files
committed
Convert array parameter to comma-separated list of values
1 parent 8eb89f5 commit a505827

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/Traits/EndpointTrait.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
2727

2828
trait EndpointTrait
2929
{
30+
/**
31+
* Check if an array containts nested array
32+
*/
33+
private function isNestedArray(array $a): bool
34+
{
35+
foreach ($a as $v) {
36+
if (is_array($v)) {
37+
return true;
38+
}
39+
}
40+
return false;
41+
}
42+
3043
/**
3144
* Returns the URL with the query string from $params
3245
* extracting the array keys specified in $keys
@@ -36,7 +49,15 @@ protected function addQueryString(string $url, array $params, array $keys): stri
3649
$queryParams = [];
3750
foreach ($keys as $k) {
3851
if (isset($params[$k])) {
39-
$queryParams[$k] = is_bool($params[$k]) ? ($params[$k] ? 'true' : 'false') : $params[$k];
52+
// Convert to 'true' or 'false' string if bool
53+
if (is_bool($params[$k])) {
54+
$queryParams[$k] = $params[$k] ? 'true' : 'false';
55+
// Convert to comma-separated list if array
56+
} elseif (is_array($params[$k]) && $this->isNestedArray($params[$k]) === false) {
57+
$queryParams[$k] = implode(',', $params[$k]);
58+
} else {
59+
$queryParams[$k] = $params[$k];
60+
}
4061
}
4162
}
4263
if (empty($queryParams)) {

tests/Integration/BasicTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,29 @@ public function testIndexDocumentWithBodyAsString()
8888
return $response['_id'];
8989
}
9090

91+
public function testIndexStatistics()
92+
{
93+
$response = $this->client->indices()->stats([
94+
'index' => 'stocks',
95+
'metric' => 'completion,docs'
96+
]);
97+
$this->assertEquals(200, $response->getStatusCode());
98+
$this->assertNotNull($response['_all']['primaries']['completion']);
99+
$this->assertNotNull($response['_all']['primaries']['docs']);
100+
}
101+
102+
public function testIndexStatisticsWithExpandWildcardsAsArray()
103+
{
104+
$response = $this->client->indices()->stats([
105+
'index' => 'stocks',
106+
'metric' => 'completion,docs',
107+
'expand_wildcards' => ['open', 'closed']
108+
]);
109+
$this->assertEquals(200, $response->getStatusCode());
110+
$this->assertNotNull($response['_all']['primaries']['completion']);
111+
$this->assertNotNull($response['_all']['primaries']['docs']);
112+
}
113+
91114
/**
92115
* @depends testIndexDocumentWithBodyAsString
93116
*/

0 commit comments

Comments
 (0)