-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathNominatim.php
233 lines (204 loc) · 6.59 KB
/
Nominatim.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
declare(strict_types=1);
/**
* This file is part of PHP Nominatim.
* (c) Maxime Hélias <[email protected]>
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace maxh\Nominatim;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use maxh\Nominatim\Exceptions\NominatimException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use SimpleXMLElement;
/**
* Wrapper to manage exchanges with OSM Nominatim API.
*
* @see http://wiki.openstreetmap.org/wiki/Nominatim
*/
class Nominatim
{
/**
* Contain url of the current application.
*
* @var string
*/
private $application_url;
/**
* Contain default request headers.
*
* @var array
*/
private $defaultHeaders;
/**
* Contain http client connection.
*
* @var \GuzzleHttp\Client
*/
private $http_client;
/**
* The search object which serves as a template for new ones created
* by 'newSearch()' method.
*
* @var \maxh\Nominatim\Search
*/
private $baseSearch;
/**
* Template for new ones created by 'newReverser()' method.
*
* @var \maxh\Nominatim\Reverse
*/
private $baseReverse;
/**
* Template for new ones created by 'newLookup()' method.
*
* @var \maxh\Nominatim\Lookup
*/
private $baseLookup;
/**
* Template for new ones created by 'newDetails()' method.
*
* @var \maxh\Nominatim\Lookup
*/
private $baseDetails;
/**
* Constructor.
*
* @param string $application_url Contain url of the current application
* @param null|\GuzzleHttp\Client $http_client Client object from Guzzle
* @param array $defaultHeaders Define default header for all request
*
* @throws NominatimException
*/
public function __construct(
string $application_url,
array $defaultHeaders = [],
?Client $http_client = null
) {
if (empty($application_url)) {
throw new NominatimException('Application url parameter is empty');
}
if (null === $http_client) {
$http_client = new Client([
'base_uri' => $application_url,
'timeout' => 30,
'connection_timeout' => 5,
]);
} elseif ($http_client instanceof Client) {
$application_url_client = (string) $http_client->getConfig('base_uri');
if (empty($application_url_client)) {
throw new NominatimException('http_client must have a configured base_uri.');
}
if ($application_url_client !== $application_url) {
throw new NominatimException('http_client parameter hasn\'t the same url application.');
}
} else {
throw new NominatimException('http_client parameter must be a \\GuzzleHttp\\Client object or empty');
}
$this->application_url = $application_url;
$this->defaultHeaders = $defaultHeaders;
$this->http_client = $http_client;
//Create base
$this->baseSearch = new Search();
$this->baseReverse = new Reverse();
$this->baseLookup = new Lookup();
$this->baseDetails = new Details();
}
/**
* Returns a new search object based on the base search.
*
* @return \maxh\Nominatim\Search
*/
public function newSearch(): Search
{
return clone $this->baseSearch;
}
/**
* Returns a new reverse object based on the base reverse.
*
* @return \maxh\Nominatim\Reverse
*/
public function newReverse(): Reverse
{
return clone $this->baseReverse;
}
/**
* Returns a new lookup object based on the base lookup.
*
* @return \maxh\Nominatim\Lookup
*/
public function newLookup(): Lookup
{
return clone $this->baseLookup;
}
/**
* Returns a new datails object based on the base details.
*
* @return \maxh\Nominatim\Details
*/
public function newDetails(): Details
{
return clone $this->baseDetails;
}
/**
* Runs the query and returns the result set from Nominatim.
*
* @param QueryInterface $nRequest The object request to send
* @param array $headers Override the request header
*
* @throws NominatimException if no format for decode
* @throws \GuzzleHttp\Exception\GuzzleException
*
* @return array|SimpleXMLElement The decoded data returned from Nominatim
*/
public function find(QueryInterface $nRequest, array $headers = [])
{
$url = $this->application_url.'/'.$nRequest->getPath().'?';
$request = new Request('GET', $url, array_merge($this->defaultHeaders, $headers));
//Convert the query array to string with space replace to +
if (method_exists(\GuzzleHttp\Psr7\Query::class, 'build')) {
$query = \GuzzleHttp\Psr7\Query::build($nRequest->getQuery(), PHP_QUERY_RFC1738);
} else {
$query = \GuzzleHttp\Psr7\build_query($nRequest->getQuery(), PHP_QUERY_RFC1738);
}
$url = $request->getUri()->withQuery($query);
$request = $request->withUri($url);
return $this->decodeResponse(
$nRequest->getFormat(),
$request,
$this->http_client->send($request)
);
}
/**
* Return the client using by instance.
*/
public function getClient(): Client
{
return $this->http_client;
}
/**
* Decode the data returned from the request.
*
* @param string $format json or xml
* @param RequestInterface $request Request object from Guzzle
* @param ResponseInterface $response Interface response object from Guzzle
*
* @throws RuntimeException
* @throws \maxh\Nominatim\Exceptions\NominatimException if no format for decode
*
* @return array|SimpleXMLElement
*/
private function decodeResponse(string $format, RequestInterface $request, ResponseInterface $response)
{
if ('json' === $format || 'jsonv2' === $format || 'geojson' === $format || 'geocodejson' === $format) {
return json_decode($response->getBody()->getContents(), true);
}
if ('xml' === $format) {
return new SimpleXMLElement($response->getBody()->getContents());
}
throw new NominatimException('Format is undefined or not supported for decode response', $request, $response);
}
}