12
12
13
13
use Geocoder \Exception \UnsupportedException ;
14
14
use Geocoder \Exception \NoResultException ;
15
+ use Geocoder \Exception \QuotaExceededException ;
16
+ use Geocoder \Exception \InvalidCredentialsException ;
17
+ use Geocoder \HttpAdapter \HttpAdapterInterface ;
15
18
16
19
/**
17
20
* @author Antoine Corcy <[email protected] >
@@ -21,12 +24,35 @@ class GeocoderCaProvider extends AbstractProvider implements ProviderInterface
21
24
/**
22
25
* @var string
23
26
*/
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 ' ;
25
28
26
29
/**
27
30
* @var string
28
31
*/
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
+ }
30
56
31
57
/**
32
58
* {@inheritDoc}
@@ -38,17 +64,21 @@ public function getGeocodedData($address)
38
64
throw new UnsupportedException ('The GeocoderCaProvider does not support IP addresses. ' );
39
65
}
40
66
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 );
43
68
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 ) {
46
76
throw new NoResultException (sprintf ('Could not execute query %s ' , $ query ));
47
77
}
48
78
49
79
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 ' ))
52
82
)));
53
83
}
54
84
@@ -57,22 +87,26 @@ public function getGeocodedData($address)
57
87
*/
58
88
public function getReversedData (array $ coordinates )
59
89
{
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 );
62
91
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 ) {
65
99
throw new NoResultException (sprintf ('Could not resolve coordinates %s ' , implode (', ' , $ coordinates )));
66
100
}
67
101
68
102
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 ' )),
76
110
)));
77
111
}
78
112
@@ -93,4 +127,31 @@ private function getNodeValue(\DOMNodeList $element)
93
127
{
94
128
return $ element ->length ? $ element ->item (0 )->nodeValue : null ;
95
129
}
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
+ }
96
157
}
0 commit comments