1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace guastallaigor\PhpBattlerite; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\RequestException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* PHP-Battlerite easy API |
9
|
|
|
* |
10
|
|
|
* @category Games |
11
|
|
|
* @package src |
12
|
|
|
* Main class |
13
|
|
|
* @author Igor Guastalla de Lima <[email protected]> |
14
|
|
|
* @copyright 2018 PHP Battlerite |
15
|
|
|
* @license MIT https://github.com/guastallaigor/php-battlerite/blob/master/LICENSE |
16
|
|
|
* @link https://github.com/guastallaigor/php-battlerite |
17
|
|
|
*/ |
18
|
|
|
class Main |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Guzzle Client variable to send all requests. |
22
|
|
|
* |
23
|
|
|
* @var object<GuzzleHttp\Client> |
24
|
|
|
*/ |
25
|
|
|
private $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* API Key of your development battlerite account. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $apiKey; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \guastallaigor\PhpBattlerite\Config |
36
|
|
|
*/ |
37
|
|
|
private $config; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Main constructor. |
41
|
|
|
* |
42
|
|
|
* @param \guastallaigor\PhpBattlerite\Config $config |
43
|
|
|
*/ |
44
|
4 |
|
public function __construct(Config $config) |
45
|
|
|
{ |
46
|
4 |
|
$this->config = $config; |
47
|
4 |
|
$this->client = new \GuzzleHttp\Client(); |
48
|
4 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Method to set your API Key provided by your Battlerite development account. |
52
|
|
|
* |
53
|
|
|
* @param string $apiKey |
54
|
|
|
* |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
4 |
|
public function setAPIKey($apiKey) |
58
|
|
|
{ |
59
|
4 |
|
$this->apiKey = $apiKey; |
60
|
4 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Function that is going to make all the requests you need. |
64
|
|
|
* |
65
|
|
|
* @param string $method |
66
|
|
|
* @param string $request |
67
|
|
|
* @param array $filter |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
4 |
|
public function sendRequest($method, $request, $filter = [], $global = true) |
72
|
|
|
{ |
73
|
4 |
|
$apiUrl = 'https://api.dc01.gamelockerapp.com/'; |
74
|
4 |
|
$shardsGlobal = 'shards/global/'; |
75
|
4 |
|
$url = $apiUrl . ($global ? $shardsGlobal . $request : $request); |
76
|
|
|
$header = [ |
77
|
4 |
|
'Authorization' => 'Bearer ' . $this->apiKey, |
78
|
4 |
|
'Accept' => 'application/vnd.api+json' |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
try { |
82
|
4 |
|
$response = $this->client->request( |
83
|
4 |
|
$method, |
84
|
4 |
|
$url, |
85
|
|
|
[ |
86
|
4 |
|
'query' => $filter, |
87
|
4 |
|
'headers' => $header, |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
|
91
|
4 |
|
return json_decode($response->getBody()->getContents()); |
92
|
|
|
} catch (RequestException $error) { |
93
|
|
|
$response = $this->statusCodeHandling($error); |
94
|
|
|
|
95
|
|
|
return $response; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get a single player request. |
101
|
|
|
* |
102
|
|
|
* @param string $id |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
1 |
|
public function getPlayer($id) |
107
|
|
|
{ |
108
|
1 |
|
return $this->sendRequest('GET', 'players/' . $id); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get a collection of players. |
113
|
|
|
* |
114
|
|
|
* @param string $ids |
115
|
|
|
* @param string $type |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
1 |
|
public function getPlayers($ids, $type = 'playerIds') |
120
|
|
|
{ |
121
|
1 |
|
$filter = ['filter['. $type .']' => $ids]; |
122
|
1 |
|
return $this->sendRequest('GET', 'players', $filter); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get a collection of teams. |
127
|
|
|
* |
128
|
|
|
* @param array $filters |
|
|
|
|
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function getTeams($filter) |
133
|
|
|
{ |
134
|
|
|
// need work |
135
|
|
|
return $this->sendRequest('GET', 'teams', $filter); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get Battlerite status. |
140
|
|
|
* |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
1 |
|
public function getStatus() |
144
|
|
|
{ |
145
|
1 |
|
return $this->sendRequest('GET', 'status', [], false); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get all the telemetry data. |
150
|
|
|
* |
151
|
|
|
* @param array $filters |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
1 |
|
public function getMatches($filters) |
156
|
|
|
{ |
157
|
1 |
|
$formattedFilter = []; |
158
|
1 |
|
foreach($filters as $key => $value) { |
159
|
1 |
|
if ($key === 'offset' || $key === 'limit') { |
160
|
1 |
|
$formattedFilter['page['. $key .']'] = $value; |
161
|
1 |
|
} else if ($key === 'sort') { |
162
|
1 |
|
$formattedFilter['sort'] = $value; |
163
|
|
|
} else { |
164
|
1 |
|
$formattedFilter['filter['. $key .']'] = $value; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
|
return $this->sendRequest('GET', 'matches', $formattedFilter); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Function to handle unexpected errors. |
173
|
|
|
* |
174
|
|
|
* @param RequestException $error |
175
|
|
|
* |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
protected function statusCodeHandling($error) |
179
|
|
|
{ |
180
|
|
|
return [ |
181
|
|
|
'statuscode' => $error->getResponse()->getStatusCode(), |
182
|
|
|
'error' => $error->getResponse()->getBody()->getContents(), |
183
|
|
|
]; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.