Skip to content

Commit 9c87457

Browse files
unkindNyholm
authored andcommitted
Finalize the providers (#540)
1 parent 87a0f6d commit 9c87457

27 files changed

+215
-293
lines changed

src/Geocoder/Provider/ArcGISOnline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* @author ALKOUM Dorian <[email protected]>
1919
*/
20-
class ArcGISOnline extends AbstractHttpProvider implements Provider
20+
final class ArcGISOnline extends AbstractHttpProvider implements Provider
2121
{
2222
/**
2323
* @var string

src/Geocoder/Provider/BingMaps.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* @author David Guyon <[email protected]>
2020
*/
21-
class BingMaps extends AbstractHttpProvider implements LocaleAwareProvider
21+
final class BingMaps extends AbstractHttpProvider implements LocaleAwareProvider
2222
{
2323
use LocaleTrait;
2424

src/Geocoder/Provider/Chain.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* @author Markus Bachmann <[email protected]>
1818
*/
19-
class Chain implements LocaleAwareProvider
19+
final class Chain implements LocaleAwareProvider
2020
{
2121
use LocaleTrait;
2222

src/Geocoder/Provider/FreeGeoIp.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* @author William Durand <[email protected]>
1919
*/
20-
class FreeGeoIp extends AbstractHttpProvider implements Provider
20+
final class FreeGeoIp extends AbstractHttpProvider implements Provider
2121
{
2222
/**
2323
* @var string

src/Geocoder/Provider/GeoIP2.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* @author Jens Wiese <[email protected]>
2020
*/
21-
class GeoIP2 extends AbstractProvider implements LocaleAwareProvider
21+
final class GeoIP2 extends AbstractProvider implements LocaleAwareProvider
2222
{
2323
use LocaleTrait;
2424

src/Geocoder/Provider/GeoIPs.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
* @link http://www.geoips.com/en/developer/api-guide
2525
*/
26-
class GeoIPs extends AbstractHttpProvider implements Provider
26+
final class GeoIPs extends AbstractHttpProvider implements Provider
2727
{
2828
/**
2929
* @var string

src/Geocoder/Provider/GeoPlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
/**
1717
* @author Andrea Cristaudo <[email protected]>
1818
*/
19-
class GeoPlugin extends AbstractHttpProvider implements Provider
19+
final class GeoPlugin extends AbstractHttpProvider implements Provider
2020
{
2121
/**
2222
* @var string

src/Geocoder/Provider/Geoip.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @link http://php.net/manual/ref.geoip.php
2121
*/
22-
class Geoip extends AbstractProvider implements Provider
22+
final class Geoip extends AbstractProvider implements Provider
2323
{
2424
public function __construct()
2525
{

src/Geocoder/Provider/Geonames.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* @author Giovanni Pirrotta <[email protected]>
2121
*/
22-
class Geonames extends AbstractHttpProvider implements LocaleAwareProvider
22+
final class Geonames extends AbstractHttpProvider implements LocaleAwareProvider
2323
{
2424
/**
2525
* @var string

src/Geocoder/Provider/GoogleMaps.php

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/**
2121
* @author William Durand <[email protected]>
2222
*/
23-
class GoogleMaps extends AbstractHttpProvider implements LocaleAwareProvider
23+
final class GoogleMaps extends AbstractHttpProvider implements LocaleAwareProvider
2424
{
2525
/**
2626
* @var string
@@ -49,6 +49,38 @@ class GoogleMaps extends AbstractHttpProvider implements LocaleAwareProvider
4949
*/
5050
private $apiKey;
5151

52+
/**
53+
* @var string
54+
*/
55+
private $clientId;
56+
57+
/**
58+
* @var string
59+
*/
60+
private $privateKey;
61+
62+
/**
63+
* Google Maps for Business
64+
* https://developers.google.com/maps/documentation/business/
65+
*
66+
* @param HttpClient $client An HTTP adapter
67+
* @param string $clientId Your Client ID
68+
* @param string $privateKey Your Private Key (optional)
69+
* @param string $locale A locale (optional)
70+
* @param string $region Region biasing (optional)
71+
* @param bool $useSsl Whether to use an SSL connection (optional)
72+
* @param string $apiKey Google Geocoding API key (optional)
73+
* @return GoogleMaps
74+
*/
75+
public static function business(HttpClient $client, $clientId, $privateKey = null, $locale = null, $region = null, $useSsl = false, $apiKey = null)
76+
{
77+
$provider = new self($client, $locale, $region, $useSsl, $apiKey);
78+
$provider->clientId = $clientId;
79+
$provider->privateKey = $privateKey;
80+
81+
return $provider;
82+
}
83+
5284
/**
5385
* @param HttpClient $client An HTTP adapter
5486
* @param string $locale A locale (optional)
@@ -113,7 +145,7 @@ public function setRegion($region)
113145
*
114146
* @return string query with extra params
115147
*/
116-
protected function buildQuery($query)
148+
private function buildQuery($query)
117149
{
118150
if (null !== $this->getLocale()) {
119151
$query = sprintf('%s&language=%s', $query, $this->getLocale());
@@ -127,6 +159,14 @@ protected function buildQuery($query)
127159
$query = sprintf('%s&key=%s', $query, $this->apiKey);
128160
}
129161

162+
if (null !== $this->clientId) {
163+
$query = sprintf('%s&client=%s', $query, $this->clientId);
164+
165+
if (null !== $this->privateKey) {
166+
$query = $this->signQuery($query);
167+
}
168+
}
169+
130170
return $query;
131171
}
132172

@@ -269,4 +309,31 @@ private function updateAddressComponent(&$resultSet, $type, $values)
269309

270310
return $resultSet;
271311
}
312+
313+
/**
314+
* Sign a URL with a given crypto key
315+
* Note that this URL must be properly URL-encoded
316+
* src: http://gmaps-samples.googlecode.com/svn/trunk/urlsigning/UrlSigner.php-source
317+
*
318+
* @param string $query Query to be signed
319+
*
320+
* @return string $query Query with signature appended.
321+
*/
322+
private function signQuery($query)
323+
{
324+
$url = parse_url($query);
325+
326+
$urlPartToSign = $url['path'] . '?' . $url['query'];
327+
328+
// Decode the private key into its binary format
329+
$decodedKey = base64_decode(str_replace(array('-', '_'), array('+', '/'), $this->privateKey));
330+
331+
// Create a signature using the private key and the URL-encoded
332+
// string using HMAC SHA1. This signature will be binary.
333+
$signature = hash_hmac('sha1', $urlPartToSign, $decodedKey, true);
334+
335+
$encodedSignature = str_replace(array('+', '/'), array('-', '_'), base64_encode($signature));
336+
337+
return sprintf('%s&signature=%s', $query, $encodedSignature);
338+
}
272339
}

0 commit comments

Comments
 (0)