|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BlizzardApi; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Blizzard Client |
|
7
|
|
|
* |
|
8
|
|
|
* @author Oleg Kachinsky <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
class BlizzardClient |
|
11
|
|
|
{ |
|
12
|
|
|
const API_URL_PATTERN = 'https://region.api.battle.net'; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string $apiUrl Blizzard API url |
|
16
|
|
|
*/ |
|
17
|
|
|
private $apiUrl; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var string $apiKey API key |
|
21
|
|
|
*/ |
|
22
|
|
|
private $apiKey; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string $locale Locale |
|
26
|
|
|
*/ |
|
27
|
|
|
private $locale; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string $region Region |
|
31
|
|
|
*/ |
|
32
|
|
|
private $region; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* BlizzardClient constructor |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $apiKey API key |
|
38
|
|
|
* @param null|string $accessToken OAuth access token |
|
39
|
|
|
* @param string $locale Locale |
|
40
|
|
|
* @param string $region Region |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($apiKey, $accessToken = null, $locale = 'en_us', $region = 'us') |
|
43
|
|
|
{ |
|
44
|
|
|
$this->apiKey = $apiKey; |
|
45
|
|
|
$this->accessToken = $accessToken; |
|
|
|
|
|
|
46
|
|
|
$this->locale = strtolower($locale); |
|
47
|
|
|
$this->region = strtolower($region); |
|
48
|
|
|
|
|
49
|
|
|
$this->updateApiUrl($region); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get api url |
|
54
|
|
|
* |
|
55
|
|
|
* @return string Api url |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getApiUrl() |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->apiUrl; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get api key |
|
64
|
|
|
* |
|
65
|
|
|
* @return string Api key |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getApiKey() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->apiKey; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set api key |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $apiKey Api key |
|
76
|
|
|
* |
|
77
|
|
|
* @return $this |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setApiKey($apiKey) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->apiKey = $apiKey; |
|
82
|
|
|
|
|
83
|
|
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get access token |
|
88
|
|
|
* |
|
89
|
|
|
* @return null|string Access token |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getAccessToken() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->accessToken; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Set access token |
|
98
|
|
|
* |
|
99
|
|
|
* @param null|string $accessToken Access token |
|
100
|
|
|
* |
|
101
|
|
|
* @return $this |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setAccessToken($accessToken) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->accessToken = $accessToken; |
|
106
|
|
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get locale |
|
112
|
|
|
* |
|
113
|
|
|
* @return string Locale |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getLocale() |
|
116
|
|
|
{ |
|
117
|
|
|
return strtolower($this->locale); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Set locale |
|
122
|
|
|
* |
|
123
|
|
|
* @param string $locale locale |
|
124
|
|
|
* |
|
125
|
|
|
* @return $this |
|
126
|
|
|
*/ |
|
127
|
|
|
public function setLocale($locale) |
|
128
|
|
|
{ |
|
129
|
|
|
$this->locale = strtolower($locale); |
|
130
|
|
|
|
|
131
|
|
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get region |
|
136
|
|
|
* |
|
137
|
|
|
* @return string Region |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getRegion() |
|
140
|
|
|
{ |
|
141
|
|
|
return strtolower($this->region); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Set region |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $region region |
|
148
|
|
|
* |
|
149
|
|
|
* @return $this |
|
150
|
|
|
*/ |
|
151
|
|
|
public function setRegion($region) |
|
152
|
|
|
{ |
|
153
|
|
|
$this->region = strtolower($region); |
|
154
|
|
|
|
|
155
|
|
|
$this->updateApiUrl($region); |
|
156
|
|
|
|
|
157
|
|
|
return $this; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Update API url |
|
162
|
|
|
* |
|
163
|
|
|
* Update API url by replacing region in API url pattern |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $region Region |
|
166
|
|
|
* |
|
167
|
|
|
* @return $this |
|
168
|
|
|
*/ |
|
169
|
|
|
private function updateApiUrl($region) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->apiUrl = str_replace('region', strtolower($region), self::API_URL_PATTERN); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: