1212
1313use Geocoder \Exception \UnsupportedException ;
1414use Geocoder \Exception \NoResultException ;
15+ use Geocoder \Exception \QuotaExceededException ;
16+ use Geocoder \Exception \InvalidCredentialsException ;
17+ use Geocoder \HttpAdapter \HttpAdapterInterface ;
1518
1619/**
1720 * @author Antoine Corcy <contact@sbin.dk>
@@ -21,12 +24,35 @@ class GeocoderCaProvider extends AbstractProvider implements ProviderInterface
2124 /**
2225 * @var string
2326 */
24- const GEOCODE_ENDPOINT_URL = 'http ://geocoder.ca/?geoit=xml&locate=%s ' ;
27+ const GEOCODE_ENDPOINT_URL = '%s ://geocoder.ca/?geoit=xml&locate=%s&auth =%s ' ;
2528
2629 /**
2730 * @var string
2831 */
29- const REVERSE_ENDPOINT_URL = 'http://geocoder.ca/?geoit=xml&reverse=1&latt=%F&longt=%F ' ;
32+ const REVERSE_ENDPOINT_URL = '%s://geocoder.ca/?geoit=xml&reverse=1&latt=%F&longt=%F&auth=%s ' ;
33+
34+ /**
35+ * @var string
36+ */
37+ private $ scheme = 'http ' ;
38+
39+ /**
40+ * @var string
41+ */
42+ private $ apiKey = null ;
43+
44+ /**
45+ * @param HttpAdapterInterface $adapter An HTTP adapter.
46+ * @param bool $useSsl Whether to use an SSL connection (optional).
47+ * @param string $apiKey An API key (optional).
48+ */
49+ public function __construct (HttpAdapterInterface $ adapter , $ useSsl = false , $ apiKey = null )
50+ {
51+ parent ::__construct ($ adapter );
52+
53+ $ this ->scheme = $ useSsl ? 'https ' : $ this ->scheme ;
54+ $ this ->apiKey = $ apiKey ;
55+ }
3056
3157 /**
3258 * {@inheritDoc}
@@ -38,17 +64,21 @@ public function getGeocodedData($address)
3864 throw new UnsupportedException ('The GeocoderCaProvider does not support IP addresses. ' );
3965 }
4066
41- $ query = sprintf (self ::GEOCODE_ENDPOINT_URL , urlencode ($ address ));
42- $ content = $ this ->getAdapter ()->getContent ($ query );
67+ $ query = sprintf (self ::GEOCODE_ENDPOINT_URL , $ this ->scheme , urlencode ($ address ), $ this ->apiKey );
4368
44- $ doc = new \DOMDocument ();
45- if (!@$ doc ->loadXML ($ content ) || $ doc ->getElementsByTagName ('error ' )->length ) {
69+ try {
70+ $ content = $ this ->handleQuery ($ query );
71+ } catch (InvalidCredentialsException $ e ) {
72+ throw $ e ;
73+ } catch (QuotaExceededException $ e ) {
74+ throw $ e ;
75+ } catch (NoResultException $ e ) {
4676 throw new NoResultException (sprintf ('Could not execute query %s ' , $ query ));
4777 }
4878
4979 return array (array_merge ($ this ->getDefaults (), array (
50- 'latitude ' => $ this ->getNodeValue ($ doc ->getElementsByTagName ('latt ' )),
51- 'longitude ' => $ this ->getNodeValue ($ doc ->getElementsByTagName ('longt ' ))
80+ 'latitude ' => $ this ->getNodeValue ($ content ->getElementsByTagName ('latt ' )),
81+ 'longitude ' => $ this ->getNodeValue ($ content ->getElementsByTagName ('longt ' ))
5282 )));
5383 }
5484
@@ -57,22 +87,26 @@ public function getGeocodedData($address)
5787 */
5888 public function getReversedData (array $ coordinates )
5989 {
60- $ query = sprintf (self ::REVERSE_ENDPOINT_URL , $ coordinates [0 ], $ coordinates [1 ]);
61- $ content = $ this ->getAdapter ()->getContent ($ query );
90+ $ query = sprintf (self ::REVERSE_ENDPOINT_URL , $ this ->scheme , $ coordinates [0 ], $ coordinates [1 ], $ this ->apiKey );
6291
63- $ doc = new \DOMDocument ();
64- if (!@$ doc ->loadXML ($ content ) || $ doc ->getElementsByTagName ('error ' )->length ) {
92+ try {
93+ $ content = $ this ->handleQuery ($ query );
94+ } catch (InvalidCredentialsException $ e ) {
95+ throw $ e ;
96+ } catch (QuotaExceededException $ e ) {
97+ throw $ e ;
98+ } catch (NoResultException $ e ) {
6599 throw new NoResultException (sprintf ('Could not resolve coordinates %s ' , implode (', ' , $ coordinates )));
66100 }
67101
68102 return array (array_merge ($ this ->getDefaults (), array (
69- 'latitude ' => $ this ->getNodeValue ($ doc ->getElementsByTagName ('latt ' )),
70- 'longitude ' => $ this ->getNodeValue ($ doc ->getElementsByTagName ('longt ' )),
71- 'streetNumber ' => $ this ->getNodeValue ($ doc ->getElementsByTagName ('stnumber ' )),
72- 'streetName ' => $ this ->getNodeValue ($ doc ->getElementsByTagName ('staddress ' )),
73- 'city ' => $ this ->getNodeValue ($ doc ->getElementsByTagName ('city ' )),
74- 'zipcode ' => $ this ->getNodeValue ($ doc ->getElementsByTagName ('postal ' )),
75- 'cityDistrict ' => $ this ->getNodeValue ($ doc ->getElementsByTagName ('prov ' )),
103+ 'latitude ' => $ this ->getNodeValue ($ content ->getElementsByTagName ('latt ' )),
104+ 'longitude ' => $ this ->getNodeValue ($ content ->getElementsByTagName ('longt ' )),
105+ 'streetNumber ' => $ this ->getNodeValue ($ content ->getElementsByTagName ('stnumber ' )),
106+ 'streetName ' => $ this ->getNodeValue ($ content ->getElementsByTagName ('staddress ' )),
107+ 'city ' => $ this ->getNodeValue ($ content ->getElementsByTagName ('city ' )),
108+ 'zipcode ' => $ this ->getNodeValue ($ content ->getElementsByTagName ('postal ' )),
109+ 'cityDistrict ' => $ this ->getNodeValue ($ content ->getElementsByTagName ('prov ' )),
76110 )));
77111 }
78112
@@ -93,4 +127,31 @@ private function getNodeValue(\DOMNodeList $element)
93127 {
94128 return $ element ->length ? $ element ->item (0 )->nodeValue : null ;
95129 }
130+
131+ /**
132+ * @param string $query
133+ * @throws InvalidCredentialsException
134+ * @throws QuotaExceededException
135+ * @throws NoResultException
136+ * @return \DOMDocument
137+ */
138+ private function handleQuery ($ query )
139+ {
140+ $ content = $ this ->getAdapter ()->getContent ($ query );
141+
142+ $ doc = new \DOMDocument ;
143+ if (!@$ doc ->loadXML ($ content ) || $ doc ->getElementsByTagName ('error ' )->length ) {
144+ switch ($ this ->getNodeValue ($ doc ->getElementsByTagName ('code ' ))) {
145+ case '001 ' :
146+ case '003 ' :
147+ throw new InvalidCredentialsException (sprintf ('Invalid authentification token %s ' , $ query ));
148+ case '002 ' :
149+ throw new QuotaExceededException (sprintf ('Account ran out of credits %s ' , $ query ));
150+ default :
151+ throw new NoResultException ;
152+ }
153+ }
154+
155+ return $ doc ;
156+ }
96157}
0 commit comments