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

Skip to content

Commit a79fdcf

Browse files
committed
Added Telize Provider
1 parent fb65249 commit a79fdcf

File tree

2 files changed

+299
-0
lines changed

2 files changed

+299
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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\NoResultException;
14+
use Geocoder\Exception\UnsupportedException;
15+
use Geocoder\HttpAdapter\HttpAdapterInterface;
16+
17+
/**
18+
* @author Tudor Matei <[email protected]>
19+
*/
20+
class TelizeProvider extends AbstractProvider implements ProviderInterface
21+
{
22+
/**
23+
* @var string
24+
*/
25+
const GEOIP_ENDPOINT_URL = 'http://www.telize.com/geoip';
26+
27+
/**
28+
* @param HttpAdapterInterface $adapter An HTTP adapter.
29+
* @param string $locale A locale (optional).
30+
*/
31+
public function __construct(HttpAdapterInterface $adapter, $locale = null)
32+
{
33+
parent::__construct($adapter, $locale);
34+
}
35+
36+
/**
37+
* {@inheritDoc}
38+
*/
39+
public function getGeocodedData($address)
40+
{
41+
if (!filter_var($address, FILTER_VALIDATE_IP)) {
42+
throw new UnsupportedException('The TelizeProvider does not support street addresses.');
43+
}
44+
45+
if (in_array($address, array('127.0.0.1', '::1'))) {
46+
return array($this->getLocalhostDefaults());
47+
}
48+
49+
$query = self::GEOIP_ENDPOINT_URL . '/' . rawurlencode($address);
50+
51+
return $this->executeQuery($query);
52+
}
53+
54+
/**
55+
* {@inheritDoc}
56+
*/
57+
public function getReversedData(array $coordinates)
58+
{
59+
throw new UnsupportedException('The TelizeProvider is not able to do reverse geocoding.');
60+
}
61+
62+
/**
63+
* @param string $query
64+
*
65+
* @throws \Geocoder\Exception\NoResultException
66+
* @return array
67+
*/
68+
protected function executeQuery($query)
69+
{
70+
$content = $this->getAdapter()->getContent($query);
71+
72+
if (null === $content || '' === $content) {
73+
throw new NoResultException(sprintf('Could not execute query %s', $query));
74+
}
75+
76+
$data = json_decode($content, true);
77+
78+
if (isset($data['code']) && $data['code'] == 401) {
79+
throw new NoResultException('Input string is not a valid IP address.');
80+
}
81+
82+
if (!isset($data['ip'])) {
83+
throw new NoResultException('Invalid result returned by provider.');
84+
}
85+
86+
return array(array_merge($this->getDefaults(), array(
87+
'latitude' => isset($data['latitude']) ? $data['latitude'] : null,
88+
'longitude' => isset($data['longitude']) ? $data['longitude'] : null,
89+
'city' => isset($data['city']) ? $data['city'] : null,
90+
'zipcode' => isset($data['postal_code']) ? $data['postal_code'] : null,
91+
'region' => isset($data['region']) ? $data['region'] : null,
92+
'regionCode' => isset($data['region_code']) ? $data['region_code'] : null,
93+
'country' => isset($data['country']) ? $data['country'] : null,
94+
'countryCode' => isset($data['country_code']) ? $data['country_code'] : null,
95+
'timezone' => isset($data['timezone']) ? $data['timezone'] : null,
96+
)));
97+
}
98+
99+
/**
100+
* {@inheritDoc}
101+
*/
102+
public function getName()
103+
{
104+
return 'telize';
105+
}
106+
107+
}
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
3+
namespace Geocoder\Tests\Provider;
4+
5+
use Geocoder\Tests\TestCase;
6+
use Geocoder\Provider\TelizeProvider;
7+
8+
/**
9+
* @author Tudor Matei <[email protected]>
10+
*/
11+
class TelizeBaseProviderTest extends TestCase
12+
{
13+
public function testGetName()
14+
{
15+
$provider = new TelizeProvider($this->getMockAdapter($this->never()));
16+
$this->assertEquals('telize', $provider->getName());
17+
}
18+
19+
/**
20+
* @expectedException \Geocoder\Exception\UnsupportedException
21+
* @expectedExceptionMessage The TelizeProvider does not support street addresses.
22+
*/
23+
public function testGetGeocodedDataWithNull()
24+
{
25+
$provider = new TelizeProvider($this->getMockAdapter($this->never()));
26+
$provider->getGeocodedData(null);
27+
}
28+
29+
/**
30+
* @expectedException \Geocoder\Exception\UnsupportedException
31+
* @expectedExceptionMessage The TelizeProvider does not support street addresses.
32+
*/
33+
public function testGetGeocodedDataWithEmpty()
34+
{
35+
$provider = new TelizeProvider($this->getMockAdapter($this->never()));
36+
$provider->getGeocodedData('');
37+
}
38+
39+
/**
40+
* @expectedException \Geocoder\Exception\UnsupportedException
41+
* @expectedExceptionMessage The TelizeProvider does not support street addresses.
42+
*/
43+
public function testGetGeocodedDataWithAddress()
44+
{
45+
$provider = new TelizeProvider($this->getMockAdapter($this->never()));
46+
$provider->getGeocodedData('10 avenue Gambetta, Paris, France');
47+
}
48+
49+
public function testGetGeocodedDataWithLocalhostIPv4()
50+
{
51+
$provider = new TelizeProvider($this->getMockAdapter($this->never()));
52+
$result = $provider->getGeocodedData('127.0.0.1');
53+
54+
$this->assertInternalType('array', $result);
55+
$this->assertCount(1, $result);
56+
57+
$result = $result[0];
58+
$this->assertInternalType('array', $result);
59+
$this->assertArrayNotHasKey('latitude', $result);
60+
$this->assertArrayNotHasKey('longitude', $result);
61+
$this->assertArrayNotHasKey('zipcode', $result);
62+
$this->assertArrayNotHasKey('timezone', $result);
63+
64+
$this->assertEquals('localhost', $result['city']);
65+
$this->assertEquals('localhost', $result['region']);
66+
$this->assertEquals('localhost', $result['county']);
67+
$this->assertEquals('localhost', $result['country']);
68+
}
69+
70+
public function testGetGeocodedDataWithLocalhostIPv6()
71+
{
72+
$provider = new TelizeProvider($this->getMockAdapter($this->never()));
73+
$result = $provider->getGeocodedData('::1');
74+
75+
$this->assertInternalType('array', $result);
76+
$this->assertCount(1, $result);
77+
78+
$result = $result[0];
79+
$this->assertInternalType('array', $result);
80+
$this->assertArrayNotHasKey('latitude', $result);
81+
$this->assertArrayNotHasKey('longitude', $result);
82+
$this->assertArrayNotHasKey('zipcode', $result);
83+
$this->assertArrayNotHasKey('timezone', $result);
84+
85+
$this->assertEquals('localhost', $result['city']);
86+
$this->assertEquals('localhost', $result['region']);
87+
$this->assertEquals('localhost', $result['county']);
88+
$this->assertEquals('localhost', $result['country']);
89+
}
90+
91+
/**
92+
* @expectedException \Geocoder\Exception\NoResultException
93+
* @expectedExceptionMessage Could not execute query http://www.telize.com/geoip/88.188.221.14
94+
*/
95+
public function testGetGeocodedDataWithRealIPv4GetsNullContent()
96+
{
97+
$provider = new TelizeProvider($this->getMockAdapterReturns(null));
98+
$provider->getGeocodedData('88.188.221.14');
99+
}
100+
101+
/**
102+
* @expectedException \Geocoder\Exception\NoResultException
103+
* @expectedExceptionMessage Could not execute query http://www.telize.com/geoip/88.188.221.14
104+
*/
105+
public function testGetGeocodedDataWithRealIPv4GetsEmptyContent()
106+
{
107+
$provider = new TelizeProvider($this->getMockAdapterReturns(''));
108+
$provider->getGeocodedData('88.188.221.14');
109+
}
110+
111+
public function testGetGeocodedDataWithRealIPv4UnitedStates()
112+
{
113+
$provider = new TelizeProvider($this->getAdapter());
114+
$result = $provider->getGeocodedData('74.200.247.59');
115+
116+
$this->assertInternalType('array', $result);
117+
$this->assertCount(1, $result);
118+
119+
$result = $result[0];
120+
$this->assertInternalType('array', $result);
121+
$this->assertEquals(33.0347, $result['latitude'], '', 0.0001);
122+
$this->assertEquals(-96.8134, $result['longitude'], '', 0.0001);
123+
$this->assertNull($result['streetNumber']);
124+
$this->assertNull($result['streetName']);
125+
$this->assertEquals('Plano', $result['city']);
126+
$this->assertEquals('75093', $result['zipcode']);
127+
$this->assertNull($result['cityDistrict']);
128+
$this->assertEquals('Texas', $result['region']);
129+
$this->assertEquals('TX', $result['regionCode']);
130+
$this->assertEquals('United States', $result['country']);
131+
$this->assertEquals('US', $result['countryCode']);
132+
$this->assertEquals('America/Chicago', $result['timezone']);
133+
}
134+
135+
public function testGetGeocodedDataWithRealIPv4France()
136+
{
137+
$provider = new TelizeProvider($this->getAdapter());
138+
$result = $provider->getGeocodedData('88.188.221.14');
139+
140+
$this->assertInternalType('array', $result);
141+
$this->assertCount(1, $result);
142+
143+
$result = $result[0];
144+
$this->assertInternalType('array', $result);
145+
$this->assertEquals(45.7797, $result['latitude'], '', 0.0001);
146+
$this->assertEquals(3.0863, $result['longitude'], '', 0.0001);
147+
$this->assertNull($result['streetNumber']);
148+
$this->assertNull($result['streetName']);
149+
$this->assertEquals('Clermont', $result['city']);
150+
$this->assertEquals('63023', $result['zipcode']);
151+
$this->assertNull($result['cityDistrict']);
152+
$this->assertEquals('Auvergne', $result['region']);
153+
$this->assertEquals('98', $result['regionCode']);
154+
$this->assertEquals('France', $result['country']);
155+
$this->assertEquals('FR', $result['countryCode']);
156+
$this->assertEquals('Europe/Paris', $result['timezone']);
157+
}
158+
159+
public function testGetGeocodedDataWithRealIPv6France()
160+
{
161+
$provider = new TelizeProvider($this->getAdapter());
162+
$result = $provider->getGeocodedData('::ffff:88.188.221.14');
163+
164+
$this->assertInternalType('array', $result);
165+
$this->assertCount(1, $result);
166+
167+
$result = $result[0];
168+
$this->assertInternalType('array', $result);
169+
$this->assertEquals(45.7797, $result['latitude'], '', 0.0001);
170+
$this->assertEquals(3.0863, $result['longitude'], '', 0.0001);
171+
$this->assertNull($result['streetNumber']);
172+
$this->assertNull($result['streetName']);
173+
$this->assertEquals('Clermont', $result['city']);
174+
$this->assertEquals('63023', $result['zipcode']);
175+
$this->assertNull($result['cityDistrict']);
176+
$this->assertEquals('Auvergne', $result['region']);
177+
$this->assertEquals('98', $result['regionCode']);
178+
$this->assertEquals('France', $result['country']);
179+
$this->assertEquals('FR', $result['countryCode']);
180+
$this->assertEquals('Europe/Paris', $result['timezone']);
181+
}
182+
183+
/**
184+
* @expectedException \Geocoder\Exception\UnsupportedException
185+
* @expectedExceptionMessage The TelizeProvider is not able to do reverse geocoding.
186+
*/
187+
public function testGetReverseData()
188+
{
189+
$provider = new TelizeProvider($this->getMockAdapter($this->never()));
190+
$provider->getReversedData(array(1, 2));
191+
}
192+
}

0 commit comments

Comments
 (0)