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

Skip to content

Commit 86ddb57

Browse files
committed
add Naver provider, doc and tests
1 parent 771a428 commit 86ddb57

File tree

5 files changed

+270
-1
lines changed

5 files changed

+270
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor/
22
composer.lock
3+
phpunit.xml

README.md

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

3838
### Providers
3939

40-
* [OGD Vienna](https://open.wien.at/site/datensatz/?id=c223b93a-2634-4f06-ac73-8709b9e16888) as Address-Based geocoding provider (exclusively in Vienna / Austria).
40+
* [OGD Vienna](https://open.wien.at/site/datensatz/?id=c223b93a-2634-4f06-ac73-8709b9e16888) as Address-Based geocoding provider (exclusively in Vienna / Austria);
41+
* [Naver](http://developer.naver.com/wiki/pages/SrchAPI) as Address-Base geocoding provider (exclusively in South Korea).
4142

4243

4344
Contributing

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
>
1313
<php>
1414
<!-- API Keys -->
15+
<!-- <server name="NAVER_API_KEY" value="YOUR_API_KEY" /> -->
1516
</php>
1617
<testsuites>
1718
<testsuite name="geocoder-extra Test Suite">
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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\HttpAdapter\HttpAdapterInterface;
14+
use Geocoder\Exception\InvalidCredentialsException;
15+
use Geocoder\Exception\UnsupportedException;
16+
use Geocoder\Exception\NoResultException;
17+
18+
/**
19+
* @author Antoine Corcy <[email protected]>
20+
*/
21+
class NaverProvider extends AbstractProvider implements ProviderInterface
22+
{
23+
/**
24+
* @var string
25+
*/
26+
const ENDPOINT_URL = 'http://openapi.map.naver.com/api/geocode.php?key=%s&encoding=utf-8&coord=latlng&query=%s';
27+
28+
/**
29+
* @var string
30+
*/
31+
private $apiKey = null;
32+
33+
/**
34+
* @param HttpAdapterInterface $adapter An HTTP adapter.
35+
* @param string $apiKey An API key.
36+
*/
37+
public function __construct(HttpAdapterInterface $adapter, $apiKey)
38+
{
39+
parent::__construct($adapter);
40+
41+
$this->apiKey = $apiKey;
42+
}
43+
44+
/**
45+
* {@inheritDoc}
46+
*/
47+
public function getGeocodedData($address)
48+
{
49+
if (null === $this->apiKey) {
50+
throw new InvalidCredentialsException('No API Key provided');
51+
}
52+
53+
// This API doesn't handle IPs
54+
if (filter_var($address, FILTER_VALIDATE_IP)) {
55+
throw new UnsupportedException('The NaverProvider does not support IP addresses.');
56+
}
57+
58+
$query = sprintf(self::ENDPOINT_URL, $this->apiKey, rawurlencode($address));
59+
60+
try {
61+
$result = new \SimpleXmlElement($this->getAdapter()->getContent($query));
62+
} catch (\Exception $e) {
63+
throw new NoResultException(sprintf('Could not execute query %s', $query));
64+
}
65+
66+
if (0 === (int) $result->total) {
67+
throw new NoResultException(sprintf('Could not execute query %s', $query));
68+
}
69+
70+
return array(array_merge($this->getDefaults(), array(
71+
'latitude' => (double) $result->item->point->x,
72+
'longitude' => (double) $result->item->point->y,
73+
'region' => isset($result->item->addrdetail->sido)
74+
? trim((string) $result->item->addrdetail->sido) : null,
75+
'city' => isset($result->item->addrdetail->sido->sigugun)
76+
? trim((string) $result->item->addrdetail->sido->sigugun) : null,
77+
'streetName' => isset($result->item->addrdetail->sido->sigugun->dongmyun)
78+
? trim((string) $result->item->addrdetail->sido->sigugun->dongmyun) : null,
79+
'streetNumber' => isset($result->item->addrdetail->sido->sigugun->dongmyun->rest)
80+
? (string) $result->item->addrdetail->sido->sigugun->dongmyun->rest : null,
81+
)));
82+
}
83+
84+
/**
85+
* {@inheritDoc}
86+
*/
87+
public function getReversedData(array $coordinates)
88+
{
89+
throw new UnsupportedException('The NaverProvider is not able to do reverse geocoding.');
90+
}
91+
92+
/**
93+
* {@inheritDoc}
94+
*/
95+
public function getName()
96+
{
97+
return 'naver';
98+
}
99+
}
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
3+
namespace Geocoder\Tests\Provider;
4+
5+
use Geocoder\Tests\TestCase;
6+
use Geocoder\Provider\NaverProvider;
7+
8+
/**
9+
* @author Antoine Corcy <[email protected]>
10+
*/
11+
class NaverProviderTest extends TestCase
12+
{
13+
public function testGetName()
14+
{
15+
$provider = new NaverProvider($this->getMockAdapter($this->never()), 'api_key');
16+
$this->assertEquals('naver', $provider->getName());
17+
}
18+
19+
/**
20+
* @expectedException \RuntimeException
21+
*/
22+
public function testGetGeocodedDataWithNullApiKey()
23+
{
24+
$provider = new NaverProvider($this->getMockAdapter($this->never()), null);
25+
$provider->getGeocodedData('foo');
26+
}
27+
28+
/**
29+
* @expectedException \Geocoder\Exception\NoResultException
30+
* @expectedExceptionMessage Could not execute query http://openapi.map.naver.com/api/geocode.php?key=api_key&encoding=utf-8&coord=latlng&query=%EC%84%9C%EC%9A%B8
31+
*/
32+
public function testGetGeocodedDataWithAddress()
33+
{
34+
$provider = new NaverProvider($this->getMockAdapter(), 'api_key');
35+
$provider->getGeocodedData('서울');
36+
}
37+
38+
/**
39+
* @expectedException \Geocoder\Exception\NoResultException
40+
* @expectedExceptionMessage Could not execute query http://openapi.map.naver.com/api/geocode.php?key=api_key&encoding=utf-8&coord=latlng&query=foobar
41+
*/
42+
public function testGetGeocodedDataWithAddressGetsZeroResult()
43+
{
44+
$xml = <<<XML
45+
<geocode><userquery>foobar</userquery><total>0</total></geocode>
46+
XML;
47+
48+
$provider = new NaverProvider($this->getMockAdapterReturns($xml), 'api_key');
49+
$provider->getGeocodedData('foobar');
50+
}
51+
52+
public function testGetGeocodedDataWithAddressGetsOneResult()
53+
{
54+
$xml = <<<XML
55+
<geocode>
56+
<userquery>경북 영천시 임고면 매호리 143-9번지</userquery>
57+
<total>1</total>
58+
<item>
59+
<point>
60+
<x>128.9675615</x>
61+
<y>36.0062826</y>
62+
</point>
63+
<address>경상북도 영천시 임고면 매호리 143-9</address>
64+
<addrdetail>
65+
<sido>
66+
경상북도
67+
<sigugun>
68+
영천시 임고면
69+
<dongmyun>
70+
매호리
71+
<rest>143-9</rest>
72+
</dongmyun>
73+
</sigugun>
74+
</sido>
75+
</addrdetail>
76+
</item>
77+
</geocode>
78+
XML;
79+
80+
$provider = new NaverProvider($this->getMockAdapterReturns($xml), 'api_key');
81+
$results = $provider->getGeocodedData('경북 영천시 임고면 매호리 143-9번지');
82+
83+
$this->assertInternalType('array', $results);
84+
$this->assertCount(1, $results);
85+
86+
$result = $results[0];
87+
$this->assertInternalType('array', $result);
88+
$this->assertEquals(128.9675615, $result['latitude'], '', 0.01);
89+
$this->assertEquals(36.0062826, $result['longitude'], '', 0.01);
90+
$this->assertNull($result['bounds']);
91+
92+
$this->assertEquals('143-9', $result['streetNumber']);
93+
$this->assertEquals('매호리', $result['streetName']);
94+
$this->assertEquals('영천시 임고면', $result['city']);
95+
$this->assertNull($result['zipcode']);
96+
$this->assertNull($result['cityDistrict']);
97+
$this->assertNull($result['county']);
98+
$this->assertNull($result['countyCode']);
99+
$this->assertEquals('경상북도', $result['region']);
100+
$this->assertNull($result['regionCode']);
101+
$this->assertNull($result['country']);
102+
$this->assertNull($result['countryCode']);
103+
$this->assertNull($result['timezone']);
104+
}
105+
106+
public function testGetGeocodedDataWithRealAddress()
107+
{
108+
if (!isset($_SERVER['NAVER_API_KEY'])) {
109+
$this->markTestSkipped('You need to configure the NAVER_API_KEY value in phpunit.xml');
110+
}
111+
112+
$provider = new NaverProvider($this->getAdapter(), $_SERVER['NAVER_API_KEY']);
113+
$results = $provider->getGeocodedData('경북 영천시 임고면 매호리 143-9번지');
114+
115+
$this->assertInternalType('array', $results);
116+
$this->assertCount(1, $results);
117+
118+
$result = $results[0];
119+
$this->assertInternalType('array', $result);
120+
$this->assertEquals(128.9675615, $result['latitude'], '', 0.01);
121+
$this->assertEquals(36.0062826, $result['longitude'], '', 0.01);
122+
$this->assertNull($result['bounds']);
123+
124+
$this->assertEquals('143-9', $result['streetNumber']);
125+
$this->assertEquals('매호리', $result['streetName']);
126+
$this->assertEquals('영천시 임고면', $result['city']);
127+
$this->assertNull($result['zipcode']);
128+
$this->assertNull($result['cityDistrict']);
129+
$this->assertNull($result['county']);
130+
$this->assertNull($result['countyCode']);
131+
$this->assertEquals('경상북도', $result['region']);
132+
$this->assertNull($result['regionCode']);
133+
$this->assertNull($result['country']);
134+
$this->assertNull($result['countryCode']);
135+
$this->assertNull($result['timezone']);
136+
}
137+
138+
/**
139+
* @expectedException \Geocoder\Exception\UnsupportedException
140+
* @expectedExceptionMessage The NaverProvider does not support IP addresses.
141+
*/
142+
public function testGetGeocodedDataWithIPv4()
143+
{
144+
$provider = new NaverProvider($this->getAdapter(), 'api_key');
145+
$provider->getGeocodedData('74.200.247.59');
146+
}
147+
148+
/**
149+
* @expectedException \Geocoder\Exception\UnsupportedException
150+
* @expectedExceptionMessage The NaverProvider does not support IP addresses.
151+
*/
152+
public function testGetGeocodedDataWithIPv6()
153+
{
154+
$provider = new NaverProvider($this->getAdapter(), 'api_key');
155+
$provider->getGeocodedData('::ffff:74.200.247.59');
156+
}
157+
158+
/**
159+
* @expectedException \Geocoder\Exception\UnsupportedException
160+
* @expectedExceptionMessage The NaverProvider is not able to do reverse geocoding.
161+
*/
162+
public function testGetReverseData()
163+
{
164+
$provider = new NaverProvider($this->getAdapter(), 'api_key');
165+
$provider->getReversedData(array(1, 2));
166+
}
167+
}

0 commit comments

Comments
 (0)