Skip to content

Commit bbcdf9d

Browse files
authored
Switch to JSON form submission (#22)
* switch from query params to json form body * update guzzle * update version
1 parent fc55801 commit bbcdf9d

File tree

6 files changed

+48
-22
lines changed

6 files changed

+48
-22
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Refer to the table for PHP version compatibility:
1111

1212
| ChallongePHP Ver. | Compatible PHP |
1313
|----------|-------------|
14-
| ^4.0 | 8.0 -8.1 |
14+
| ^4.0 | 8.0 - 8.1 |
1515
| ^3.0 | 7.4 - 8.0 |
1616
| ^2.1 | 7.4 |
1717
| ^2.0 | 7.4 |
@@ -68,6 +68,6 @@ As the package is fully type-hinted, everything should be self documenting, howe
6868

6969
## Contact
7070
- [@Reflexgg](http://twitter.com/Reflexgg)
71-
- [@Kairuxo](http://twitter.com/Kairuxo)
71+
- [@rfxkairu](http://twitter.com/rfxkairu)
7272

7373
[Challonge]: <http://api.challonge.com/v1>

composer.lock

+15-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Challonge/Challonge.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Challonge
1414
* ChallongePHP version.
1515
* Required to pass into Challonge.
1616
*/
17-
protected string $version = '3.1';
17+
protected string $version = '4.0';
1818

1919
/**
2020
* PSR-18 compatible HTTP client wrapped in our wrapper.

src/Challonge/ClientWrapper.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function request(string $method, string $uri, array $content = []): array
5959
$method,
6060
$base_uri,
6161
$this->buildHeaders(),
62-
\http_build_query($content, '', '&'),
62+
json_encode($content),
6363
);
6464
$response = $this->client->sendRequest($request);
6565

@@ -74,7 +74,7 @@ protected function buildHeaders(): array
7474
{
7575
return [
7676
'Accept' => 'application/json',
77-
'Content-Type' => 'application/x-www-form-urlencoded',
77+
'Content-Type' => 'application/json',
7878
'User-Agent' => "ChallongePHP/{$this->version} ChallongePHP (https://github.com/teamreflex/ChallongePHP, {$this->version})",
7979
];
8080
}
@@ -153,6 +153,22 @@ public function setKey(string $key): void
153153
$this->key = $key;
154154
}
155155

156+
/**
157+
* @return bool
158+
*/
159+
public function getMapOptions(): bool
160+
{
161+
return $this->mapOptions;
162+
}
163+
164+
/**
165+
* @param bool $mapOptions
166+
*/
167+
public function setMapOptions(bool $mapOptions): void
168+
{
169+
$this->mapOptions = $mapOptions;
170+
}
171+
156172
/**
157173
* Get the underlying client.
158174
* @return ClientInterface

src/Challonge/DTO/Tournament.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,14 @@ public function addParticipant(array $options = []): Participant
297297
*/
298298
public function bulkAddParticipant(array $options = []): Collection
299299
{
300-
$response = $this->client->request('POST', "tournaments/{$this->id}/participants/bulk_add", $this->client->mapOptions($options, 'participant'));
300+
// have to bypass the mapOptions function as the format is a bit different
301+
if ($this->client->getMapOptions()) {
302+
$options = [
303+
'participants' => $options,
304+
];
305+
}
306+
307+
$response = $this->client->request('POST', "tournaments/{$this->id}/participants/bulk_add", $options);
301308
return Collection::make($response)
302309
->map(fn (array $participant) => Participant::fromResponse($this->client, $participant['participant']));
303310
}

tests/TournamentTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,10 @@ public function test_tournament_bulkadd_participant(): void
192192
json_decode(file_get_contents(__DIR__ . '/stubs/tournament_fetch.json'), true)['tournament']
193193
);
194194

195-
$response = $tournament->bulkAddParticipant();
195+
$response = $tournament->bulkAddParticipant([
196+
['name' => 'Team 1'],
197+
['name' => 'Team 2'],
198+
]);
196199

197200
$this->assertCount(3, $response);
198201
}

0 commit comments

Comments
 (0)