|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PoLaKoSz\CoC_API |
|
4
|
|
|
{ |
|
5
|
|
|
use PoLaKoSz\CoC_API\DataAccessLayer\WebClient; |
|
6
|
|
|
use PoLaKoSz\CoC_API\Models\DetailedClan; |
|
7
|
|
|
use PoLaKoSz\CoC_API\Models\DetailedClanPlayer; |
|
8
|
|
|
use PoLaKoSz\CoC_API\Models\League; |
|
9
|
|
|
use PoLaKoSz\CoC_API\Models\Location; |
|
10
|
|
|
use PoLaKoSz\CoC_API\Models\LocationClan; |
|
11
|
|
|
use PoLaKoSz\CoC_API\Models\LocationPlayer; |
|
12
|
|
|
use PoLaKoSz\CoC_API\Models\LocationVersusClan; |
|
13
|
|
|
use PoLaKoSz\CoC_API\Models\LocationVersusPlayer; |
|
14
|
|
|
use PoLaKoSz\CoC_API\Models\Paging; |
|
15
|
|
|
use PoLaKoSz\CoC_API\Models\Player; |
|
16
|
|
|
use PoLaKoSz\CoC_API\Models\SearchClan; |
|
17
|
|
|
use PoLaKoSz\CoC_API\Models\SearchFilter; |
|
18
|
|
|
use PoLaKoSz\CoC_API\Models\Season; |
|
19
|
|
|
use PoLaKoSz\CoC_API\Models\SeasonPlayer; |
|
20
|
|
|
use PoLaKoSz\CoC_API\Models\SeasonPeriod; |
|
21
|
|
|
use PoLaKoSz\CoC_API\Models\Warlog; |
|
22
|
|
|
use PoLaKoSz\CoC_API\Models\WarDetails; |
|
23
|
|
|
use \Exception; |
|
24
|
|
|
|
|
25
|
|
|
class ClashOfClansApi |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var WebClient to perform requests to the Clash Of Clans API |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $webClient; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string $apikey Every call to the Clash Of Clans API needs to contain an Api Key |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(string $apiKey) |
|
36
|
|
|
{ |
|
37
|
|
|
if ( strlen($apiKey) == 0 ) |
|
38
|
|
|
throw new Exception('$apiKey cannot be null!'); |
|
39
|
|
|
|
|
40
|
|
|
$this->webClient = new WebClient($apiKey); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string $tag The player's tag (with the hasttag) |
|
45
|
|
|
* @return Player |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getPlayerByTag(string $tag) |
|
48
|
|
|
{ |
|
49
|
|
|
$response = $this->webClient->sendRequest('/players/' . $tag); |
|
50
|
|
|
|
|
51
|
|
|
return new Player($response); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The method return all existing league from the game |
|
56
|
|
|
* |
|
57
|
|
|
* @param SearchFilter $filter |
|
58
|
|
|
* @return array of League objects |
|
59
|
|
|
*/ |
|
60
|
|
View Code Duplication |
public function getLeagues(SearchFilter $filter = null) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$response = $this->webClient->sendRequest('/leagues' . $this->filterAppendToUrl($filter)); |
|
63
|
|
|
|
|
64
|
|
|
for ($index = 0; $index < count($response->items); $index++) |
|
|
|
|
|
|
65
|
|
|
$response->items[$index] = new League($response->items[$index]); |
|
66
|
|
|
|
|
67
|
|
|
$response->paging = new Paging($response->paging); |
|
68
|
|
|
|
|
69
|
|
|
return $response; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get every information about the given league |
|
74
|
|
|
* |
|
75
|
|
|
* @var int $leagueId id of the League |
|
76
|
|
|
* @return League object |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getLeagueById(int $leagueId) |
|
79
|
|
|
{ |
|
80
|
|
|
$response = $this->webClient->sendRequest('/leagues/' . $leagueId); |
|
81
|
|
|
|
|
82
|
|
|
return new League($response); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get a league seasons. Note that league season information is available only for Legend League |
|
87
|
|
|
* |
|
88
|
|
|
* @var int $leagueId id of the League |
|
89
|
|
|
* @param SearchFilter $filter |
|
90
|
|
|
* @return array of Season objects |
|
91
|
|
|
*/ |
|
92
|
|
View Code Duplication |
public function getLeagueSeasons(int $leagueId, SearchFilter $filter = null) |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
$response = $this->webClient->sendRequest('/leagues/' . $leagueId . '/seasons' . $this->filterAppendToUrl($filter)); |
|
95
|
|
|
|
|
96
|
|
|
for ($index = 0; $index < count($response->items); $index++) |
|
|
|
|
|
|
97
|
|
|
$response->items[$index] = new Season($response->items[$index]); |
|
98
|
|
|
|
|
99
|
|
|
$response->paging = new Paging($response->paging); |
|
100
|
|
|
|
|
101
|
|
|
return $response; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get a league season rankings. Note that league season information is available only for Legend League (ID: 29000022) |
|
106
|
|
|
* |
|
107
|
|
|
* @param int $leagueId |
|
108
|
|
|
* @param SeasonPeriod $seasonId |
|
109
|
|
|
* @param SearchFilter $filter |
|
110
|
|
|
* @return array of SeasonPlayer objects |
|
111
|
|
|
*/ |
|
112
|
|
View Code Duplication |
public function getLeagueSeason(int $leagueId, SeasonPeriod $seasonId, SearchFilter $filter = null) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
$url = '/leagues/' . $leagueId . '/seasons/' . $seasonId . $this->filterAppendToUrl($filter); |
|
115
|
|
|
|
|
116
|
|
|
$response = $this->webClient->sendRequest($url); |
|
117
|
|
|
|
|
118
|
|
|
for ($index = 0; $index < count($response->items); $index++) |
|
|
|
|
|
|
119
|
|
|
$response->items[$index] = new SeasonPlayer($response->items[$index]); |
|
120
|
|
|
|
|
121
|
|
|
$response->paging = new Paging($response->paging); |
|
122
|
|
|
|
|
123
|
|
|
return $response; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* List all available locations |
|
128
|
|
|
* |
|
129
|
|
|
* @param SearchFilter $filter |
|
130
|
|
|
* @return array of Location objects |
|
131
|
|
|
*/ |
|
132
|
|
View Code Duplication |
public function getLocations(SearchFilter $filter = null) |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
$url = '/locations' . $this->filterAppendToUrl($filter); |
|
135
|
|
|
|
|
136
|
|
|
$response = $this->webClient->sendRequest($url); |
|
137
|
|
|
|
|
138
|
|
|
for ($index = 0; $index < count($response->items); $index++) |
|
|
|
|
|
|
139
|
|
|
$response->items[$index] = new Location($response->items[$index]); |
|
140
|
|
|
|
|
141
|
|
|
$response->paging = new Paging($response->paging); |
|
142
|
|
|
|
|
143
|
|
|
return $response; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Get information about specific location |
|
148
|
|
|
* |
|
149
|
|
|
* @param int $locationId |
|
150
|
|
|
* @return Location |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getLocationById(int $locationId) |
|
153
|
|
|
{ |
|
154
|
|
|
$url = '/locations/' . $locationId; |
|
155
|
|
|
|
|
156
|
|
|
$response = $this->webClient->sendRequest($url); |
|
157
|
|
|
|
|
158
|
|
|
return new Location($response); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Get clan rankings for a specific location |
|
163
|
|
|
* |
|
164
|
|
|
* @param int $locationId |
|
165
|
|
|
* @param SearchFilter $filter |
|
166
|
|
|
* @return array of LocationClan objects |
|
167
|
|
|
*/ |
|
168
|
|
View Code Duplication |
public function getLocationClanRankings(int $locationId, SearchFilter $filter = null) |
|
|
|
|
|
|
169
|
|
|
{ |
|
170
|
|
|
$url = '/locations/' . $locationId . '/rankings/clans' . $this->filterAppendToUrl($filter); |
|
171
|
|
|
|
|
172
|
|
|
$response = $this->webClient->sendRequest($url); |
|
173
|
|
|
|
|
174
|
|
|
for ($index = 0; $index < count($response->items); $index++) |
|
|
|
|
|
|
175
|
|
|
$response->items[$index] = new LocationClan($response->items[$index]); |
|
176
|
|
|
|
|
177
|
|
|
$response->paging = new Paging($response->paging); |
|
178
|
|
|
|
|
179
|
|
|
return $response; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Get player rankings for a specific location |
|
184
|
|
|
* |
|
185
|
|
|
* @param int $locationId |
|
186
|
|
|
* @param SearchFilter $filter |
|
187
|
|
|
* @return array of LocationPlayer objects |
|
188
|
|
|
*/ |
|
189
|
|
View Code Duplication |
public function getLocationPlayerRankings(int $locationId, SearchFilter $filter = null) |
|
|
|
|
|
|
190
|
|
|
{ |
|
191
|
|
|
$url = '/locations/' . $locationId . '/rankings/players' . $this->filterAppendToUrl($filter); |
|
192
|
|
|
|
|
193
|
|
|
$response = $this->webClient->sendRequest($url); |
|
194
|
|
|
|
|
195
|
|
|
for ($index = 0; $index < count($response->items); $index++) |
|
|
|
|
|
|
196
|
|
|
$response->items[$index] = new LocationPlayer($response->items[$index]); |
|
197
|
|
|
|
|
198
|
|
|
$response->paging = new Paging($response->paging); |
|
199
|
|
|
|
|
200
|
|
|
return $response; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Get clan versus rankings for a specific location |
|
205
|
|
|
* |
|
206
|
|
|
* @param int $locationId |
|
207
|
|
|
* @param SearchFilter $filter |
|
208
|
|
|
* @return array of LocationVersusClan objects |
|
209
|
|
|
*/ |
|
210
|
|
View Code Duplication |
public function getLocationClanVersusRankings(int $locationId, SearchFilter $filter = null) |
|
|
|
|
|
|
211
|
|
|
{ |
|
212
|
|
|
$url = '/locations/' . $locationId . '/rankings/clans-versus' . $this->filterAppendToUrl($filter); |
|
213
|
|
|
|
|
214
|
|
|
$response = $this->webClient->sendRequest($url); |
|
215
|
|
|
|
|
216
|
|
|
for ($index = 0; $index < count($response->items); $index++) |
|
|
|
|
|
|
217
|
|
|
$response->items[$index] = new LocationVersusClan($response->items[$index]); |
|
218
|
|
|
|
|
219
|
|
|
$response->paging = new Paging($response->paging); |
|
220
|
|
|
|
|
221
|
|
|
return $response; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Get player rankings for a specific location |
|
226
|
|
|
* |
|
227
|
|
|
* @param int $locationId |
|
228
|
|
|
* @param SearchFilter $filter |
|
229
|
|
|
* @return array of LocationVersusPlayer objects |
|
230
|
|
|
*/ |
|
231
|
|
View Code Duplication |
public function getLocationPlayerVersusRankings(int $locationId, SearchFilter $filter = null) |
|
|
|
|
|
|
232
|
|
|
{ |
|
233
|
|
|
$url = '/locations/' . $locationId . '/rankings/players-versus' . $this->filterAppendToUrl($filter); |
|
234
|
|
|
|
|
235
|
|
|
$response = $this->webClient->sendRequest($url); |
|
236
|
|
|
|
|
237
|
|
|
for ($index = 0; $index < count($response->items); $index++) |
|
|
|
|
|
|
238
|
|
|
$response->items[$index] = new LocationVersusPlayer($response->items[$index]); |
|
239
|
|
|
|
|
240
|
|
|
$response->paging = new Paging($response->paging); |
|
241
|
|
|
|
|
242
|
|
|
return $response; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Search all clans by name and/or filtering the results using various criteria |
|
247
|
|
|
* |
|
248
|
|
|
* @param ClanSearchFilter $filter |
|
249
|
|
|
* @return array of SearchClan objects |
|
250
|
|
|
*/ |
|
251
|
|
View Code Duplication |
public function getClans(ClanSearchFilter $filter = null) |
|
|
|
|
|
|
252
|
|
|
{ |
|
253
|
|
|
$url = '/clans' . $this->filterAppendToUrl($filter); |
|
254
|
|
|
|
|
255
|
|
|
$response = $this->webClient->sendRequest($url); |
|
256
|
|
|
|
|
257
|
|
|
for ($index = 0; $index < count($response->items); $index++) |
|
|
|
|
|
|
258
|
|
|
$response->items[$index] = new SearchClan($response->items[$index]); |
|
259
|
|
|
|
|
260
|
|
|
$response->paging = new Paging($response->paging); |
|
261
|
|
|
|
|
262
|
|
|
return $response; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Get information about a single clan by clan tag |
|
267
|
|
|
* |
|
268
|
|
|
* @param string $clanTag |
|
269
|
|
|
* @return DetailedClan |
|
270
|
|
|
*/ |
|
271
|
|
|
public function getClanyByTag(string $clanTag) |
|
272
|
|
|
{ |
|
273
|
|
|
$url = '/clans/' . $clanTag; |
|
274
|
|
|
|
|
275
|
|
|
$response = $this->webClient->sendRequest($url); |
|
276
|
|
|
|
|
277
|
|
|
return new DetailedClan($response); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Get information about a single clan by clan tag |
|
282
|
|
|
* |
|
283
|
|
|
* @param string $clanTag |
|
284
|
|
|
* @param SearchFilter $filter |
|
285
|
|
|
* @return array of DetailedClanPlayer objects |
|
286
|
|
|
*/ |
|
287
|
|
View Code Duplication |
public function getClanMembers(string $clanTag, SearchFilter $filter = null) |
|
|
|
|
|
|
288
|
|
|
{ |
|
289
|
|
|
$url = '/clans/' . $clanTag . '/members' . $this->filterAppendToUrl($filter); |
|
290
|
|
|
|
|
291
|
|
|
$response = $this->webClient->sendRequest($url); |
|
292
|
|
|
|
|
293
|
|
|
for ($index = 0; $index < count($response->items); $index++) |
|
|
|
|
|
|
294
|
|
|
$response->items[$index] = new DetailedClanPlayer($response->items[$index]); |
|
295
|
|
|
|
|
296
|
|
|
$response->paging = new Paging($response->paging); |
|
297
|
|
|
|
|
298
|
|
|
return $response; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* Get information about a single clan by clan tag |
|
303
|
|
|
* |
|
304
|
|
|
* @param string $clanTag |
|
305
|
|
|
* @param SearchFilter $filter |
|
306
|
|
|
* @return array of DetailedClanPlayer objects |
|
307
|
|
|
*/ |
|
308
|
|
View Code Duplication |
public function getClanWarlog(string $clanTag, SearchFilter $filter = null) |
|
|
|
|
|
|
309
|
|
|
{ |
|
310
|
|
|
$url = '/clans/' . $clanTag . '/warlog' . $this->filterAppendToUrl($filter); |
|
311
|
|
|
|
|
312
|
|
|
$response = $this->webClient->sendRequest($url); |
|
313
|
|
|
|
|
314
|
|
|
for ($index = 0; $index < count($response->items); $index++) |
|
|
|
|
|
|
315
|
|
|
$response->items[$index] = new Warlog($response->items[$index]); |
|
316
|
|
|
|
|
317
|
|
|
$response->paging = new Paging($response->paging); |
|
318
|
|
|
|
|
319
|
|
|
return $response; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Get information about a single clan by clan tag |
|
324
|
|
|
* |
|
325
|
|
|
* @param string $clanTag |
|
326
|
|
|
* @return array of DetailedClanPlayer objects |
|
327
|
|
|
*/ |
|
328
|
|
|
public function getClanCurrentWar(string $clanTag) |
|
329
|
|
|
{ |
|
330
|
|
|
$url = '/clans/' . $clanTag . '/currentwar'; |
|
331
|
|
|
|
|
332
|
|
|
$response = $this->webClient->sendRequest($url); |
|
333
|
|
|
|
|
334
|
|
|
return new WarDetails( $response ); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* @param SearchFilter or ClanSearchFilter $filter |
|
339
|
|
|
* @return string |
|
340
|
|
|
*/ |
|
341
|
|
|
private function filterAppendToUrl($filter = null) |
|
342
|
|
|
{ |
|
343
|
|
|
if ( $filter == null ) |
|
344
|
|
|
return ''; |
|
345
|
|
|
|
|
346
|
|
|
if ( $filter->before != null && $filter->after != null) |
|
347
|
|
|
throw new Exception('SearchFilter class fields\'s (Before and After) value cannot be specified at the same time!'); |
|
348
|
|
|
|
|
349
|
|
|
$appendable = ''; |
|
350
|
|
|
|
|
351
|
|
|
$filter = (array) $filter; |
|
352
|
|
|
|
|
353
|
|
|
foreach ( $filter as $key => $value ) { |
|
354
|
|
|
if ( $value != null ) { |
|
355
|
|
|
if ( strlen($appendable) != 0 ) |
|
356
|
|
|
$appendable .= '&'; |
|
357
|
|
|
else |
|
358
|
|
|
$appendable .= '?'; |
|
359
|
|
|
|
|
360
|
|
|
$appendable .= $key . '=' . urlencode($value); |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
|
|
364
|
|
|
return $appendable; |
|
365
|
|
|
} |
|
366
|
|
|
} |
|
367
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.