Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2e4d100

Browse files
committed
add IpInfoProvider + tests
1 parent c44e215 commit 2e4d100

File tree

3 files changed

+284
-1
lines changed

3 files changed

+284
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ Please, read the [Geocoder's documentation](http://geocoder-php.org/Geocoder/).
3939

4040
* [OGD Vienna](https://open.wien.at/site/datensatz/?id=c223b93a-2634-4f06-ac73-8709b9e16888) as Address-Based geocoding provider (exclusively in Vienna / Austria);
4141
* [Naver](http://developer.naver.com/wiki/pages/SrchAPI) as Address-Base geocoding provider (exclusively in South Korea);
42-
* [Geocodio](http://geocod.io/) as Address-Based geocoding and reverse geocoding provider (exclusively in USA).
42+
* [Geocodio](http://geocod.io/) as Address-Based geocoding and reverse geocoding provider (exclusively in USA);
43+
* [IpInfo](http://ipinfo.io/developers) as IP-Based geocoding provider.
4344

4445
Contributing
4546
------------
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Geocoder package.
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @license MIT License
9+
*/
10+
11+
namespace Geocoder\Provider;
12+
13+
use Geocoder\Exception\UnsupportedException;
14+
use Geocoder\Exception\NoResultException;
15+
16+
/**
17+
* @author Antoine Corcy <[email protected]>
18+
*/
19+
class IpInfoProvider extends AbstractProvider implements ProviderInterface
20+
{
21+
/**
22+
* @var string
23+
*/
24+
const ENDPOINT_URL = 'http://ipinfo.io/%s/json';
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function getGeocodedData($address)
30+
{
31+
if (!filter_var($address, FILTER_VALIDATE_IP)) {
32+
throw new UnsupportedException('The IpInfoProvider does not support Street addresses.');
33+
}
34+
35+
if (in_array($address, array('127.0.0.1', '::1'))) {
36+
return array($this->getLocalhostDefaults());
37+
}
38+
39+
$query = sprintf(self::ENDPOINT_URL, $address);
40+
$content = $this->getAdapter()->getContent($query);
41+
$data = json_decode($content, true);
42+
43+
if (empty($data) || '' === $data['loc']) {
44+
throw new NoResultException(sprintf('Could not execute query %s', $query));
45+
}
46+
47+
$location = explode(',', $data['loc']);
48+
49+
return array(array_merge($this->getDefaults(), array(
50+
'latitude' => $location[0],
51+
'longitude' => $location[1],
52+
'city' => isset($data['city']) ? $data['city'] : null,
53+
'zipcode' => isset($data['postal']) ? $data['postal'] : null,
54+
'region' => isset($data['region']) ? $data['region'] : null,
55+
'countryCode' => isset($data['country']) ? $data['country'] : null,
56+
)));
57+
}
58+
59+
/**
60+
* {@inheritDoc}
61+
*/
62+
public function getReversedData(array $coordinates)
63+
{
64+
throw new UnsupportedException('The IpInfoProvider is not able to do reverse geocoding.');
65+
}
66+
67+
/**
68+
* {@inheritDoc}
69+
*/
70+
public function getName()
71+
{
72+
return 'ip_info';
73+
}
74+
}
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
<?php
2+
3+
namespace Geocoder\Tests\Provider;
4+
5+
use Geocoder\Tests\TestCase;
6+
use Geocoder\Provider\IpInfoProvider;
7+
8+
class IpInfoProviderTest extends TestCase
9+
{
10+
public function testGetName()
11+
{
12+
$provider = new IpInfoProvider($this->getMockAdapter($this->never()));
13+
$this->assertEquals('ip_info', $provider->getName());
14+
}
15+
16+
/**
17+
* @expectedException \Geocoder\Exception\UnsupportedException
18+
* @expectedExceptionMessage The IpInfoProvider does not support Street addresses.
19+
*/
20+
public function testGetGeocodedDataWithNull()
21+
{
22+
$provider = new IpInfoProvider($this->getMockAdapter($this->never()));
23+
$provider->getGeocodedData(null);
24+
}
25+
26+
/**
27+
* @expectedException \Geocoder\Exception\UnsupportedException
28+
* @expectedExceptionMessage The IpInfoProvider does not support Street addresses.
29+
*/
30+
public function testGetGeocodedDataWithEmpty()
31+
{
32+
$provider = new IpInfoProvider($this->getMockAdapter($this->never()));
33+
$provider->getGeocodedData('');
34+
}
35+
36+
/**
37+
* @expectedException \Geocoder\Exception\UnsupportedException
38+
* @expectedExceptionMessage The IpInfoProvider does not support Street addresses.
39+
*/
40+
public function testGetGeocodedDataWithAddress()
41+
{
42+
$provider = new IpInfoProvider($this->getMockAdapter($this->never()));
43+
$provider->getGeocodedData('10 avenue Gambetta, Paris, France');
44+
}
45+
46+
public function testGetGeocodedDataWithLocalhostIPv4()
47+
{
48+
$provider = new IpInfoProvider($this->getMockAdapter($this->never()));
49+
$result = $provider->getGeocodedData('127.0.0.1');
50+
51+
$this->assertInternalType('array', $result);
52+
$this->assertCount(1, $result);
53+
54+
$result = $result[0];
55+
$this->assertInternalType('array', $result);
56+
$this->assertArrayNotHasKey('latitude', $result);
57+
$this->assertArrayNotHasKey('longitude', $result);
58+
$this->assertArrayNotHasKey('zipcode', $result);
59+
$this->assertArrayNotHasKey('timezone', $result);
60+
61+
$this->assertEquals('localhost', $result['city']);
62+
$this->assertEquals('localhost', $result['region']);
63+
$this->assertEquals('localhost', $result['county']);
64+
$this->assertEquals('localhost', $result['country']);
65+
}
66+
67+
public function testGetGeocodedDataWithLocalhostIPv6()
68+
{
69+
$provider = new IpInfoProvider($this->getMockAdapter($this->never()));
70+
$result = $provider->getGeocodedData('::1');
71+
72+
$this->assertInternalType('array', $result);
73+
$this->assertCount(1, $result);
74+
75+
$result = $result[0];
76+
$this->assertInternalType('array', $result);
77+
$this->assertArrayNotHasKey('latitude', $result);
78+
$this->assertArrayNotHasKey('longitude', $result);
79+
$this->assertArrayNotHasKey('zipcode', $result);
80+
$this->assertArrayNotHasKey('timezone', $result);
81+
82+
$this->assertEquals('localhost', $result['city']);
83+
$this->assertEquals('localhost', $result['region']);
84+
$this->assertEquals('localhost', $result['county']);
85+
$this->assertEquals('localhost', $result['country']);
86+
}
87+
88+
/**
89+
* @expectedException \Geocoder\Exception\NoResultException
90+
* @expectedExceptionMessage Could not execute query http://ipinfo.io/74.200.247.59/json
91+
*/
92+
public function testGetGeocodedDataWithRealIPv4GetsNullContent()
93+
{
94+
$provider = new IpInfoProvider($this->getMockAdapterReturns(null));
95+
$provider->getGeocodedData('74.200.247.59');
96+
}
97+
98+
/**
99+
* @expectedException \Geocoder\Exception\NoResultException
100+
* @expectedExceptionMessage Could not execute query http://ipinfo.io/74.200.247.59/json
101+
*/
102+
public function testGetGeocodedDataWithRealIPv4GetsEmptyContent()
103+
{
104+
$provider = new IpInfoProvider($this->getMockAdapterReturns(''));
105+
$provider->getGeocodedData('74.200.247.59');
106+
}
107+
108+
public function testGetGeocodedDataWithRealIPv4()
109+
{
110+
$json = <<<JSON
111+
{
112+
"ip": "74.200.247.59",
113+
"hostname": "wordpress.com",
114+
"city": "Plano",
115+
"region": "Texas",
116+
"country": "US",
117+
"loc": "33.0347,-96.8134",
118+
"org": "AS22576 Layered Technologies, Inc.",
119+
"postal": "75093"
120+
}
121+
JSON;
122+
123+
$provider = new IpInfoProvider($this->getMockAdapterReturns($json));
124+
$result = $provider->getGeocodedData('74.200.247.59');
125+
126+
$this->assertInternalType('array', $result);
127+
$this->assertCount(1, $result);
128+
129+
$result = $result[0];
130+
$this->assertInternalType('array', $result);
131+
$this->assertEquals(33.0347, $result['latitude'], '', 0.01);
132+
$this->assertEquals(-96.8134, $result['longitude'], '', 0.01);
133+
$this->assertEquals(75093, $result['zipcode']);
134+
$this->assertEquals('Plano', $result['city']);
135+
$this->assertEquals('Texas', $result['region']);
136+
$this->assertEquals('US', $result['countryCode']);
137+
}
138+
139+
public function testGetGeocodedDataWithRealIPv6()
140+
{
141+
$json = <<<JSON
142+
{
143+
"ip": "74.200.247.59",
144+
"hostname": "No Hostname",
145+
"city": "Plano",
146+
"region": "Texas",
147+
"country": "US",
148+
"loc": "33.0347,-96.8134",
149+
"org": "AS22576 Layered Technologies, Inc.",
150+
"postal": "75093"
151+
}
152+
JSON;
153+
154+
$provider = new IpInfoProvider($this->getMockAdapterReturns($json));
155+
$result = $provider->getGeocodedData('::ffff:74.200.247.59');
156+
157+
$this->assertInternalType('array', $result);
158+
$this->assertCount(1, $result);
159+
160+
$result = $result[0];
161+
$this->assertInternalType('array', $result);
162+
$this->assertEquals(33.0347, $result['latitude'], '', 0.01);
163+
$this->assertEquals(-96.8134, $result['longitude'], '', 0.01);
164+
$this->assertEquals(75093, $result['zipcode']);
165+
$this->assertEquals('Plano', $result['city']);
166+
$this->assertEquals('Texas', $result['region']);
167+
$this->assertEquals('US', $result['countryCode']);
168+
}
169+
170+
/**
171+
* @expectedException \Geocoder\Exception\NoResultException
172+
* @expectedExceptionMessage Could not execute query http://ipinfo.io/255.255.255.255/json
173+
*/
174+
public function testGetGeocodedDataWithoutLocation()
175+
{
176+
$json = <<<JSON
177+
{
178+
"ip": "255.255.255.255",
179+
"hostname": "No Hostname",
180+
"loc": "",
181+
"bogon": true
182+
}
183+
JSON;
184+
185+
$provider = new IpInfoProvider($this->getMockAdapterReturns($json));
186+
$provider->getGeocodedData('255.255.255.255');
187+
}
188+
189+
/**
190+
* @expectedException \Geocoder\Exception\NoResultException
191+
* @expectedExceptionMessage Could not execute query http://ipinfo.io/::ffff:74.200.247.59/json
192+
*/
193+
public function testGetGeocodedDataWithRealIPv6GetsNullContent()
194+
{
195+
$provider = new IpInfoProvider($this->getMockAdapterReturns(null));
196+
$provider->getGeocodedData('::ffff:74.200.247.59');
197+
}
198+
199+
/**
200+
* @expectedException \Geocoder\Exception\UnsupportedException
201+
* @expectedExceptionMessage The IpInfoProvider is not able to do reverse geocoding.
202+
*/
203+
public function testGetReverseData()
204+
{
205+
$provider = new IpInfoProvider($this->getMockAdapter($this->never()));
206+
$provider->getReversedData(array(1, 2));
207+
}
208+
}

0 commit comments

Comments
 (0)