Skip to content

Commit c47a91d

Browse files
committed
Basic tournament/participant/match viewing
1 parent 7cf0b96 commit c47a91d

15 files changed

+345
-0
lines changed

.editorconfig

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ChallongePHP.sublime-project
2+
ChallongePHP.sublime-workspace
3+
/vendor/
4+
5+
composer.lock
6+
7+
index.php

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Kyle Ward <kairu@team-reflex.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "team-reflex/challonge-php",
3+
"description": "Unofficial API to interface with the bracket generator Challonge.",
4+
"require": {
5+
"guzzlehttp/guzzle": "^5.0"
6+
},
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Kyle Ward",
11+
"email": "kairu@team-reflex.com"
12+
}
13+
],
14+
"autoload": {
15+
"psr-4": {
16+
"Reflex\\Challonge\\": "src/Challonge"
17+
}
18+
}
19+
}

src/Challonge/Challonge.php

+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
<?php
2+
3+
namespace Reflex\Challonge;
4+
5+
use GuzzleHttp\Client;
6+
use Reflex\Challonge\Models\Match;
7+
use Reflex\Challonge\Models\Tournament;
8+
use Reflex\Challonge\Models\Participant;
9+
use Reflex\Challonge\Exceptions\ServerException;
10+
use Reflex\Challonge\Exceptions\NotFoundException;
11+
use Reflex\Challonge\Exceptions\ValidationException;
12+
use Reflex\Challonge\Exceptions\UnauthorizedException;
13+
use Reflex\Challonge\Exceptions\InvalidFormatException;
14+
15+
class Challonge
16+
{
17+
/**
18+
* ChallongePHP version.
19+
*/
20+
const VERSION = '1.0';
21+
22+
/**
23+
* Challonge API key.
24+
* @var string
25+
*/
26+
private $api_key;
27+
28+
/**
29+
* Instantiate an instance with the API key.
30+
* @param string $api_key
31+
*/
32+
public function __construct($api_key = '')
33+
{
34+
$this->api_key = $api_key;
35+
}
36+
37+
/**
38+
* Build any headers the requests need.
39+
* @return array
40+
*/
41+
private function buildHeaders()
42+
{
43+
return [
44+
'User-Agent' => 'ChallongePHP/' . self::VERSION . ' ChallongePHP (https://github.com/teamreflex/ChallongePHP, ' . self::VERSION . ')'
45+
];
46+
}
47+
48+
/**
49+
* Base function for all API requests.
50+
* @param string $path
51+
* @param array $params
52+
* @param string $method
53+
* @return Response
54+
*/
55+
private function makeCall($path, $params = [], $method = 'get')
56+
{
57+
if (empty($this->api_key)) {
58+
throw new UnauthorizedException('Must set an API key.');
59+
}
60+
61+
$base_uri = "https://api.challonge.com/v1/{$path}.json";
62+
$client = new Client();
63+
64+
$request = $client->createRequest($method, $base_uri, [
65+
'headers' => $this->buildHeaders(),
66+
'exceptions' => false,
67+
]);
68+
69+
$query = $request->getQuery();
70+
$query->set('api_key', $this->api_key);
71+
72+
$response = $client->send($request);
73+
74+
return $this->handleErrors($response);
75+
}
76+
77+
/**
78+
* Handles the response and throws errors accordingly.
79+
*/
80+
private function handleErrors($response)
81+
{
82+
switch ($response->getStatusCode()) {
83+
case 200:
84+
return $response->json();
85+
break;
86+
case 401:
87+
throw new UnauthorizedException('Unauthorized (Invalid API key or insufficient permissions)');
88+
break;
89+
case 404:
90+
throw new NotFoundException('Object not found within your account scope');
91+
break;
92+
case 406:
93+
throw new InvalidFormatException('Requested format is not supported - request JSON or XML only');
94+
break;
95+
case 422:
96+
throw new ValidationException('Validation error(s) for create or update method');
97+
break;
98+
case 500:
99+
throw new ServerException('Something went wrong on Challonge\'s end');
100+
break;
101+
default:
102+
$errors = $response->json()['errors'];
103+
throw new UnexpectedErrorException($errors);
104+
break;
105+
}
106+
}
107+
108+
/**
109+
* Retrieve a set of tournaments created with your account.
110+
* @return array
111+
*/
112+
public function getTournaments() {
113+
$response = $this->makeCall('tournaments');
114+
115+
$tournaments = [];
116+
foreach ($response as $tourney) {
117+
$tournaments[] = new Tournament($tourney['tournament']);
118+
}
119+
120+
return $tournaments;
121+
}
122+
123+
/**
124+
* Retrieve a single tournament record created with your account.
125+
* @param string $tournament
126+
* @return Tournament
127+
*/
128+
public function getTournament($tournament)
129+
{
130+
$response = $this->makeCall("tournaments/{$tournament}");
131+
return new Tournament($response['tournament']);
132+
}
133+
134+
/**
135+
* Retrieve a tournament's participant list.
136+
* @param string $tournament
137+
* @return array
138+
*/
139+
public function getParticipants($tournament)
140+
{
141+
$response = $this->makeCall("tournaments/{$tournament}/participants");
142+
143+
$participants = [];
144+
foreach ($response as $team) {
145+
$participants[] = new Participant($team['participant']);
146+
}
147+
148+
return $participants;
149+
}
150+
151+
/**
152+
* Retrieve a single participant record for a tournament.
153+
* @param string $tournament
154+
* @param string $participant
155+
* @return array
156+
*/
157+
public function getParticipant($tournament, $participant)
158+
{
159+
$response = $this->makeCall("tournaments/{$tournament}/participants/{$participant}");
160+
161+
$participant = new Participant($response['participant']);
162+
163+
return $participant;
164+
}
165+
166+
/**
167+
* Retrieve a tournament's match list.
168+
* @param string $tournament
169+
* @return array
170+
*/
171+
public function getMatches($tournament)
172+
{
173+
$response = $this->makeCall("tournaments/{$tournament}/matches");
174+
175+
$matches = [];
176+
foreach ($response as $match) {
177+
$matches[] = new Match($match['match']);
178+
}
179+
180+
return $matches;
181+
}
182+
183+
/**
184+
* Retrieve a single match record for a tournament.
185+
* @param string $tournament
186+
* @param string $match
187+
* @return array
188+
*/
189+
public function getMatch($tournament, $match)
190+
{
191+
$response = $this->makeCall("tournaments/{$tournament}/matches/{$match}");
192+
193+
$match = new Match($response['match']);
194+
195+
return $match;
196+
}
197+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Reflex\Challonge\Exceptions;
4+
5+
class InvalidFormatException extends \Exception
6+
{
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Reflex\Challonge\Exceptions;
4+
5+
class NotFoundException extends \Exception
6+
{
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Reflex\Challonge\Exceptions;
4+
5+
class ServerException extends \Exception
6+
{
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Reflex\Challonge\Exceptions;
4+
5+
class UnauthorizedException extends \Exception
6+
{
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Reflex\Challonge\Exceptions;
4+
5+
class UnexpectedErrorException extends \Exception
6+
{
7+
public $errors;
8+
9+
public function __construct($errors)
10+
{
11+
$this->errors = $errors;
12+
}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Reflex\Challonge\Exceptions;
4+
5+
class ValidationException extends \Exception
6+
{
7+
}

src/Challonge/Model.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Reflex\Challonge;
4+
5+
class Model
6+
{
7+
public function __construct($params = array())
8+
{
9+
foreach ($params as $key=>$value) {
10+
$this->{$key} = $value;
11+
}
12+
}
13+
}

src/Challonge/Models/Match.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Reflex\Challonge\Models;
4+
5+
use Reflex\Challonge\Model;
6+
7+
class Match extends Model
8+
{
9+
10+
}

src/Challonge/Models/Participant.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Reflex\Challonge\Models;
4+
5+
use Reflex\Challonge\Model;
6+
7+
class Participant extends Model
8+
{
9+
10+
}

src/Challonge/Models/Tournament.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Reflex\Challonge\Models;
4+
5+
use Reflex\Challonge\Model;
6+
7+
class Tournament extends Model
8+
{
9+
10+
}

0 commit comments

Comments
 (0)