1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ClashOfClans; |
4
|
|
|
|
5
|
|
|
use ClashOfClans\Api\Clan\Clan; |
6
|
|
|
use ClashOfClans\Api\Clan\Player; |
7
|
|
|
use ClashOfClans\Api\WarLog\WarLog; |
8
|
|
|
use ClashOfClans\Api\League\League; |
9
|
|
|
use ClashOfClans\Api\Location\Location; |
10
|
|
|
use ClashOfClans\Api\Location\LocationList; |
11
|
|
|
use ClashOfClans\Api\ResponseMediator; |
12
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
13
|
|
|
use GuzzleHttp\ClientInterface; |
14
|
|
|
|
15
|
|
|
class Client |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
protected $httpClient; |
19
|
|
|
|
20
|
|
|
protected $token; |
21
|
|
|
|
22
|
|
|
public function __construct($token) |
23
|
|
|
{ |
24
|
|
|
$this->token = $token; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get full details for specific clan |
29
|
|
|
* |
30
|
|
|
* @param string $tag |
31
|
|
|
* @return Clan |
32
|
|
|
*/ |
33
|
|
|
public function getClan($tag) |
34
|
|
|
{ |
35
|
|
|
$response = $this->request('clans/' . urlencode($tag)); |
36
|
|
|
|
37
|
|
|
return Clan::makeFromArray($response); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Search for clans using parameters |
42
|
|
|
* @see Documentation at https://developer.clashofclans.com/ |
43
|
|
|
* |
44
|
|
|
* @param $params |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function getClans($params) |
48
|
|
|
{ |
49
|
|
|
$params = is_array($params) ? $params : ['name' => $params]; |
50
|
|
|
|
51
|
|
|
$response = $this->request('clans?' . http_build_query($params)); |
52
|
|
|
|
53
|
|
|
return array_map(function ($item) { |
54
|
|
|
return Clan::makeFromArray($item); |
55
|
|
|
}, $response['items']); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get warlog for specific clan |
60
|
|
|
* |
61
|
|
|
* @param string $tag |
62
|
|
|
* @return WarLog |
63
|
|
|
*/ |
64
|
|
|
public function getClanWarLog($tag) |
65
|
|
|
{ |
66
|
|
|
$response = $this->request('clans/' . urlencode($tag).'/warlog'); |
67
|
|
|
|
68
|
|
|
return array_map(function ($item) { |
69
|
|
|
return WarLog::makeFromArray($item); |
70
|
|
|
}, $response['items']); |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get details for specific location |
76
|
|
|
* @param $id |
77
|
|
|
* @return Location |
78
|
|
|
*/ |
79
|
|
|
public function getLocation($id) |
80
|
|
|
{ |
81
|
|
|
return Location::makeFromArray($this->request('locations/' . urlencode($id))); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get list of all locations |
86
|
|
|
* |
87
|
|
|
* @return array |
88
|
|
|
*/ |
89
|
|
|
public function getLocations() |
90
|
|
|
{ |
91
|
|
|
return array_map(function ($item) { |
92
|
|
|
return Location::makeFromArray($item); |
93
|
|
|
}, $this->request('locations')['items']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get rankings for specific location |
98
|
|
|
* @param $locationId |
99
|
|
|
* @param $rankingId |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function getRankingsForLocation($locationId, $rankingId) |
103
|
|
|
{ |
104
|
|
|
$url = 'locations/' . $locationId . '/rankings/' . $rankingId; |
105
|
|
|
|
106
|
|
|
if ($rankingId == 'clans') { |
107
|
|
|
return array_map(function ($item) { |
108
|
|
|
return Clan::makeFromArray($item); |
109
|
|
|
}, $this->request($url)['items']); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return array_map(function ($item) { |
113
|
|
|
return Player::makeFromArray($item); |
114
|
|
|
}, $this->request($url)['items']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get all available leagues |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function getLeagues() |
123
|
|
|
{ |
124
|
|
|
return array_map(function ($item) { |
125
|
|
|
return League::makeFromArray($item); |
126
|
|
|
}, $this->request('leagues')['items']); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $url |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
protected function request($url) |
134
|
|
|
{ |
135
|
|
|
$response = $this->getHttpClient() |
136
|
|
|
->request('GET', $url, ['headers' => ['authorization' => 'Bearer ' . $this->getToken()]]); |
137
|
|
|
|
138
|
|
|
return ResponseMediator::convertResponseToArray($response); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return GuzzleClient |
143
|
|
|
*/ |
144
|
|
|
public function getHttpClient() |
145
|
|
|
{ |
146
|
|
|
if ($this->httpClient === null) { |
147
|
|
|
$this->httpClient = new GuzzleClient(['base_uri' => 'https://api.clashofclans.com/v1/']); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $this->httpClient; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function setHttpClient(ClientInterface $client) |
154
|
|
|
{ |
155
|
|
|
$this->httpClient = $client; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return string|null |
162
|
|
|
*/ |
163
|
|
|
public function getToken() |
164
|
|
|
{ |
165
|
|
|
return $this->token; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|