Skip to content

Commit 87b72de

Browse files
committed
Merge pull request #319 from jpb0104/mapquest-licensed-endpoints
Added support in MapQuestProvider for licensed endpoints #318
2 parents 9452d83 + 7e2e59e commit 87b72de

File tree

3 files changed

+81
-14
lines changed

3 files changed

+81
-14
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ The `ChainProvider` named `chain` is a special provider that takes a list of pro
196196
### MapQuestProvider ###
197197

198198
The `MapQuestProvider` named `map_quest` is able to geocode and reverse geocode **street addresses**.
199-
A valid api key is required.
199+
A valid api key is required. Access to [MapQuest's licensed endpoints](http://developer.mapquest.com/web/tools/getting-started/platform/licensed-vs-open)
200+
is provided via constructor agrument.
200201

201202

202203
### OIORestProvider ###

src/Geocoder/Provider/MapQuestProvider.php

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,48 @@ class MapQuestProvider extends AbstractProvider implements ProviderInterface
2323
/**
2424
* @var string
2525
*/
26-
const GEOCODE_ENDPOINT_URL = 'http://open.mapquestapi.com/geocoding/v1/address?location=%s&outFormat=json&maxResults=%d&key=%s&thumbMaps=false';
26+
const OPEN_GEOCODE_ENDPOINT_URL = 'http://open.mapquestapi.com/geocoding/v1/address?location=%s&outFormat=json&maxResults=%d&key=%s&thumbMaps=false';
2727

2828
/**
2929
* @var string
3030
*/
31-
const REVERSE_ENDPOINT_URL = 'http://open.mapquestapi.com/geocoding/v1/reverse?key=%s&lat=%F&lng=%F';
31+
const OPEN_REVERSE_ENDPOINT_URL = 'http://open.mapquestapi.com/geocoding/v1/reverse?key=%s&lat=%F&lng=%F';
32+
33+
/**
34+
* @var string
35+
*/
36+
const LICENSED_GEOCODE_ENDPOINT_URL = 'http://www.mapquestapi.com/geocoding/v1/address?location=%s&outFormat=json&maxResults=%d&key=%s&thumbMaps=false';
37+
38+
/**
39+
* @var string
40+
*/
41+
const LICENSED_REVERSE_ENDPOINT_URL = 'http://www.mapquestapi.com/geocoding/v1/reverse?key=%s&lat=%F&lng=%F';
42+
43+
/**
44+
* MapQuest offers two geocoding endpoints one commercial (true) and one open (false)
45+
* More information: http://developer.mapquest.com/web/tools/getting-started/platform/licensed-vs-open
46+
*
47+
* @var bool
48+
*/
49+
protected $licensed = false;
3250

3351
/**
3452
* @var string
3553
*/
3654
private $apiKey = null;
3755

3856
/**
39-
* @param HttpAdapterInterface $adapter An HTTP adapter.
40-
* @param string $apiKey An API key.
41-
* @param string $locale A locale (optional).
57+
* @param HttpAdapterInterface $adapter An HTTP adapter.
58+
* @param string $apiKey An API key.
59+
* @param string|null $locale A locale (optional).
60+
* @param bool $licensed True to use MapQuest's licensed endpoints, default is false to use the open endpoints (optional).
4261
*/
43-
public function __construct(HttpAdapterInterface $adapter, $apiKey, $locale = null)
62+
public function __construct(HttpAdapterInterface $adapter, $apiKey, $locale = null, $licensed = false)
4463
{
4564
parent::__construct($adapter, $locale);
4665

4766
$this->apiKey = $apiKey;
67+
$this->licensed = $licensed;
4868
}
4969

5070
/**
@@ -61,7 +81,11 @@ public function getGeocodedData($address)
6181
throw new InvalidCredentialsException('No API Key provided.');
6282
}
6383

64-
$query = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $this->getMaxResults(), $this->apiKey);
84+
if ($this->licensed) {
85+
$query = sprintf(self::LICENSED_GEOCODE_ENDPOINT_URL, urlencode($address), $this->getMaxResults(), $this->apiKey);
86+
} else {
87+
$query = sprintf(self::OPEN_GEOCODE_ENDPOINT_URL, urlencode($address), $this->getMaxResults(), $this->apiKey);
88+
}
6589

6690
return $this->executeQuery($query);
6791
}
@@ -75,7 +99,11 @@ public function getReversedData(array $coordinates)
7599
throw new InvalidCredentialsException('No API Key provided.');
76100
}
77101

78-
$query = sprintf(self::REVERSE_ENDPOINT_URL, $this->apiKey, $coordinates[0], $coordinates[1]);
102+
if ($this->licensed) {
103+
$query = sprintf(self::LICENSED_REVERSE_ENDPOINT_URL, $this->apiKey, $coordinates[0], $coordinates[1]);
104+
} else {
105+
$query = sprintf(self::OPEN_REVERSE_ENDPOINT_URL, $this->apiKey, $coordinates[0], $coordinates[1]);
106+
}
79107

80108
return $this->executeQuery($query);
81109
}

tests/Geocoder/Tests/Provider/MapQuestProviderTest.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function testGetGeocodedDataWithAddressGetsNullContent()
3939
public function testGetGeocodedDataWithRealAddress()
4040
{
4141
if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
42-
$this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml');
42+
$this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml');
4343
}
4444

4545
$provider = new MapQuestProvider($this->getAdapter(), $_SERVER['MAPQUEST_API_KEY']);
@@ -71,7 +71,7 @@ public function testGetGeocodedDataWithRealAddress()
7171
public function testGetReversedData()
7272
{
7373
if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
74-
$this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml');
74+
$this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml');
7575
}
7676

7777
$provider = new MapQuestProvider($this->getMockAdapter(), $_SERVER['MAPQUEST_API_KEY']);
@@ -81,7 +81,7 @@ public function testGetReversedData()
8181
public function testGetReversedDataWithRealCoordinates()
8282
{
8383
if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
84-
$this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml');
84+
$this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml');
8585
}
8686

8787
$provider = new MapQuestProvider($this->getAdapter(), $_SERVER['MAPQUEST_API_KEY']);
@@ -110,7 +110,7 @@ public function testGetReversedDataWithRealCoordinates()
110110
public function testGetGeocodedDataWithCity()
111111
{
112112
if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
113-
$this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml');
113+
$this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml');
114114
}
115115

116116
$provider = new MapQuestProvider($this->getAdapter(), $_SERVER['MAPQUEST_API_KEY']);
@@ -154,7 +154,7 @@ public function testGetGeocodedDataWithCity()
154154
public function testGetGeocodedDataWithCityDistrict()
155155
{
156156
if (!isset($_SERVER['MAPQUEST_API_KEY'])) {
157-
$this->markTestSkipped('You need to configure the CLOUDMADE_API_KEY value in phpunit.xml');
157+
$this->markTestSkipped('You need to configure the MAPQUEST_API_KEY value in phpunit.xml');
158158
}
159159

160160
$provider = new MapQuestProvider($this->getAdapter(), $_SERVER['MAPQUEST_API_KEY']);
@@ -220,4 +220,42 @@ public function testGetGeocodedDataWithRealIPv6()
220220
$provider = new MapQuestProvider($this->getAdapter(), 'api_key');
221221
$provider->getGeocodedData('::ffff:74.200.247.59');
222222
}
223+
224+
public function testLicensedVsOpenGeocodeEndpoints()
225+
{
226+
$licensed = false;
227+
$provider = new MockMapQuestProvider($this->getAdapter(), 'api_key', null, $licensed);
228+
$queryUrl = $provider->getGeocodedData('Hanover');
229+
$this->assertContains('http://open.', $queryUrl);
230+
231+
$licensed = true;
232+
$provider = new MockMapQuestProvider($this->getAdapter(), 'api_key', null, $licensed);
233+
$queryUrl = $provider->getGeocodedData('Hanover');
234+
$this->assertContains('http://www.', $queryUrl);
235+
}
236+
237+
public function testLicensedVsOpenReverseGeocodeEndpoints()
238+
{
239+
$licensed = false;
240+
$provider = new MockMapQuestProvider($this->getAdapter(), 'api_key', null, $licensed);
241+
$queryUrl = $provider->getReversedData(array(54.0484068, -2.7990345));
242+
$this->assertContains('http://open.', $queryUrl);
243+
244+
$licensed = true;
245+
$provider = new MockMapQuestProvider($this->getAdapter(), 'api_key', null, $licensed);
246+
$queryUrl = $provider->getReversedData(array(54.0484068, -2.7990345));
247+
$this->assertContains('http://www.', $queryUrl);
248+
}
249+
}
250+
251+
class MockMapQuestProvider extends MapQuestProvider
252+
{
253+
/**
254+
* Short circuits so assertions can inspect the
255+
* executed query URL
256+
*/
257+
protected function executeQuery($query)
258+
{
259+
return $query;
260+
}
223261
}

0 commit comments

Comments
 (0)