Skip to content

Commit 58b6cad

Browse files
committed
feat: add query handling methods to Request class
1 parent 5a4f1dc commit 58b6cad

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/http-client/src/Request.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,22 @@ public function withData(array $data): Request
198198
return $this;
199199
}
200200

201+
public function query(): array
202+
{
203+
parse_str($this->request->getUri()->getQuery(), $query);
204+
205+
return $query;
206+
}
207+
208+
public function withQuery(array $query = []): Request
209+
{
210+
$new = $this->request->getUri()->withQuery(http_build_query(array_merge($this->query(), $query)));
211+
212+
$this->request = $this->request->withUri($new);
213+
214+
return $this;
215+
}
216+
201217
/**
202218
* Get the underlying PSR compliant request instance.
203219
*/

tests/ApiClient/ApiRequestTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,11 @@ public function testWithUserAgent(): void
176176

177177
$this->assertSame([$userAgent], $request->toPsrRequest()->getHeader('User-Agent'));
178178
}
179+
180+
public function testWithQuery(): void
181+
{
182+
$request = $this->request->withQuery(['param1' => 'value1', 'param2' => 'value2']);
183+
184+
$this->assertSame('param1=value1&param2=value2', $request->toPsrRequest()->getUri()->getQuery());
185+
}
179186
}

0 commit comments

Comments
 (0)