Skip to content

Commit 43696e2

Browse files
DamienHarperjbelien
authored andcommitted
Add support to Here CIT environment (#955)
* Add support to CIT environment. * PHPDoc fix * Tests regarding CIT environment. * PHPDoc fix
1 parent 3191b98 commit 43696e2

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

Here.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ final class Here extends AbstractHttpProvider implements Provider
3939
*/
4040
const REVERSE_ENDPOINT_URL = 'https://reverse.geocoder.api.here.com/6.2/reversegeocode.json?prox=%F,%F&250&app_id=%s&app_code=%s&mode=retrieveAddresses&gen=8&maxresults=%d';
4141

42+
/**
43+
* @var string
44+
*/
45+
const GEOCODE_CIT_ENDPOINT_URL = 'https://geocoder.cit.api.here.com/6.2/geocode.json?app_id=%s&app_code=%s&searchtext=%s&gen=8';
46+
47+
/**
48+
* @var string
49+
*/
50+
const REVERSE_CIT_ENDPOINT_URL = 'https://reverse.geocoder.cit.api.here.com/6.2/reversegeocode.json?prox=%F,%F&250&app_id=%s&app_code=%s&mode=retrieveAddresses&gen=8&maxresults=%d';
51+
4252
/**
4353
* @var string
4454
*/
@@ -50,17 +60,24 @@ final class Here extends AbstractHttpProvider implements Provider
5060
private $appCode;
5161

5262
/**
53-
* @param HttpClient $adapter An HTTP adapter.
63+
* @var bool
64+
*/
65+
private $useCIT;
66+
67+
/**
68+
* @param HttpClient $client An HTTP adapter.
5469
* @param string $appId An App ID.
55-
* @param string $apoCode An App code.
70+
* @param string $appCode An App code.
71+
* @param bool $useCIT Use Customer Integration Testing environment (CIT) instead of production.
5672
*/
57-
public function __construct(HttpClient $client, string $appId, string $appCode)
73+
public function __construct(HttpClient $client, string $appId, string $appCode, bool $useCIT = false)
5874
{
5975
if (empty($appId) || empty($appCode)) {
6076
throw new InvalidCredentials('Invalid or missing api key.');
6177
}
6278
$this->appId = $appId;
6379
$this->appCode = $appCode;
80+
$this->useCIT = $useCIT;
6481

6582
parent::__construct($client);
6683
}
@@ -75,7 +92,7 @@ public function geocodeQuery(GeocodeQuery $query): Collection
7592
throw new UnsupportedOperation('The Here provider does not support IP addresses, only street addresses.');
7693
}
7794

78-
$url = sprintf(self::GEOCODE_ENDPOINT_URL, $this->appId, $this->appCode, rawurlencode($query->getText()));
95+
$url = sprintf($this->useCIT ? self::GEOCODE_CIT_ENDPOINT_URL : self::GEOCODE_ENDPOINT_URL, $this->appId, $this->appCode, rawurlencode($query->getText()));
7996

