|
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\IP2LocationBinary; |
|
14
|
|
|
|
|
15
|
|
|
use Geocoder\Collection; |
|
16
|
|
|
use Geocoder\Exception\FunctionNotFound; |
|
17
|
|
|
use Geocoder\Exception\InvalidArgument; |
|
18
|
|
|
use Geocoder\Exception\UnsupportedOperation; |
|
19
|
|
|
use Geocoder\Model\Address; |
|
20
|
|
|
use Geocoder\Model\AddressCollection; |
|
21
|
|
|
use Geocoder\Query\GeocodeQuery; |
|
22
|
|
|
use Geocoder\Query\ReverseQuery; |
|
23
|
|
|
use Geocoder\Provider\AbstractProvider; |
|
24
|
|
|
use Geocoder\Provider\Provider; |
|
25
|
|
|
|
|
26
|
|
|
final class IP2LocationBinary extends AbstractProvider implements Provider |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $binFile; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var int|null |
|
35
|
|
|
*/ |
|
36
|
|
|
private $openFlag; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $binFile |
|
40
|
|
|
* @param int|null $openFlag |
|
41
|
|
|
* |
|
42
|
|
|
* @throws FunctionNotFound if IP2Location's library not installed |
|
43
|
|
|
* @throws InvalidArgument if dat file is not correct (optional) |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(string $binFile, int $openFlag = null) |
|
46
|
|
|
{ |
|
47
|
|
|
if (false === class_exists('\\IP2Location\\Database')) { |
|
48
|
|
|
throw new FunctionNotFound('ip2location_database', 'The IP2LocationBinary requires IP2Location\'s library to be installed and loaded.'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (false === is_file($binFile)) { |
|
52
|
|
|
throw new InvalidArgument(sprintf('Given IP2Location BIN file "%s" does not exist.', $binFile)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (false === is_readable($binFile)) { |
|
56
|
|
|
throw new InvalidArgument(sprintf('Given IP2Location BIN file "%s" does not readable.', $binFile)); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$this->binFile = $binFile; |
|
60
|
|
|
$this->openFlag = null === $openFlag ? \IP2Location\Database::FILE_IO : $openFlag; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function geocodeQuery(GeocodeQuery $query): Collection |
|
67
|
|
|
{ |
|
68
|
|
|
$address = $query->getText(); |
|
69
|
|
|
if (false === filter_var($address, FILTER_VALIDATE_IP)) { |
|
70
|
|
|
throw new UnsupportedOperation('The IP2LocationBinary provider does not support street addresses.'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$db = new \IP2Location\Database($this->binFile, $this->openFlag); |
|
74
|
|
|
$records = $db->lookup($address, \IP2Location\Database::ALL); |
|
75
|
|
|
|
|
76
|
|
|
if (false === $records) { |
|
77
|
|
|
return new AddressCollection([]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$adminLevels = []; |
|
81
|
|
|
|
|
82
|
|
|
if (isset($records['regionName'])) { |
|
83
|
|
|
$adminLevels[] = ['name' => $records['regionName'], 'level' => 1]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return new AddressCollection([ |
|
87
|
|
|
Address::createFromArray([ |
|
88
|
|
|
'providedBy' => $this->getName(), |
|
89
|
|
|
'countryCode' => $records['countryCode'], |
|
90
|
|
|
'country' => null === $records['countryName'] ? null : utf8_encode($records['countryName']), |
|
91
|
|
|
'adminLevels' => $adminLevels, |
|
92
|
|
|
'locality' => null === $records['cityName'] ? null : utf8_encode($records['cityName']), |
|
93
|
|
|
'latitude' => $records['latitude'], |
|
94
|
|
|
'longitude' => $records['longitude'], |
|
95
|
|
|
'postalCode' => $records['zipCode'], |
|
96
|
|
|
]), |
|
97
|
|
|
]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function reverseQuery(ReverseQuery $query): Collection |
|
104
|
|
|
{ |
|
105
|
|
|
throw new UnsupportedOperation('The IP2LocationBinary is not able to do reverse geocoding.'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getName(): string |
|
112
|
|
|
{ |
|
113
|
|
|
return 'ip2location_binary'; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|