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,23 +65,23 @@ 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
{
@@ -106,7 +105,7 @@ private function executeQuery($query)
106
105
107
106
$ json = json_decode ($ content , true );
108
107
109
- if (!isset ($ json ['total_results ' ]) || $ json ['total_results ' ] == 0 ) {
108
+ if (!isset ($ json ['total_results ' ]) || $ json ['total_results ' ] == 0 ) {
110
109
throw new NoResult (sprintf ('Could not find results for query "%s". ' , $ query ));
111
110
}
112
111
@@ -122,9 +121,9 @@ private function executeQuery($query)
122
121
if (isset ($ location ['bounds ' ])) {
123
122
$ bounds = [
124
123
'south ' => $ location ['bounds ' ]['southwest ' ]['lat ' ],
125
- 'west ' => $ location ['bounds ' ]['southwest ' ]['lng ' ],
124
+ 'west ' => $ location ['bounds ' ]['southwest ' ]['lng ' ],
126
125
'north ' => $ location ['bounds ' ]['northeast ' ]['lat ' ],
127
- 'east ' => $ location ['bounds ' ]['northeast ' ]['lng ' ],
126
+ 'east ' => $ location ['bounds ' ]['northeast ' ]['lng ' ],
128
127
];
129
128
}
130
129
@@ -138,21 +137,39 @@ private function executeQuery($query)
138
137
}
139
138
140
139
$ 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 ?: [],
144
143
'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 ,
153
152
));
154
153
}
155
154
156
155
return $ this ->returnResults ($ results );
157
156
}
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
+ }
158
175
}
0 commit comments