Skip to content

Commit 90894f9

Browse files
committed
Merge branch 'clans-integration'
2 parents 203adf5 + e5c4146 commit 90894f9

File tree

2 files changed

+96
-12
lines changed

2 files changed

+96
-12
lines changed

src/wargaming.php

+61-9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Exception;
66
use GuzzleHttp\Client;
7+
use function GuzzleHttp\Psr7\str;
78

89

910
/**
@@ -19,6 +20,8 @@ class WargamingApi
1920
"accountSearch" => "api.worldoftanks.{region}/wgn/account/list/?application_id={key}&search={search}&limit={limit}&type={method}",
2021
"accountId" => "api.worldoftanks.{region}/wgn/account/info/?application_id={key}&account_id={accounts}",
2122

23+
"clansSearch" => "api.worldoftanks.{region}/wgn/clans/list/?application_id={key}&search={search}&limit={limit}&page_no={pagination}",
24+
2225
"serverInfo" => "api.worldoftanks.{region}/wgn/servers/info/?application_id={key}"
2326
];
2427

@@ -46,7 +49,7 @@ public function searchPlayers($search, $options = null)
4649

4750
//Search not specified
4851
throw new Exception("SEARCH_NOT_SPECIFIED", "402");
49-
} else if (strlen($search) <= 3) {
52+
} else if (strlen($search) < 3) {
5053

5154
//Search no enough
5255
throw new Exception("NOT_ENOUGH_SEARCH_LENGTH", "407");
@@ -59,7 +62,8 @@ public function searchPlayers($search, $options = null)
5962
$returned = $this->request("accountSearch", [
6063
"search" => $search,
6164
"limit" => !empty($options['limit']) ? $options['limit'] : 100,
62-
"method" => !empty($options['method']) ? $options['method'] : "startswith"
65+
"method" => !empty($options['method']) ? $options['method'] : "startswith",
66+
"region" => !empty($options['region']) ? $options['region'] : $this->region
6367
]);
6468

6569
return [
@@ -82,7 +86,8 @@ public function searchPlayer($accounts_id = [])
8286
}
8387

8488
$returned = $this->request("accountId", [
85-
"accounts" => $accounts
89+
"accounts" => $accounts,
90+
"region" => !empty($options['region']) ? $options['region'] : $this->region
8691
]);
8792

8893
return [
@@ -99,20 +104,56 @@ public function searchPlayer($accounts_id = [])
99104
*/
100105
public function serverInfo($region = null)
101106
{
102-
$region = !empty($region) ? $region : $this->region;
103107

104108
$returned = $this->request("serverInfo", [
105-
"region" => $region
106-
])['data'];
109+
"region" => !empty($region) ? $region : $this->region
110+
]);
107111

108112
return [
109-
"wotb" => $returned['wotb'],
110-
"wot" => $returned['wot'],
111-
"wows" => $returned['wows']
113+
"wotb" => $returned['data']['wotb'],
114+
"wot" => $returned['data']['wot'],
115+
"wows" => $returned['data']['wows']
112116
];
113117

114118
}
115119

120+
/**
121+
* @param string $search
122+
* @param array|null $options
123+
* @return array
124+
* @throws Exception
125+
*/
126+
public function searchClans($search, $options = null)
127+
{
128+
129+
if (strlen($search) == 0) {
130+
131+
//Search not specified
132+
throw new Exception("SEARCH_NOT_SPECIFIED", "402");
133+
} else if (strlen($search) < 3) {
134+
135+
//Search no enough
136+
throw new Exception("NOT_ENOUGH_SEARCH_LENGTH", "407");
137+
} else if (strlen($search) >= 100) {
138+
139+
//Search as exceeded
140+
throw new Exception("SEARCH_LIST_LIMIT_EXCEEDED", "407");
141+
}
142+
143+
$returned = $this->request("clansSearch", [
144+
"search" => $search,
145+
"limit" => !empty($options['limit']) ? $options['limit'] : 100,
146+
"pagination" => !empty($options['pagination']) ? $options['pagination'] : 1,
147+
"region" => !empty($options['region']) ? $options['region'] : $this->region
148+
]);
149+
150+
return [
151+
"count" => $returned['meta']['count'],
152+
"total" => $returned['meta']['total'],
153+
"clans" => $returned['data']
154+
];
155+
}
156+
116157
/**
117158
* @param string $ref
118159
* @param array $options
@@ -130,12 +171,23 @@ private function request($ref, $options)
130171
$link = str_replace("{search}", $options['search'], $link);
131172
$link = str_replace("{limit}", $options['limit'], $link);
132173
$link = str_replace("{method}", $options['method'], $link);
174+
$link = str_replace("{region}", $options['region'], $link);
133175
break;
134176

135177
case "accountId":
136178

137179
//Replace data of the link
138180
$link = str_replace("{accounts}", $options['accounts'], $link);
181+
$link = str_replace("{region}", $options['region'], $link);
182+
break;
183+
184+
case "clansSearch":
185+
186+
//Replace data of the link
187+
$link = str_replace("{search}", $options['search'], $link);
188+
$link = str_replace("{limit}", $options['limit'], $link);
189+
$link = str_replace("{pagination}", $options['pagination'], $link);
190+
$link = str_replace("{region}", $options['region'], $link);
139191
break;
140192

141193
case "serverInfo":

tests/testWargamingApi.php

+35-3
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,46 @@ public function check_server_info_with_default_region() {
8080
*/
8181
public function check_server_info_with_custom_region() {
8282

83+
//Init Wargaming.net api key and region
84+
$war = new WargamingApi("e9807cace93606169c54fb8e9ec763b2", "ru");
85+
86+
$server = $war->serverInfo("eu");
87+
88+
$this->assertEquals("EU1",$server['wot'][1]['server']);
89+
$this->assertEquals("EU2",$server['wot'][0]['server']);
90+
91+
}
92+
93+
/**
94+
* @test
95+
* @throws Exception
96+
*/
97+
public function check_search_clans_with_default_option()
98+
{
8399
//Init Wargaming.net api key and region
84100
$war = new WargamingApi("e9807cace93606169c54fb8e9ec763b2", "eu");
85101

86-
$server = $war->serverInfo("com");
102+
$clans = $war->searchClans("aze");
103+
104+
$this->assertEquals(100, $clans['count']);
105+
}
106+
107+
/**
108+
* @test
109+
* @throws Exception
110+
*/
111+
public function check_search_clans_with_custom_option()
112+
{
113+
//Init Wargaming.net api key and region
114+
$war = new WargamingApi("e9807cace93606169c54fb8e9ec763b2", "eu");
87115

88-
$this->assertEquals(304,$server['wot'][1]['server']);
89-
$this->assertEquals(303,$server['wot'][0]['server']);
116+
$clans = $war->searchClans("aze", [
117+
"limit" => 10,
118+
"pagination" => 1,
119+
"region" => "ru"
120+
]);
90121

122+
$this->assertEquals(10, $clans['count']);
91123
}
92124

93125
}

0 commit comments

Comments
 (0)