|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BlizzardApi; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; |
|
6
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Blizzard Client |
|
10
|
|
|
* |
|
11
|
|
|
* @author Oleg Kachinsky <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class BlizzardClient |
|
14
|
|
|
{ |
|
15
|
|
|
const API_URL_PATTERN = 'https://region.api.battle.net'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string $apiUrl Blizzard API url |
|
19
|
|
|
*/ |
|
20
|
|
|
private $apiUrl; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string $apiKey API key |
|
24
|
|
|
*/ |
|
25
|
|
|
private $apiKey; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string $accessToken Access token |
|
29
|
|
|
*/ |
|
30
|
|
|
private $accessToken; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string $locale Locale |
|
34
|
|
|
*/ |
|
35
|
|
|
private $locale; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string $region Region |
|
39
|
|
|
*/ |
|
40
|
|
|
private $region; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* BlizzardClient constructor |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $apiKey API key |
|
46
|
|
|
* @param string $region Region |
|
47
|
|
|
* @param string $locale Locale |
|
48
|
|
|
* @param null|string $accessToken OAuth access token |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct($apiKey, $region = 'us', $locale = 'en_us', $accessToken = null) |
|
51
|
|
|
{ |
|
52
|
|
|
$options = [ |
|
53
|
|
|
'apiKey' => $apiKey, |
|
54
|
|
|
'region' => strtolower($region), |
|
55
|
|
|
'locale' => strtolower($locale), |
|
56
|
|
|
'accessToken' => $accessToken, |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
$resolver = (new OptionsResolver()); |
|
60
|
|
|
$this->configureOptions($resolver, $options['region']); |
|
61
|
|
|
|
|
62
|
|
|
$options = $resolver->resolve($options); |
|
63
|
|
|
|
|
64
|
|
|
$this->apiKey = $options['apiKey']; |
|
65
|
|
|
$this->region = $options['region']; |
|
66
|
|
|
$this->locale = $options['locale']; |
|
67
|
|
|
$this->accessToken = $options['accessToken']; |
|
68
|
|
|
|
|
69
|
|
|
$this->updateApiUrl($options['region']); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get api url |
|
74
|
|
|
* |
|
75
|
|
|
* @return string Api url |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getApiUrl() |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->apiUrl; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get api key |
|
84
|
|
|
* |
|
85
|
|
|
* @return string Api key |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getApiKey() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->apiKey; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Set api key |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $apiKey Api key |
|
96
|
|
|
* |
|
97
|
|
|
* @return $this |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setApiKey($apiKey) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->apiKey = $apiKey; |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get region |
|
108
|
|
|
* |
|
109
|
|
|
* @return string Region |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getRegion() |
|
112
|
|
|
{ |
|
113
|
|
|
return strtolower($this->region); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Set region |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $region region |
|
120
|
|
|
* |
|
121
|
|
|
* @return $this |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setRegion($region) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->region = strtolower($region); |
|
126
|
|
|
|
|
127
|
|
|
$this->updateApiUrl($region); |
|
128
|
|
|
|
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Get locale |
|
134
|
|
|
* |
|
135
|
|
|
* @return string Locale |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getLocale() |
|
138
|
|
|
{ |
|
139
|
|
|
return strtolower($this->locale); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Set locale |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $locale locale |
|
146
|
|
|
* |
|
147
|
|
|
* @return $this |
|
148
|
|
|
*/ |
|
149
|
|
|
public function setLocale($locale) |
|
150
|
|
|
{ |
|
151
|
|
|
$this->locale = strtolower($locale); |
|
152
|
|
|
|
|
153
|
|
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get access token |
|
158
|
|
|
* |
|
159
|
|
|
* @return null|string Access token |
|
160
|
|
|
*/ |
|
161
|
|
|
public function getAccessToken() |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->accessToken; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Set access token |
|
168
|
|
|
* |
|
169
|
|
|
* @param null|string $accessToken Access token |
|
170
|
|
|
* |
|
171
|
|
|
* @return $this |
|
172
|
|
|
*/ |
|
173
|
|
|
public function setAccessToken($accessToken) |
|
174
|
|
|
{ |
|
175
|
|
|
$this->accessToken = $accessToken; |
|
176
|
|
|
|
|
177
|
|
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Update API url |
|
182
|
|
|
* |
|
183
|
|
|
* Update API url by replacing region in API url pattern |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $region Region |
|
186
|
|
|
* |
|
187
|
|
|
* @return $this |
|
188
|
|
|
*/ |
|
189
|
|
|
private function updateApiUrl($region) |
|
190
|
|
|
{ |
|
191
|
|
|
$this->apiUrl = str_replace('region', strtolower($region), self::API_URL_PATTERN); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Configure options |
|
196
|
|
|
* |
|
197
|
|
|
* @param OptionsResolver $resolver Symfony options resolver |
|
198
|
|
|
* @param string $region Region |
|
199
|
|
|
* |
|
200
|
|
|
* @throws InvalidOptionsException |
|
201
|
|
|
*/ |
|
202
|
|
|
private function configureOptions(OptionsResolver $resolver, $region) |
|
203
|
|
|
{ |
|
204
|
|
|
if (isset(GeoData::$list[$region])) { |
|
205
|
|
|
$locales = GeoData::$list[$region]; |
|
206
|
|
|
} else { |
|
207
|
|
|
throw new InvalidOptionsException( |
|
208
|
|
|
sprintf( |
|
209
|
|
|
'The option "region" with value "%s" is invalid. Accepted values are: "%s".', |
|
210
|
|
|
$region, |
|
211
|
|
|
implode('", "', array_keys(GeoData::$list)) |
|
212
|
|
|
) |
|
213
|
|
|
); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
$resolver->setRequired(['apiKey', 'region', 'locale', 'accessToken']) |
|
217
|
|
|
->setAllowedTypes('apiKey', 'string') |
|
218
|
|
|
->setAllowedTypes('region', 'string') |
|
219
|
|
|
->setAllowedValues('region', array_keys(GeoData::$list)) |
|
220
|
|
|
->setAllowedTypes('locale', 'string') |
|
221
|
|
|
->setAllowedValues('locale', $locales) |
|
222
|
|
|
->setAllowedTypes('accessToken', ['null', 'string']); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|