1 | <?php |
||
33 | final class Nominatim extends AbstractHttpProvider implements Provider |
||
34 | { |
||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $rootUrl; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $userAgent; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $referer; |
||
49 | |||
50 | /** |
||
51 | * @param HttpClient $client an HTTP client |
||
52 | * @param string $userAgent Value of the User-Agent header |
||
53 | * @param string $referer Value of the Referer header |
||
54 | * |
||
55 | * @return Nominatim |
||
56 | */ |
||
57 | 24 | public static function withOpenStreetMapServer(HttpClient $client, string $userAgent, string $referer = ''): self |
|
61 | |||
62 | /** |
||
63 | * @param HttpClient $client an HTTP client |
||
64 | * @param string $rootUrl Root URL of the nominatim server |
||
65 | * @param string $userAgent Value of the User-Agent header |
||
66 | * @param string $referer Value of the Referer header |
||
67 | */ |
||
68 | 24 | public function __construct(HttpClient $client, $rootUrl, string $userAgent, string $referer = '') |
|
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | 15 | public function geocodeQuery(GeocodeQuery $query): Collection |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 9 | public function reverseQuery(ReverseQuery $query): Collection |
|
182 | |||
183 | /** |
||
184 | * @param \stdClass $place |
||
185 | * @param bool $reverse |
||
186 | * |
||
187 | * @return Location |
||
188 | */ |
||
189 | 8 | private function jsonResultToLocation(\stdClass $place, bool $reverse): Location |
|
190 | { |
||
191 | 8 | $builder = new AddressBuilder($this->getName()); |
|
192 | |||
193 | 8 | foreach (['state', 'county'] as $i => $tagName) { |
|
194 | 8 | if (isset($place->address->{$tagName})) { |
|
195 | 8 | $builder->addAdminLevel($i + 1, $place->address->{$tagName}, ''); |
|
196 | } |
||
197 | } |
||
198 | |||
199 | // get the first postal-code when there are many |
||
200 | 8 | if (isset($place->address->postcode)) { |
|
201 | 8 | $postalCode = $place->address->postcode; |
|
202 | 8 | if (!empty($postalCode)) { |
|
203 | 8 | $postalCode = current(explode(';', $postalCode)); |
|
204 | } |
||
205 | 8 | $builder->setPostalCode($postalCode); |
|
206 | } |
||
207 | |||
208 | 8 | $localityFields = ['city', 'town', 'village', 'hamlet']; |
|
209 | 8 | foreach ($localityFields as $localityField) { |
|
210 | 8 | if (isset($place->address->{$localityField})) { |
|
211 | 8 | $localityFieldContent = $place->address->{$localityField}; |
|
212 | |||
213 | 8 | if (!empty($localityFieldContent)) { |
|
214 | 8 | $builder->setLocality($localityFieldContent); |
|
215 | |||
216 | 8 | break; |
|
217 | } |
||
218 | } |
||
219 | } |
||
220 | |||
221 | 8 | $builder->setStreetName($place->address->road ?? $place->address->pedestrian ?? null); |
|
222 | 8 | $builder->setStreetNumber($place->address->house_number ?? null); |
|
223 | 8 | $builder->setSubLocality($place->address->suburb ?? null); |
|
224 | 8 | $builder->setCountry($place->address->country ?? null); |
|
225 | 8 | $builder->setCountryCode(isset($place->address->country_code) ? strtoupper($place->address->country_code) : null); |
|
226 | |||
227 | 8 | $builder->setCoordinates(floatval($place->lat), floatval($place->lon)); |
|
228 | |||
229 | 8 | $builder->setBounds($place->boundingbox[0], $place->boundingbox[2], $place->boundingbox[1], $place->boundingbox[3]); |
|
230 | |||
231 | 8 | $location = $builder->build(NominatimAddress::class); |
|
232 | 8 | $location = $location->withAttribution($place->licence); |
|
233 | 8 | $location = $location->withDisplayName($place->display_name); |
|
234 | |||
235 | 8 | if (isset($place->osm_id)) { |
|
236 | 7 | $location = $location->withOSMId(intval($place->osm_id)); |
|
237 | } |
||
238 | 8 | if (isset($place->osm_type)) { |
|
239 | 7 | $location = $location->withOSMType($place->osm_type); |
|
240 | } |
||
241 | |||
242 | 8 | if (false === $reverse) { |
|
243 | 6 | $location = $location->withCategory($place->category); |
|
244 | 6 | $location = $location->withType($place->type); |
|
245 | } |
||
246 | |||
247 | 8 | return $location; |
|
248 | } |
||
249 | |||
250 | /** |
||
251 | * {@inheritdoc} |
||
252 | */ |
||
253 | 8 | public function getName(): string |
|
257 | |||
258 | /** |
||
259 | * @param string $url |
||
260 | * @param string|null $locale |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | 21 | private function executeQuery(string $url, string $locale = null): string |
|
281 | } |
||
282 |