77 *
88 * @license MIT License
99 */
10-
1110namespace Geocoder \Provider ;
1211
1312use Geocoder \Exception \InvalidCredentials ;
@@ -53,7 +52,7 @@ public function __construct(HttpAdapterInterface $adapter, $apiKey, $useSsl = fa
5352 }
5453
5554 /**
56- * {@inheritDoc }
55+ * {@inheritdoc }
5756 */
5857 public function geocode ($ address )
5958 {
@@ -66,31 +65,32 @@ public function geocode($address)
6665 throw new UnsupportedOperation ('The OpenCage provider does not support IP addresses, only street addresses. ' );
6766 }
6867
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 ());
7069
7170 return $ this ->executeQuery ($ query );
7271 }
7372
7473 /**
75- * {@inheritDoc }
74+ * {@inheritdoc }
7675 */
7776 public function reverse ($ latitude , $ longitude )
7877 {
79- $ address = sprintf (" %f, %f " , $ latitude , $ longitude );
78+ $ address = sprintf (' %f, %f ' , $ latitude , $ longitude );
8079
8180 return $ this ->geocode ($ address );
8281 }
8382
8483 /**
85- * {@inheritDoc }
84+ * {@inheritdoc }
8685 */
8786 public function getName ()
8887 {
8988 return 'opencage ' ;
9089 }
9190
9291 /**
93- * @param string $query
92+ * @param $query
93+ * @return \Geocoder\Model\AddressCollection
9494 */
9595 private function executeQuery ($ query )
9696 {
@@ -106,7 +106,7 @@ private function executeQuery($query)
106106
107107 $ json = json_decode ($ content , true );
108108
109- if (!isset ($ json ['total_results ' ]) || $ json ['total_results ' ] == 0 ) {
109+ if (!isset ($ json ['total_results ' ]) || $ json ['total_results ' ] == 0 ) {
110110 throw new NoResult (sprintf ('Could not find results for query "%s". ' , $ query ));
111111 }
112112
@@ -122,9 +122,9 @@ private function executeQuery($query)
122122 if (isset ($ location ['bounds ' ])) {
123123 $ bounds = [
124124 'south ' => $ location ['bounds ' ]['southwest ' ]['lat ' ],
125- 'west ' => $ location ['bounds ' ]['southwest ' ]['lng ' ],
125+ 'west ' => $ location ['bounds ' ]['southwest ' ]['lng ' ],
126126 'north ' => $ location ['bounds ' ]['northeast ' ]['lat ' ],
127- 'east ' => $ location ['bounds ' ]['northeast ' ]['lng ' ],
127+ 'east ' => $ location ['bounds ' ]['northeast ' ]['lng ' ],
128128 ];
129129 }
130130
@@ -138,21 +138,74 @@ private function executeQuery($query)
138138 }
139139
140140 $ 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 ?: [],
144144 '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 ,
153153 ));
154154 }
155155
156156 return $ this ->returnResults ($ results );
157157 }
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+ }
158211}
0 commit comments