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\IpAddressGeocoder; |
23
|
|
|
use Geocoder\Provider\Provider; |
24
|
|
|
use Http\Client\HttpClient; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author William Durand <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
final class FreeGeoIp extends AbstractHttpProvider implements Provider, IpAddressGeocoder |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
private $baseUrl; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param HttpClient $client |
38
|
|
|
* @param string $baseUrl |
39
|
|
|
*/ |
40
|
24 |
|
public function __construct(HttpClient $client, string $baseUrl = 'https://freegeoip.net/json/%s') |
41
|
|
|
{ |
42
|
24 |
|
parent::__construct($client); |
43
|
|
|
|
44
|
24 |
|
$this->baseUrl = $baseUrl; |
45
|
24 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
22 |
|
public function geocodeQuery(GeocodeQuery $query): Collection |
51
|
|
|
{ |
52
|
22 |
|
$address = $query->getText(); |
53
|
22 |
|
if (!filter_var($address, FILTER_VALIDATE_IP)) { |
54
|
1 |
|
throw new UnsupportedOperation('The FreeGeoIp provider does not support street addresses.'); |
55
|
|
|
} |
56
|
|
|
|
57
|
21 |
|
if (in_array($address, ['127.0.0.1', '::1'])) { |
58
|
2 |
|
return new AddressCollection([$this->getLocationForLocalhost()]); |
59
|
|
|
} |
60
|
|
|
|
61
|
19 |
|
$content = $this->getUrlContents(sprintf($this->baseUrl, $address)); |
62
|
8 |
|
$data = json_decode($content, true); |
63
|
8 |
|
$builder = new AddressBuilder($this->getName()); |
64
|
|
|
|
65
|
8 |
|
if (!empty($data['region_name'])) { |
66
|
7 |
|
$builder->addAdminLevel(1, $data['region_name'], $data['region_code'] ?? null); |
67
|
|
|
} |
68
|
|
|
|
69
|
8 |
|
if ($data['latitude'] !== 0 || $data['longitude'] !== 0) { |
70
|
7 |
|
$builder->setCoordinates($data['latitude'] ?? null, $data['longitude'] ?? null); |
71
|
|
|
} |
72
|
8 |
|
$builder->setLocality(empty($data['city']) ? null : $data['city']); |
73
|
8 |
|
$builder->setPostalCode(empty($data['zip_code']) ? null : $data['zip_code']); |
74
|
8 |
|
$builder->setCountry(empty($data['country_name']) ? null : $data['country_name']); |
75
|
8 |
|
$builder->setCountryCode(empty($data['country_code']) ? null : $data['country_code']); |
76
|
8 |
|
$builder->setTimezone(empty($data['time_zone']) ? null : $data['time_zone']); |
77
|
|
|
|
78
|
8 |
|
return new AddressCollection([$builder->build()]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
1 |
|
public function reverseQuery(ReverseQuery $query): Collection |
85
|
|
|
{ |
86
|
1 |
|
throw new UnsupportedOperation('The FreeGeoIp provider is not able to do reverse geocoding.'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
11 |
|
public function getName(): string |
93
|
|
|
{ |
94
|
11 |
|
return 'free_geo_ip'; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|