Skip to content

Commit 8597e85

Browse files
committed
Search clans in server.
1 parent f37d61f commit 8597e85

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/wargaming.php

+48-1
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");
@@ -113,6 +116,42 @@ public function serverInfo($region = null)
113116

114117
}
115118

119+
/**
120+
* @param string $search
121+
* @param array|null $option
122+
* @return array
123+
* @throws Exception
124+
*/
125+
public function searchClans($search, $option = null)
126+
{
127+
128+
if (strlen($search) == 0) {
129+
130+
//Search not specified
131+
throw new Exception("SEARCH_NOT_SPECIFIED", "402");
132+
} else if (strlen($search) < 3) {
133+
134+
//Search no enough
135+
throw new Exception("NOT_ENOUGH_SEARCH_LENGTH", "407");
136+
} else if (strlen($search) >= 100) {
137+
138+
//Search as exceeded
139+
throw new Exception("SEARCH_LIST_LIMIT_EXCEEDED", "407");
140+
}
141+
142+
$returned = $this->request("clansSearch", [
143+
"search" => $search,
144+
"limit" => !empty($options['limit']) ? $options['limit'] : 100,
145+
"pagination" => !empty($options['pagination']) ? $options['pagination'] : 1
146+
]);
147+
148+
return [
149+
"count" => $returned['meta']['count'],
150+
"total" => $returned['meta']['total'],
151+
"clans" => $returned['data']
152+
];
153+
}
154+
116155
/**
117156
* @param string $ref
118157
* @param array $options
@@ -138,6 +177,14 @@ private function request($ref, $options)
138177
$link = str_replace("{accounts}", $options['accounts'], $link);
139178
break;
140179

180+
case "clansSearch":
181+
182+
//Replace data of the link
183+
$link = str_replace("{search}", $options['search'], $link);
184+
$link = str_replace("{limit}", $options['limit'], $link);
185+
$link = str_replace("{pagination}", $options['pagination'], $link);
186+
break;
187+
141188
case "serverInfo":
142189

143190
//Replace data of the link

tests/testWargamingApi.php

+14
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,18 @@ public function check_server_info_with_custom_region() {
9090

9191
}
9292

93+
/**
94+
* @test
95+
* @throws Exception
96+
*/
97+
public function check_search_clans_with_default_option()
98+
{
99+
//Init Wargaming.net api key and region
100+
$war = new WargamingApi("e9807cace93606169c54fb8e9ec763b2", "eu");
101+
102+
$clans = $war->searchClans("aze");
103+
104+
$this->assertEquals(100, $clans['count']);
105+
}
106+
93107
}

0 commit comments

Comments
 (0)