7
7
*
8
8
* @license MIT License
9
9
*/
10
-
11
10
namespace Geocoder \Provider ;
12
11
13
12
use Geocoder \Exception \InvalidCredentials ;
@@ -53,7 +52,7 @@ public function __construct(HttpAdapterInterface $adapter, $apiKey, $useSsl = fa
53
52
}
54
53
55
54
/**
56
- * {@inheritDoc }
55
+ * {@inheritdoc }
57
56
*/
58
57
public function geocode ($ address )
59
58
{
@@ -66,31 +65,32 @@ public function geocode($address)
66
65
throw new UnsupportedOperation ('The OpenCage provider does not support IP addresses, only street addresses. ' );
67
66
}
68
67
69
- $ query = sprintf (self ::GEOCODE_ENDPOINT_URL , $ this ->scheme , $ this ->apiKey , urlencode ($ address ), $ this ->getLimit () );
68
+ $ query = sprintf (self ::GEOCODE_ENDPOINT_URL , $ this ->scheme , $ this ->apiKey , urlencode ($ address ), $ this ->getLimit ());
70
69
71
70
return $ this ->executeQuery ($ query );
72
71
}
73
72
74
73
/**
75
- * {@inheritDoc }
74
+ * {@inheritdoc }
76
75
*/
77
76
public function reverse ($ latitude , $ longitude )
78
77
{
79
- $ address = sprintf (" %f, %f " , $ latitude , $ longitude );
78
+ $ address = sprintf (' %f, %f ' , $ latitude , $ longitude );
80
79
81
80
return $ this ->geocode ($ address );
82
81
}
83
82
84
83
/**
85
- * {@inheritDoc }
84
+ * {@inheritdoc }
86
85
*/
87
86
public function getName ()
88
87
{
89
88
return 'opencage ' ;
90
89
}
91
90
92
91
/**
93
- * @param string $query
92
+ * @param $query
93
+ * @return \Geocoder\Model\AddressCollection
94
94
*/
95
95
private function executeQuery ($ query )
96
96
{
@@ -106,7 +106,7 @@ private function executeQuery($query)
106
106
107
107
$ json = json_decode ($ content , true );
108
108
109
- if (!isset ($ json ['total_results ' ]) || $ json ['total_results ' ] == 0 ) {
109
+ if (!isset ($ json ['total_results ' ]) || $ json ['total_results ' ] == 0 ) {
110
110
throw new NoResult (sprintf ('Could not find results for query "%s". ' , $ query ));
111
111
}
112
112
@@ -122,9 +122,9 @@ private function executeQuery($query)
122
122
if (isset ($ location ['bounds ' ])) {
123
123
$ bounds = [
124
124
'south ' => $ location ['bounds ' ]['southwest ' ]['lat ' ],
125
- 'west ' => $ location ['bounds ' ]['southwest ' ]['lng ' ],
125
+ 'west ' => $ location ['bounds ' ]['southwest ' ]['lng ' ],
126
126
'north ' => $ location ['bounds ' ]['northeast ' ]['lat ' ],
127
- 'east ' => $ location ['bounds ' ]['northeast ' ]['lng ' ],
127
+ 'east ' => $ location ['bounds ' ]['northeast ' ]['lng ' ],
128
128
];
129
129
}
130
130
@@ -138,21 +138,74 @@ private function executeQuery($query)
138
138
}
139
139
140
140
$ results [] = array_merge ($ this ->getDefaults (), array (
141
- 'latitude ' => $ location ['geometry ' ]['lat ' ],
142
- 'longitude ' => $ location ['geometry ' ]['lng ' ],
143
- 'bounds ' => $ bounds ?: [],
141
+ 'latitude ' => $ location ['geometry ' ]['lat ' ],
142
+ 'longitude ' => $ location ['geometry ' ]['lng ' ],
143
+ 'bounds ' => $ bounds ?: [],
144
144
'streetNumber ' => isset ($ comp ['house_number ' ]) ? $ comp ['house_number ' ] : null ,
145
- 'streetName ' => isset ($ comp[ ' road ' ] ) ? $ comp [ ' road ' ] : null ,
146
- 'subLocality ' => isset ($ comp[ ' suburb ' ] ) ? $ comp [ ' suburb ' ] : null ,
147
- 'locality ' => isset ($ comp[ ' city ' ] ) ? $ comp [ ' city ' ] : null ,
148
- 'postalCode ' => isset ($ comp ['postcode ' ] ) ? $ comp ['postcode ' ] : null ,
149
- 'adminLevels ' => $ adminLevels ,
150
- 'country ' => isset ($ comp ['country ' ] ) ? $ comp ['country ' ] : null ,
151
- 'countryCode ' => isset ($ comp ['country_code ' ]) ? strtoupper ($ comp ['country_code ' ]) : null ,
152
- 'timezone ' => isset ($ location ['annotations ' ]['timezone ' ]['name ' ]) ? $ location ['annotations ' ]['timezone ' ]['name ' ] : null ,
145
+ 'streetName ' => $ this -> guessStreetName ($ comp) ,
146
+ 'subLocality ' => $ this -> guessSubLocality ($ comp) ,
147
+ 'locality ' => $ this -> guessLocality ($ comp) ,
148
+ 'postalCode ' => isset ($ comp ['postcode ' ]) ? $ comp ['postcode ' ] : null ,
149
+ 'adminLevels ' => $ adminLevels ,
150
+ 'country ' => isset ($ comp ['country ' ]) ? $ comp ['country ' ] : null ,
151
+ 'countryCode ' => isset ($ comp ['country_code ' ]) ? strtoupper ($ comp ['country_code ' ]) : null ,
152
+ 'timezone ' => isset ($ location ['annotations ' ]['timezone ' ]['name ' ]) ? $ location ['annotations ' ]['timezone ' ]['name ' ] : null ,
153
153
));
154
154
}
155
155
156
156
return $ this ->returnResults ($ results );
157
157
}
158
+
159
+ /**
160
+ * @param array $components
161
+ *
162
+ * @return null|string
163
+ */
164
+ protected function guessLocality (array $ components )
165
+ {
166
+ $ localityKeys = array ('city ' , 'town ' , 'village ' , 'hamlet ' );
167
+
168
+ return $ this ->guessBestComponent ($ components , $ localityKeys );
169
+ }
170
+
171
+ /**
172
+ * @param array $components
173
+ *
174
+ * @return null|string
175
+ */
176
+ protected function guessStreetName (array $ components )
177
+ {
178
+ $ streetNameKeys = array ('road ' , 'street ' , 'street_name ' , 'residential ' );
179
+
180
+ return $ this ->guessBestComponent ($ components , $ streetNameKeys );
181
+ }
182
+
183
+ /**
184
+ * @param array $components
185
+ *
186
+ * @return null|string
187
+ */
188
+ protected function guessSubLocality (array $ components )
189
+ {
190
+ $ subLocalityKeys = array ('suburb ' , 'neighbourhood ' , 'city_district ' );
191
+
192
+ return $ this ->guessBestComponent ($ components , $ subLocalityKeys );
193
+ }
194
+
195
+ /**
196
+ * @param array $components
197
+ * @param array $keys
198
+ *
199
+ * @return null|string
200
+ */
201
+ protected function guessBestComponent (array $ components , array $ keys )
202
+ {
203
+ foreach ($ keys as $ key ) {
204
+ if (isset ($ components [$ key ]) && !empty ($ components [$ key ])) {
205
+ return $ components [$ key ];
206
+ }
207
+ }
208
+
209
+ return null ;
210
+ }
158
211
}
0 commit comments