-
Notifications
You must be signed in to change notification settings - Fork 0
/
IpInfoLocator.php
46 lines (37 loc) · 965 Bytes
/
IpInfoLocator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
include 'classes/Location.php';
/**
* Class Locator
*/
class IpInfoLocator implements Locator
{
private $client;
private $token;
public function __construct(HttpClient $client, string $token)
{
$this->client = $client;
$this->token = $token;
}
public function locate(Ip $ip)
{
$url = 'https://ip.io?' . http_build_query([
'token' => $this->token,
'ip' => $ip->getValue(), //TODO to detalize this info
]);
$response = $this->client->get($url);
$data = json_decode($response, true);
function func($value)
{
return $value !== '-' ? $value : null;
}
$data = array_map('func', $data);
if (empty($data['country_name'])) {
return null;
}
return new Location(
$data['country'],
$data['region'],
$data['city']
);
}
}