forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxMindBinaryProvider.php
More file actions
102 lines (85 loc) · 3.14 KB
/
Copy pathMaxMindBinaryProvider.php
File metadata and controls
102 lines (85 loc) · 3.14 KB
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
<?php
/**
* 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;
use Geocoder\Exception\NoResultException;
use Geocoder\Exception\InvalidArgumentException;
use Geocoder\Exception\RuntimeException;
use Geocoder\Exception\UnsupportedException;
class MaxMindBinaryProvider extends AbstractProvider implements ProviderInterface
{
/**
* @var string
*/
protected $datFile;
/**
* @var int|null
*/
protected $openFlag;
/**
* @param string $datFile
* @param int|null $openFlag
*
* @throws RuntimeException If maxmind's lib not installed.
* @throws InvalidArgumentException If dat file is not correct (optional).
*/
public function __construct($datFile, $openFlag = null)
{
if (false === function_exists('geoip_open')) {
throw new RuntimeException('The MaxMindBinaryProvider requires maxmind\'s lib to be installed and loaded. Have you included geoip.inc file?');
}
if (false === function_exists('GeoIP_record_by_addr')) {
throw new RuntimeException('The MaxMindBinaryProvider requires maxmind\'s lib to be installed and loaded. Have you included geoipcity.inc file?');
}
if (false === is_file($datFile)) {
throw new InvalidArgumentException(sprintf('Given MaxMind dat file "%s" does not exist.', $datFile));
}
if (false === is_readable($datFile)) {
throw new InvalidArgumentException(sprintf('Given MaxMind dat file "%s" does not readable.', $datFile));
}
$this->datFile = $datFile;
$this->openFlag = null === $openFlag ? GEOIP_STANDARD : $openFlag;
}
/**
* {@inheritDoc}
*/
public function getGeocodedData($address)
{
if (false === filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedException('The MaxMindBinaryProvider does not support street addresses.');
}
$geoIp = geoip_open($this->datFile, $this->openFlag);
$geoIpRecord = GeoIP_record_by_addr($geoIp, $address);
geoip_close($geoIp);
if (false === $geoIpRecord instanceof \geoiprecord) {
throw new NoResultException(sprintf('No results found for IP address %s', $address));
}
return array($this->fixEncoding(array_merge($this->getDefaults(), array(
'countryCode' => $geoIpRecord->country_code,
'country' => $geoIpRecord->country_name,
'region' => $geoIpRecord->region,
'city' => $geoIpRecord->city,
'latitude' => $geoIpRecord->latitude,
'longitude' => $geoIpRecord->longitude,
))));
}
/**
* {@inheritDoc}
*/
public function getReversedData(array $coordinates)
{
throw new UnsupportedException('The MaxMindBinaryProvider is not able to do reverse geocoding.');
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'maxmind_binary';
}
}