24 private const HTTP_SOCKET_TIMEOUT = 10;
25 private const HTTP_STREAM_TIMEOUT = 10;
30 private const API_SEARCH_LIMIT = 10;
32 private const API_AUTOCOMPLETE_LIMIT = 7;
43 $this->source = $source;
51 public function search(array $options): array
53 $client = $this->makeHttpClient();
61 'q' => Encoding::convertEncoding($options[
'q'], SITE_CHARSET,
'UTF-8'),
62 'addressdetails' => isset($options[
'addressdetails']) ? (
int)$options[
'addressdetails'] : 0,
63 'limit' => isset($options[
'limit']) ? (int)$options[
'limit'] : self::API_SEARCH_LIMIT,
64 'accept-language' => $options[
'accept-language'] ??
'',
71 return $this->getResponse($client, $body);
80 $client = $this->makeHttpClient();
83 'q' => Encoding::convertEncoding($options[
'q'], SITE_CHARSET,
'UTF-8'),
84 'limit' => isset($options[
'limit']) ? (int)$options[
'limit'] : self::API_AUTOCOMPLETE_LIMIT,
85 'lang' => $options[
'lang'] ??
'',
88 if (isset($options[
'lat']) && isset($options[
'lon']))
90 $queryData[
'lat'] = $options[
'lat'];
91 $queryData[
'lon'] = $options[
'lon'];
104 return $this->getResponse($client, $body);
112 public function lookup(array $options): array
114 $client = $this->makeHttpClient();
116 $body = $client->get(
120 $this->wrapQueryData(
122 'osm_ids' => $options[
'osm_ids'] ??
'',
123 'addressdetails' => isset($options[
'addressdetails']) ? (
int)$options[
'addressdetails'] : 0,
124 'accept-language' => $options[
'accept-language'] ??
'',
131 return $this->getResponse($client, $body);
139 public function details(array $options): array
141 $client = $this->makeHttpClient();
143 $body = $client->get(
147 $this->wrapQueryData(
149 'osmtype' => $options[
'osm_type'] ??
'',
150 'osmid' => $options[
'osm_id'] ??
'',
152 'addressdetails' => isset($options[
'addressdetails']) ? (
int)$options[
'addressdetails'] : 0,
153 'linkedplaces' => isset($options[
'linkedplaces']) ? (int)$options[
'linkedplaces'] : 0,
154 'hierarchy' => isset($options[
'hierarchy']) ? (int)$options[
'hierarchy'] : 0,
155 'accept-language' => $options[
'accept-language'] ??
'',
161 return $this->getResponse($client, $body);
170 private function getResponse(
HttpClient $client,
string $body): array
179 implode(
'; ', array_map(
180 function ($v, $k) {
return sprintf(
"%s=%s", $k, $v); },
189 throw new RuntimeException(sprintf(
'Unexpected status code - %s', $status));
194 $response = Json::decode($body);
196 catch (ArgumentException $e)
198 throw new RuntimeException(
'JSON decode error');
201 if (!is_array($response))
203 throw new RuntimeException(
'Response format error');
215 private function makeHttpClient(): HttpClient
217 $token = $this->source->getOsmToken();
219 $result =
new HttpClient(
221 'version' => self::HTTP_VERSION,
222 'socketTimeout' => self::HTTP_SOCKET_TIMEOUT,
223 'streamTimeout' => self::HTTP_STREAM_TIMEOUT,
229 sprintf(
'Bearer %s', ($token ? $token->getToken() :
''))
232 $result->setHeader(
'Bx-Location-Osm-Host', $this->source->getOsmHostName());
243 private function buildUrl(
string $controller,
string $action, array $queryData): string
245 $serviceUrl = $this->source->getOsmApiUrl();
249 throw new RuntimeException(
'Service url is not specified');
259 'action' => sprintf(
'osmgateway.%s.%s', $controller, $action)
270 private function wrapQueryData(array $queryData): array
273 'params' => $queryData