1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Geocoder package. |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @license MIT License |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Geocoder\Provider\FreeGeoIp; |
14
|
|
|
|
15
|
|
|
use Geocoder\Collection; |
16
|
|
|
use Geocoder\Exception\UnsupportedOperation; |
17
|
|
|
use Geocoder\Model\AddressBuilder; |
18
|
|
|
use Geocoder\Model\AddressCollection; |
19
|
|
|
use Geocoder\Query\GeocodeQuery; |
20
|
|
|
use Geocoder\Query\ReverseQuery; |
21
|
|
|
use Geocoder\Http\Provider\AbstractHttpProvider; |
22
|
|
|
use Geocoder\Provider\Provider; |
23
|
|
|
use Http\Client\HttpClient; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author William Durand <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
final class FreeGeoIp extends AbstractHttpProvider implements Provider |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var string|null |
32
|
|
|
*/ |
33
|
|
|
private $baseUrl; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param HttpClient $client |
37
|
|
|
* @param string $baseUrl |
38
|
|
|
*/ |
39
|
27 |
|
public function __construct(HttpClient $client, string $baseUrl = 'https://freegeoip.app/json/%s') |
40
|
|
|
{ |
41
|
27 |
|
parent::__construct($client); |
42
|
|
|
|
43
|
27 |
|
$this->baseUrl = $baseUrl; |
44
|
27 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
25 |
|
public function geocodeQuery(GeocodeQuery $query): Collection |
50
|
|
|
{ |
51
|
25 |
|
$address = $query->getText(); |
52
|
|
|
|
53
|
25 |
|
if (!filter_var($address, FILTER_VALIDATE_IP)) { |
54
|
1 |
|
throw new UnsupportedOperation('The FreeGeoIp provider does not support street addresses.'); |
55
|
|
|
} |
56
|
|
|
|
57
|
24 |
|
if (in_array($address, ['127.0.0.1', '::1'])) { |
58
|
2 |
|
return new AddressCollection([$this->getLocationForLocalhost()]); |
59
|
|
|
} |
60
|
|
|
|
61
|
22 |
|
$request = $this->getRequest(sprintf($this->baseUrl, $address)); |
62
|
|
|
|
63
|
22 |
|
if (null !== $query->getLocale()) { |
64
|
5 |
|
$request = $request->withHeader('Accept-Language', $query->getLocale()); |
65
|
|
|
} |
66
|
|
|
|
67
|
22 |
|
$body = $this->getParsedResponse($request); |
68
|
11 |
|
$data = json_decode($body, true); |
69
|
|
|
|
70
|
|
|
// Return empty collection if address was not found |
71
|
11 |
|
if ('' === $data['region_name'] |
72
|
11 |
|
&& '' === $data['region_code'] |
73
|
11 |
|
&& 0 === $data['latitude'] |
74
|
11 |
|
&& 0 === $data['longitude'] |
75
|
11 |
|
&& '' === $data['city'] |
76
|
11 |
|
&& '' === $data['zip_code'] |
77
|
11 |
|
&& '' === $data['country_name'] |
78
|
11 |
|
&& '' === $data['country_code'] |
79
|
11 |
|
&& '' === $data['time_zone']) { |
80
|
1 |
|
return new AddressCollection([]); |
81
|
|
|
} |
82
|
|
|
|
83
|
10 |
|
$builder = new AddressBuilder($this->getName()); |
84
|
|
|
|
85
|
10 |
|
if (!empty($data['region_name'])) { |
86
|
7 |
|
$builder->addAdminLevel(1, $data['region_name'], $data['region_code'] ?? null); |
87
|
|
|
} |
88
|
|
|
|
89
|
10 |
|
if (0 !== $data['latitude'] || 0 !== $data['longitude']) { |
90
|
10 |
|
$builder->setCoordinates($data['latitude'] ?? null, $data['longitude'] ?? null); |
91
|
|
|
} |
92
|
10 |
|
$builder->setLocality(empty($data['city']) ? null : $data['city']); |
93
|
10 |
|
$builder->setPostalCode(empty($data['zip_code']) ? null : $data['zip_code']); |
94
|
10 |
|
$builder->setCountry(empty($data['country_name']) ? null : $data['country_name']); |
95
|
10 |
|
$builder->setCountryCode(empty($data['country_code']) ? null : $data['country_code']); |
96
|
10 |
|
$builder->setTimezone(empty($data['time_zone']) ? null : $data['time_zone']); |
97
|
|
|
|
98
|
10 |
|
return new AddressCollection([$builder->build()]); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
1 |
|
public function reverseQuery(ReverseQuery $query): Collection |
105
|
|
|
{ |
106
|
1 |
|
throw new UnsupportedOperation('The FreeGeoIp provider is not able to do reverse geocoding.'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
13 |
|
public function getName(): string |
113
|
|
|
{ |
114
|
13 |
|
return 'free_geo_ip'; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|