Skip to content

feat: Build URL with multiple instances of the same param #136

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ echo $response->body();
echo $response->headers();
```

#### GET with array of values

```
$query_params = [
'aggregated_by' => 'month',
'subusers' => ['one', 'two', 'three'],
'start_date' => '2019-01-01',
'end_date' => '2019-01-31',
];
$request_headers = ['X-Mock: 200'];
$retryOnLimit = true;
$response = $client->subusers()->stats()->get(null, $query_params, $request_headers, $retryOnLimit);
echo $response->statusCode();
echo $response->body();
echo $response->headers();
```

<a name="delete"></a>
## DELETE

Expand Down
5 changes: 4 additions & 1 deletion lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,13 +346,16 @@ public function setIsConcurrentRequest($isConcurrent)
*
* @param array $queryParams an array of all the query parameters
*
* Nested arrays will resolve to multiple instances of the same parameter
*
* @return string
*/
private function buildUrl($queryParams = null)
{
$path = '/' . implode('/', $this->path);
if (isset($queryParams)) {
$path .= '?' . http_build_query($queryParams);
// Regex replaces `[0]=`, `[1]=`, etc. with `=`.
$path .= '?' . preg_replace('/%5B(?:\d|[1-9]\d+)%5D=/', '=', http_build_query($queryParams));
}

return sprintf('%s%s%s', $this->host, $this->version ?: '', $path);
Expand Down
16 changes: 16 additions & 0 deletions test/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,22 @@ public function testMakeRequestWithUntrustedRootCert()
$client->makeRequest('GET', 'https://untrusted-root.badssl.com/');
}

public function testFormRepeatUrlArgs()
{
$client = new Client('https://localhost:4010');

$testParams = [
'thing' => 'stuff',
'foo' => [
'bar',
'bat',
'baz',
],
];
$result = $this->callMethod($client, 'buildUrl', [$testParams]);
$this->assertEquals($result, 'https://localhost:4010/?thing=stuff&foo=bar&foo=bat&foo=baz');
}

/**
* @param object $obj
* @param string $name
Expand Down