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,23 +65,23 @@ 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 {
@@ -106,7 +105,7 @@ private function executeQuery($query)
106105
107106 $ json = json_decode ($ content , true );
108107
109- if (!isset ($ json ['total_results ' ]) || $ json ['total_results ' ] == 0 ) {
108+ if (!isset ($ json ['total_results ' ]) || $ json ['total_results ' ] == 0 ) {
110109 throw new NoResult (sprintf ('Could not find results for query "%s". ' , $ query ));
111110 }
112111
@@ -122,9 +121,9 @@ private function executeQuery($query)
122121 if (isset ($ location ['bounds ' ])) {
123122 $ bounds = [
124123 'south ' => $ location ['bounds ' ]['southwest ' ]['lat ' ],
125- 'west ' => $ location ['bounds ' ]['southwest ' ]['lng ' ],
124+ 'west ' => $ location ['bounds ' ]['southwest ' ]['lng ' ],
126125 'north ' => $ location ['bounds ' ]['northeast ' ]['lat ' ],
127- 'east ' => $ location ['bounds ' ]['northeast ' ]['lng ' ],
126+ 'east ' => $ location ['bounds ' ]['northeast ' ]['lng ' ],
128127 ];
129128 }
130129
@@ -138,21 +137,39 @@ private function executeQuery($query)
138137 }
139138
140139 $ results [] = array_merge ($ this ->getDefaults (), array (
141- 'latitude ' => $ location ['geometry ' ]['lat ' ],
142- 'longitude ' => $ location ['geometry ' ]['lng ' ],
143- 'bounds ' => $ bounds ?: [],
140+ 'latitude ' => $ location ['geometry ' ]['lat ' ],
141+ 'longitude ' => $ location ['geometry ' ]['lng ' ],
142+ 'bounds ' => $ bounds ?: [],
144143 '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 ,
144+ 'streetName ' => isset ($ comp ['road ' ]) ? $ comp ['road ' ] : null ,
145+ 'subLocality ' => isset ($ comp ['suburb ' ]) ? $ comp ['suburb ' ] : null ,
146+ 'locality ' => $ this -> guessLocality ($ comp) ,
147+ 'postalCode ' => isset ($ comp ['postcode ' ]) ? $ comp ['postcode ' ] : null ,
148+ 'adminLevels ' => $ adminLevels ,
149+ 'country ' => isset ($ comp ['country ' ]) ? $ comp ['country ' ] : null ,
150+ 'countryCode ' => isset ($ comp ['country_code ' ]) ? strtoupper ($ comp ['country_code ' ]) : null ,
151+ 'timezone ' => isset ($ location ['annotations ' ]['timezone ' ]['name ' ]) ? $ location ['annotations ' ]['timezone ' ]['name ' ] : null ,
153152 ));
154153 }
155154
156155 return $ this ->returnResults ($ results );
157156 }
157+
158+ /**
159+ * @param array $components
160+ *
161+ * @return null|string
162+ */
163+ protected function guessLocality (array $ components )
164+ {
165+ $ localityKeys = array ('city ' , 'town ' , 'village ' , 'hamlet ' );
166+
167+ foreach ($ localityKeys as $ key ) {
168+ if (isset ($ components [$ key ])) {
169+ return $ components [$ key ];
170+ }
171+ }
172+
173+ return ;
174+ }
158175}
0 commit comments