8097
if (null !== $query->getLocale()) {
8198
$url = sprintf('%s&language=%s', $url, $query->getLocale());
@@ -90,14 +107,13 @@ public function geocodeQuery(GeocodeQuery $query): Collection
90107
public function reverseQuery(ReverseQuery $query): Collection
91108
{
92109
$coordinates = $query->getCoordinates();
93-
$url = sprintf(self::REVERSE_ENDPOINT_URL, $coordinates->getLatitude(), $coordinates->getLongitude(), $this->appId, $this->appCode, $query->getLimit());
110+
$url = sprintf($this->useCIT ? self::REVERSE_CIT_ENDPOINT_URL : self::REVERSE_ENDPOINT_URL, $coordinates->getLatitude(), $coordinates->getLongitude(), $this->appId, $this->appCode, $query->getLimit());
94111

95112
return $this->executeQuery($url, $query->getLimit());
96113
}
97114

98115
/**
99116
* @param string $url
100-
* @param string $locale
101117
* @param int $limit
102118
*
103119
* @return \Geocoder\Collection
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:998:"{"Response":{"MetaInfo":{"Timestamp":"2019-05-14T11:59:07.488+0000"},"View":[{"_type":"SearchResultsViewType","ViewId":0,"Result":[{"Relevance":1.0,"MatchLevel":"houseNumber","MatchQuality":{"Country":1.0,"City":1.0,"Street":[1.0],"HouseNumber":1.0},"MatchType":"pointAddress","Location":{"LocationId":"NT_lWsc8knsFwVitNTFX88zmA_xAD","LocationType":"address","DisplayPosition":{"Latitude":51.50341,"Longitude":-0.12765},"NavigationPosition":[{"Latitude":51.50322,"Longitude":-0.12767}],"MapView":{"TopLeft":{"Latitude":51.5045342,"Longitude":-0.129456},"BottomRight":{"Latitude":51.5022858,"Longitude":-0.125844}},"Address":{"Label":"10 Downing Street, London, SW1A 2AA, United Kingdom","Country":"GBR","State":"England","County":"London","City":"London","District":"Westminster","Street":"Downing Street","HouseNumber":"10","PostalCode":"SW1A 2AA","AdditionalData":[{"value":"United Kingdom","key":"CountryName"},{"value":"England","key":"StateName"},{"value":"London","key":"CountyName"}]}}}]}]}}";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:2297:"{"Response":{"MetaInfo":{"Timestamp":"2019-05-14T14:25:50.744+0000"},"View":[{"_type":"SearchResultsViewType","ViewId":0,"Result":[{"Relevance":1.0,"Distance":-0.6,"Direction":326.1,"MatchLevel":"district","MatchQuality":{"Country":1.0,"State":1.0,"County":1.0,"City":1.0,"District":1.0,"PostalCode":1.0},"Location":{"LocationId":"NT_oGdFab4eWiYVcjO-u0pTFB","LocationType":"area","DisplayPosition":{"Latitude":38.90357,"Longitude":-77.0399},"MapView":{"TopLeft":{"Latitude":38.90939,"Longitude":-77.04974},"BottomRight":{"Latitude":38.89599,"Longitude":-77.03363}},"Address":{"Label":"Connecticut Avenue/K Street, Washington, DC, United States","Country":"USA","State":"DC","County":"District of Columbia","City":"Washington","District":"Connecticut Avenue/K Street","PostalCode":"20036","AdditionalData":[{"value":"United States","key":"CountryName"},{"value":"District of Columbia","key":"StateName"},{"value":"District of Columbia","key":"CountyName"},{"value":"N","key":"PostalCodeType"}]},"MapReference":{"ReferenceId":"1219975889","MapId":"NAAM191N0","MapVersion":"Q1/2019","MapReleaseDate":"2019-01-28","SideOfStreet":"neither","CountryId":"21000001","StateId":"21022302","CountyId":"21022303","CityId":"21022306"}}},{"Relevance":1.0,"Distance":0.7,"Direction":163.6,"MatchLevel":"district","MatchQuality":{"Country":1.0,"State":1.0,"County":1.0,"City":1.0,"District":1.0,"PostalCode":1.0},"Location":{"LocationId":"NT_zeyJ5M16Ci53y9KE8RMTGD","LocationType":"area","DisplayPosition":{"Latitude":38.89142,"Longitude":-77.03367},"MapView":{"TopLeft":{"Latitude":38.90021,"Longitude":-77.05679},"BottomRight":{"Latitude":38.88508,"Longitude":-77.00202}},"Address":{"Label":"Washington Mall, Washington, DC, United States","Country":"USA","State":"DC","County":"District of Columbia","City":"Washington","District":"Washington Mall","PostalCode":"20004","AdditionalData":[{"value":"United States","key":"CountryName"},{"value":"District of Columbia","key":"StateName"},{"value":"District of Columbia","key":"CountyName"},{"value":"N","key":"PostalCodeType"}]},"MapReference":{"ReferenceId":"1114357553","MapId":"NAAM191N0","MapVersion":"Q1/2019","MapReleaseDate":"2019-01-28","SideOfStreet":"neither","CountryId":"21000001","StateId":"21022302","CountyId":"21022303","CityId":"21022306"}}}]}]}}";

Tests/IntegrationTest.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ class IntegrationTest extends ProviderIntegrationTest
3434

3535
protected $testIpv6 = false;
3636

37-
protected function createProvider(HttpClient $httpClient)
37+
protected function createProvider(HttpClient $httpClient, bool $useCIT = false)
3838
{
39-
return new Here($httpClient, $this->getAppId(), $this->getAppCode());
39+
return new Here($httpClient, $this->getAppId(), $this->getAppCode(), $useCIT);
4040
}
4141

4242
protected function getCacheDir()
@@ -108,6 +108,31 @@ public function testGeocodeQuery()
108108
}
109109
}
110110

111+
public function testGeocodeQueryCIT()
112+
{
113+
if (isset($this->skippedTests[__FUNCTION__])) {
114+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
115+
}
116+
if (!$this->testAddress) {
117+
$this->markTestSkipped('Geocoding address is not supported by this provider');
118+
}
119+
120+
$provider = $this->createProvider($this->getCachedHttpClient(), true);
121+
$query = GeocodeQuery::create('10 Downing St, London, UK')->withLocale('en');
122+
$result = $provider->geocodeQuery($query);
123+
$this->assertWellFormattedResult($result);
124+
125+
// Check Downing Street
126+
$location = $result->first();
127+
$this->assertEquals(51.5033, $location->getCoordinates()->getLatitude(), 'Latitude should be in London', 0.1);
128+
$this->assertEquals(-0.1276, $location->getCoordinates()->getLongitude(), 'Longitude should be in London', 0.1);
129+
$this->assertContains('Downing', $location->getStreetName(), 'Street name should contain "Downing St"');
130+
131+
if (null !== $streetNumber = $location->getStreetNumber()) {
132+
$this->assertContains('10', $streetNumber, 'Street number should contain "10"');
133+
}
134+
}
135+
111136
public function testGeocodeQueryWithNoResults()
112137
{
113138
if (isset($this->skippedTests[__FUNCTION__])) {
@@ -140,6 +165,22 @@ public function testReverseQuery()
140165
$this->assertWellFormattedResult($result);
141166
}
142167

168+
public function testReverseQueryCIT()
169+
{
170+
if (isset($this->skippedTests[__FUNCTION__])) {
171+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
172+
}
173+
if (!$this->testReverse) {
174+
$this->markTestSkipped('Reverse geocoding address is not supported by this provider');
175+
}
176+
177+
$provider = $this->createProvider($this->getCachedHttpClient(), true);
178+
179+
// Close to the white house
180+
$result = $provider->reverseQuery(ReverseQuery::fromCoordinates(38.900206, -77.036991)->withLocale('en'));
181+
$this->assertWellFormattedResult($result);
182+
}
183+
143184
public function testReverseQueryWithNoResults()
144185
{
145186
if (isset($this->skippedTests[__FUNCTION__])) {

0 commit comments

Comments
 (0)