-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIP2LocationBinary.php
103 lines (85 loc) · 3.36 KB
/
IP2LocationBinary.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types=1);
/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Provider\IP2LocationBinary;
use Geocoder\Collection;
use Geocoder\Exception\FunctionNotFound;
use Geocoder\Exception\InvalidArgument;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Model\Address;
use Geocoder\Model\AddressCollection;
use Geocoder\Provider\AbstractProvider;
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
final class IP2LocationBinary extends AbstractProvider implements Provider
{
/**
* @var string
*/
private $binFile;
/**
* @var int|null
*/
private $openFlag;
/**
* @throws FunctionNotFound if IP2Location's library not installed
* @throws InvalidArgument if dat file is not correct (optional)
*/
public function __construct(string $binFile, ?int $openFlag = null)
{
if (false === class_exists('\\IP2Location\\Database')) {
throw new FunctionNotFound('ip2location_database', 'The IP2LocationBinary requires IP2Location\'s library to be installed and loaded.');
}
if (false === is_file($binFile)) {
throw new InvalidArgument(sprintf('Given IP2Location BIN file "%s" does not exist.', $binFile));
}
if (false === is_readable($binFile)) {
throw new InvalidArgument(sprintf('Given IP2Location BIN file "%s" does not readable.', $binFile));
}
$this->binFile = $binFile;
$this->openFlag = null === $openFlag ? \IP2Location\Database::FILE_IO : $openFlag;
}
public function geocodeQuery(GeocodeQuery $query): Collection
{
$address = $query->getText();
if (false === filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedOperation('The IP2LocationBinary provider does not support street addresses.');
}
$db = new \IP2Location\Database($this->binFile, $this->openFlag);
$records = $db->lookup($address, \IP2Location\Database::ALL);
if (false === $records) {
return new AddressCollection([]);
}
$adminLevels = [];
if (isset($records['regionName'])) {
$adminLevels[] = ['name' => $records['regionName'], 'level' => 1];
}
return new AddressCollection([
Address::createFromArray([
'providedBy' => $this->getName(),
'countryCode' => $records['countryCode'],
'country' => null === $records['countryName'] ? null : mb_convert_encoding($records['countryName'], 'UTF-8', 'ISO-8859-1'),
'adminLevels' => $adminLevels,
'locality' => null === $records['cityName'] ? null : mb_convert_encoding($records['cityName'], 'UTF-8', 'ISO-8859-1'),
'latitude' => $records['latitude'],
'longitude' => $records['longitude'],
'postalCode' => $records['zipCode'],
]),
]);
}
public function reverseQuery(ReverseQuery $query): Collection
{
throw new UnsupportedOperation('The IP2LocationBinary is not able to do reverse geocoding.');
}
public function getName(): string
{
return 'ip2location_binary';
}
}