|
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\HostIp; |
|
14
|
|
|
|
|
15
|
|
|
use Geocoder\Exception\UnsupportedOperation; |
|
16
|
|
|
use Geocoder\Collection; |
|
17
|
|
|
use Geocoder\Model\Address; |
|
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
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author William Durand <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final class HostIp extends AbstractHttpProvider implements Provider |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
const ENDPOINT_URL = 'http://api.hostip.info/get_json.php?ip=%s&position=true'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* {@inheritdoc} |
|
36
|
|
|
*/ |
|
37
|
|
|
public function geocodeQuery(GeocodeQuery $query): Collection |
|
38
|
|
|
{ |
|
39
|
|
|
$address = $query->getText(); |
|
40
|
|
|
if (!filter_var($address, FILTER_VALIDATE_IP)) { |
|
41
|
|
|
throw new UnsupportedOperation('The HostIp provider does not support Street addresses.'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// This API does not support IPv6 |
|
45
|
|
|
if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
|
46
|
|
|
throw new UnsupportedOperation('The HostIp provider does not support IPv6 addresses.'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if ('127.0.0.1' === $address) { |
|
50
|
|
|
return new AddressCollection([$this->getLocationForLocalhost()]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
$url = sprintf(self::ENDPOINT_URL, $address); |
|
54
|
|
|
|
|
55
|
|
|
return $this->executeQuery($url); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function reverseQuery(ReverseQuery $query): Collection |
|
62
|
|
|
{ |
|
63
|
|
|
throw new UnsupportedOperation('The HostIp provider is not able to do reverse geocoding.'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getName(): string |
|
70
|
|
|
{ |
|
71
|
|
|
return 'host_ip'; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $url |
|
76
|
|
|
* |
|
77
|
|
|
* @return Collection |
|
78
|
|
|
*/ |
|
79
|
|
|
private function executeQuery(string $url): AddressCollection |
|
80
|
|
|
{ |
|
81
|
|
|
$content = $this->getUrlContents($url); |
|
82
|
|
|
$data = json_decode($content, true); |
|
83
|
|
|
|
|
84
|
|
|
if (!$data) { |
|
85
|
|
|
return new AddressCollection([]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
// Return empty collection if address was not found |
|
89
|
|
|
if (null === $data['lat'] |
|
90
|
|
|
&& null === $data['lng'] |
|
91
|
|
|
&& '(Unknown City?)' === $data['city'] |
|
92
|
|
|
&& '(Unknown Country?)' === $data['country_name'] |
|
93
|
|
|
&& 'XX' === $data['country_code']) { |
|
94
|
|
|
return new AddressCollection([]); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Return empty collection if address was not found |
|
98
|
|
|
if (null === $data['lat'] |
|
99
|
|
|
&& null === $data['lng'] |
|
100
|
|
|
&& '(Private Address)' === $data['city'] |
|
101
|
|
|
&& '(Private Address)' === $data['country_name'] |
|
102
|
|
|
&& 'XX' === $data['country_code']) { |
|
103
|
|
|
return new AddressCollection([]); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return new AddressCollection([ |
|
107
|
|
|
Address::createFromArray([ |
|
108
|
|
|
'providedBy' => $this->getName(), |
|
109
|
|
|
'latitude' => $data['lat'], |
|
110
|
|
|
'longitude' => $data['lng'], |
|
111
|
|
|
'locality' => $data['city'], |
|
112
|
|
|
'country' => $data['country_name'], |
|
113
|
|
|
'countryCode' => $data['country_code'], |
|
114
|
|
|
]), |
|
115
|
|
|
]); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|